不那么无用的“是”bash 命令:如何在每个循环中确认命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1941242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The not-so-useless "yes" bash command: how to confirm a command in every loop
提问by Tonio
I wrote a loop to unzip all zip files in a directory.
我编写了一个循环来解压缩目录中的所有 zip 文件。
for f in *zip
do
unzip $f
done
However, I have to confirm the overwrite at every step:
但是,我必须在每一步都确认覆盖:
replace file123.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
How can I rewrite a loop to send at every cycle the same command?
如何重写循环以在每个周期发送相同的命令?
回答by Federico Giorgi
Wonderful, maybe one of the few cases where yesis still useful
精彩的极少数情况下,也许一个是仍然是有用的
Try with:
尝试:
for f in *zip
do
yes | unzip $f
done
Which will work printing "y" at every command.
这将在每个命令中打印“y”。
Or alternatively, you can specify the string provided by yes, like:
或者,您可以指定 yes 提供的字符串,例如:
for f in *zip
do
yes A | unzip $f
done
回答by Jonathan Feinberg
unzip -o $f
per the docs
根据文档
回答by Alberto Zaccagni
Try using
尝试使用
unzip -o
in your loop
在你的循环中
回答by user1977760
for f in *zip
do
echo "yes" | unzip $f
done

