第一次尝试Linux,学习bash只是为了好玩。试着写一个脚本。
下面是我的一段代码(bash):
sudo pacman -Suy | if grep --color=always -e "^" -e "there is nothing to do"; then pup=0; else pup=1; fi;
字符串
当我尝试回显变量时,什么也没有返回。我已经确认了变量根本没有被设置的多种方式。当我尝试在if语句中设置它时,echo $var工作得很好。
下面是我的一段很好的上下文代码:
case $yn in
[yY] ) echo running only yay -Suy;
echo running...;
yay -Suy | if grep --color=always -e "^" -e "there is nothing to do"; then yup=0; else yup=1; fi;
break;;
[nN] ) echo not updating;
echo exiting...;
exit;;
[pP] ) running only pacman -Suy;
echo running...;
sudo pacman -Suy | if grep --color=always -e "^" -e "there is nothing to do"; then pup=0; else pup=1; fi;
break;;
[bB] ) echo running full sys update;
echo running...;
sudo pacman -Suy | if grep --color=always -e "^" -e "there is nothing to do"; then pup=0; else pup=1; fi;
yay -Suy | if grep --color=always -e "^" -e "there is nothing to do"; then yup=0; else yup=1; fi;
break;;
* ) echo invalid response;;
esac
done
if [ "$pup" -eq 1 ]; then
echo "Pacman has updated something."
elif [ "$pup" -eq 0 ]; then
echo "Pacman has not updated anything."
else
:
fi
if [ "$yup" -eq 1 ]; then
echo "Yay has updated something."
elif [ "$yup" -eq 0 ]; then
echo "Yay has not updated anything."
else
:
fi
型
当我用“echo '0'”替换pup=0时,它返回0没有问题。我试着修改最后一个if语句,没有变化。
1条答案
按热度按时间gpnt7bae1#
变量赋值在管道中,管道运行在子shell中,所以变量赋值不会持久化。
将管道置于
if
的条件中,而不是将整个if
语句置于管道中。例如,change字符串
到
型