我正在尝试运行gem uninstall bundler
bash 脚本。当我安装了多个捆绑程序版本时会出现此问题。它会提示版本数,最后一个选项是卸载所有捆绑程序版本:-
root@testuser:/home/test# gem uninstall bundler -x
Select gem to uninstall:
1. bundler-1.13.7
2. bundler-1.16.0
3. All versions
All versions
我想输入文本前面显示的数字。
我知道我可以做类似的事情echo -e '3' | gem uninstall bundler
,但我不知道该All versions
选项是否显示在 3、4 或任何数字上。因此,我正在寻找一种解决方案,可以解析选择选项,然后在其前面输入数字。
答案1
答案2
我还没有尝试过这个,gem uninstall bundler
但它可能对你有用。
#launch your process in a subshell and direct the subshell output to a file
#yours will probably read (gem uninstall bundler -x) > count &
(echo title; echo "empty line"; echo "1. option"; echo "2. option" ; echo "3. All Versions"; sleep 10 ) > count &
pid=$! #get the pid of the subshell
disown #to avoid the untidy kill output
sleep 1 #just allow time for the subshell output
kill -9 $pid #kill the dummy process
option=$(grep -i "all versions" count | grep -Po "[^0-9]*[0-9]+")
#grab the option want from the output
rm count #tidy up
echo $option | gem etc.... #launch gem with the known option