到這樣的事情?
#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
而且如果由於某些範圍界定問題,你沒有thate局部變量可用,當你做回遷,真正需要從文件中讀取優爾應該做這樣的事情:
#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
問題不清楚。請詳細說明。你可以像你所描述的那樣使用'basename'。 – mtk
那麼,在這種情況下使用_grep_有效,如果是這樣,我怎麼得到該輸出與_mv_一起使用?如果沒有,那裏有什麼選擇? – user1732866