所以我看到的第一個問題是,你的while read $failai
應該是while read failai
(不$
)。試試這個:
katalogas="$1"
find "$katalogas" -type f -mtime +3 | while read failai; do
read -p "Run command ${foo}? [yn]" answer
if [[ "$answer" = "y" ]]; then
echo labas
fi
done
至於提示是或否,不過,我通常使用這樣的事情:
function prompt_yn()
{
local default=""
local prompt="y/n"
local input
# If $2 specifies a default choice, configure options accordingly.
if [[ "${2:-}" = "Y" ]]; then
prompt="Y/n"
default="Y"
elif [[ "${2:-}" = "N" ]]; then
prompt="y/N"
default="N"
fi
# Prompt the user until they give an appropriate answer.
while true; do
read -p "$1 [${prompt}] " input
[[ -z "$input" ]] && input="$default"
case "$input" in
[Yy]*) return 0;;
[Nn]*) return 1;;
*) echo "Please answer yes or no.";;
esac
done
}
所以,如果你使用上面的代碼,你的代碼應該是這樣的:
katalogas="$1"
find "$katalogas" -type f -mtime +3 | while read failai; do
if prompt_yn "Run command ${foo}?"; then
echo labas
fi
done
您還可以添加一個"Y"
或"Run command ${foo}?"
後"N"
指定一個默認值,如果用戶只是按下Ë nter。
編輯:似乎我錯過了關於這一部分的觀點。 Cyrus's answer是read
不能在循環內工作的解決方案。 This StackOverflow post也解釋得很好。然而,根據你的評論@semkius,似乎你只想在循環之外提問一次。
你在哪裏設置'$ failai'? –
'read ... answer'通過stdin從你的「find」命令中讀取, – Cyrus
Michael Jaros,我編輯了我的帖子,現在你會看到我在哪裏使用$ failai :) – semkius