我这里有问题。好像我的Bash脚本忽略了std::thread
和do
之间的所有内容。不知道为什么,也许你会看到问题。提前谢谢。
done
答案 0 :(得分:7)
尝试替换
read -p "Run command $foo? [yn]" answer
通过
read -p "Run command $foo? [yn]" answer </dev/tty
避免从标准输入读取。
使用Will的建议更新:
katalogas=$1
read -p "Run command $foo? [yn]" answer
if [[ $answer = y ]] ; then
find "$katalogas" -type f -mtime +3 | while read failai
do
rm "$failai"
done
fi
答案 1 :(得分:1)
因此,我看到的第一个问题是您的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
如果用户只按 Enter ,您还可以在"Y"
之后添加"N"
或"Run command ${foo}?"
来指定默认值。
编辑:似乎我错过了部分内容。 Cyrus's answer是read
在循环内部无法正常工作的解决方案。 This StackOverflow post也很好地解释了这一点。但是,基于你的评论@semkius,似乎你只想在循环之外问一次这个问题。
答案 2 :(得分:0)
使用命令reset
然后执行shell脚本。