像这样的东西?
#set a variable saving the filename but not path of a file.
MY_FILENAME=$(basename $filename)
echo $MY_FILENAME >> /directory/log
mv $MY_FILENAME /diectroy/.
# DO STUFF HERE
# to your file here
#Move the file to the PWD.
mv /directory/${MY_FILENAME} .
unset $MY_FILENAME
#unseting variable when you are done with them, while not always
#not always necessary, i think is a good practice.
相反,如果要将文件移回 orgianl 位置而不是 PWD,则第二个 mv 语句将如下所示。
mv /directory/${MY_FILENAME} $filename
此外,如果由于某些范围问题,当您向后移动时您没有可用的本地 var,并且确实需要从文件中读取它,您应该这样做:
#set a variable saving the filename but not path of a file.
MY_FILENAME=$(basename $filename)
echo "MY_FILENAME = " $MY_FILENAME >> /directory/log
# I'm tagging my var with some useful title so it is easier to grep for it later
mv $MY_FILENAME /diectroy/.
# DO STUFF HERE
# to your file here
#Ive somehow forgotten my local var and need to get it back.
MY_FILENAME=$(cat /directory/log | grep "^MY_FILENAME = .*" | awk '{print $3}');
#inside the $() a cat the file to read it
# grep on "^MY_FILENAME = .*" to get the line that starts with the header i gave my filename
# and awk to give me the third token ( I always use awk over cut out of preference, cut would work also.
# This assumes you only write ONE filename to the log,
# writing more makes things more complicated
mv /directory/${MY_FILENAME} $filename
unset $MY_FILENAME