1
我試圖使用以下命令找到一個文件,然後將第一個結果複製到由$ dir設置的目錄中。當我不設置變量並使用絕對路徑時,它工作正常,但這不是我所需要的。Bash - 找到文件,awk複製不正確
這不起作用:
dir="/path/to/destination/";
mkdir "$dir";
locate -l 1 target_file.txt | awk '{print "cp " $1 $dir " "}' | sh
錯誤消息是:
awk: illegal field $(), name "dir"
input record number 1, file
source line number 1
cp: fts_open: No such file or directory
這工作:
locate -l 1 target_file.txt | awk '{print "cp " $1 " /path/to/destination/"}' | sh
@Johnathan - 它現在可以工作,但我仍然收到錯誤或警告'cp:fts_open:沒有這樣的文件或目錄'。任何想法會導致什麼?非常感謝。 –
真的不知道多少。我會在命令結尾用'sh -x'代替'sh'開始調試。我懷疑你的'locate'命令是說應該有一個'fts_open'文件,但'cp'找不到它。爲了調試,我會插入'|在target_file.txt之後定位,然後在命令運行後仔細檢查locate.log文件。 –
有趣。至少它現在可以在你的幫助下工作,我一定會嘗試你提到的建議。再一次,謝謝你! :) –