當條目存在時,我的條件可以正常工作,但如果它們沒有,它似乎同時執行then
和else
語句(這是正確的術語?)。爲什麼這個條件返回「沒有這樣的文件或目錄」
script.sh
#!/bin/bash
if [[ $(find path/to/dir/*[^thisdir] -type d -maxdepth 0) ]]
then
find path/to/dir/*[^thisdir] -type d -maxdepth 0 -exec mv {} new/location \;
echo "Huzzah!"
else
echo "hey hey hey"
fi
提示
對於第一呼叫時,顯示目錄在那裏;在第二個,他們已經從第一個電話轉移。
$ sh script.sh
Huzzah!
$ sh script.sh
find: path/to/dir/*[^thisdir]: No such file or directory
hey hey hey
我該如何解決這個問題?
試圖建議(S)
if [[ -d $(path/to/dir/*[^thisdir]) ]]
then
find path/to/dir/*[^thisdir] -type d -maxdepth 0 -exec mv {} statamic-1.3-personal/admin/themes \;
echo "Huzzah!"
else
echo "hey hey hey"
fi
結果
$ sh script.sh
script.sh: line 1: path/to/dir/one_of_the_dirs_to_be_moved: is a directory
hey hey hey
你試過的建議*已經接近正確,但需要保留'$()',並且必須達到'... dir /!(thisdir)'而不是'* [^ thisdr]'。但是,只有當path/to/dir/* [^ thisdir]完全匹配** 1 **條目時,這纔會起作用。看看我的答案... –