在下面的程序中,如果我$foo在第一个if语句中将变量设置为值1 ,那么它的作用就是在if语句之后记住它的值.但是,当我将同一个变量设置if为while语句内部的值2时,它在while循环之后就被遗忘了.它的行为就像我$foo在while循环中使用某种变量的副本而我只修改那个特定的副本.这是一个完整的测试程序:
#!/bin/bash
set -e
set -u
foo=0
bar="hello"
if [[ "$bar" == "hello" ]]
then
foo=1
echo "Setting \$foo to 1: $foo"
fi
echo "Variable \$foo after if statement: $foo"
lines="first line\nsecond line\nthird line"
echo -e $lines | while read line
do
if [[ "$line" == "second line" ]]
then
foo=2
echo "Variable \$foo updated to $foo inside if inside while loop"
fi
echo "Value …Run Code Online (Sandbox Code Playgroud) 我想警告用户,如果他们的提交消息不遵循某组指南,然后给他们选项来编辑他们的提交消息,忽略警告或取消提交.问题是我似乎无法访问stdin.
下面是我的commit-msg文件:
function verify_info {
if [ -z "$(grep '$2:.*[a-zA-Z]' $1)" ]
then
echo >&2 $2 information should not be omitted
local_editor=`git config --get core.editor`
if [ -z "${local_editor}" ]
then
local_editor=${EDITOR}
fi
echo "Do you want to"
select CHOICE in "edit the commit message" "ignore this warning" "cancel the commit"; do
case ${CHOICE} in
i*) echo "Warning ignored"
;;
e*) ${local_editor} $1
verify_info "$1" $2
;;
*) echo "CHOICE = ${CHOICE}"
exit 1
;;
esac
done
fi
}
verify_info …Run Code Online (Sandbox Code Playgroud) 所以我想尝试做以下事情:
while read line; do
read userInput
echo "$line $userInput"
done < file.txt
Run Code Online (Sandbox Code Playgroud)
所以说file.txt有:
Hello?
Goodbye!
Run Code Online (Sandbox Code Playgroud)
运行该程序将创建:
Hello?
James
Hello? James
Goodbye!
Farewell
Goodbye! Farewell
Run Code Online (Sandbox Code Playgroud)
问题(自然地)成为用户输入读取从stdin读取,在我们的例子中是文件.txt.有没有办法改变它从临时读取到终端的位置以获取用户输入?
注意:我正在使用的文件长度为200,000行.每条线长约500个字符.因此,如果需要,请记住这一点
我这里有问题.好像我的Bash脚本忽略了do和之间的所有内容done.不知道为什么,也许你会看到问题.提前致谢.
katalogas=$1
find $katalogas -type f -mtime +3 | while read $failai
do
read -p "Run command $foo? [yn]" answer
if [[ $answer = y ]] ; then
rm $failai
fi
done
Run Code Online (Sandbox Code Playgroud) 我想等待按键并在按下字母时退出q。该脚本不等待密钥。如何纠正呢?
while read line
do
...
while :
do
read -n 1 key
if [[ $key = q ]]
then
break
fi
done
done < $1
Run Code Online (Sandbox Code Playgroud) 我需要script.sh,这将创建文件f1.txt和f2.txt发送到stdin的内容.例如:
echo ABRACODABRA | script.sh
Run Code Online (Sandbox Code Playgroud)
...应该创建文件f1.txt和f2.txt内容ABRACODABRA.
我怎样才能做到这一点?
请提供script.sh身体!