【发布时间】:2017-11-26 11:22:56
【问题描述】:
这是我第一次涉足 bash 脚本,也是第一次使用这个网站。我正在编写脚本,该脚本旨在为用户提供要安装的软件包列表,然后将他们的选择输出到第二个脚本文件中,该文件可以稍后运行以实际安装他们的选择。 到目前为止,我的脚本是半工作的,但我需要帮助弄清楚如何做; A)循环脚本,因此一旦他们选择了一个包,它就会重新启动并允许他们选择另一个而不是结束脚本 B)当他们选择“否”或“nN”进行确认时,它会将他们带回选择列表而不是退出,如果他们输入的不是是/否,它会提示输入有效
这是我当前的脚本,我知道它的格式可能很糟糕,而且很可能效率低下,但这是我第一次,而且只针对我正在处理的一个小型学校项目。任何帮助将不胜感激,谢谢!
#!/bin/bash
#bash script to present list of packages for customer install output to txt
if [[ ! -e /home/aarone/pkglist.txt ]]; then
echo "Making package list script"
echo "#!/bin/bash" > /home/aarone/pkglist
chmod -R 777 /home/aarone/pkglist
fi
# Declare variable choice and assign value 4
choice=4
# print to stdout
echo "1. Antivirus GUI"
echo "2. Firewall GUI"
echo "3. MariaDB"
echo -n "Please choose a A package [1,2 or 3]? "
# Loop while the variable choice is equal 4
# bash while loop
while [ $choice -eq 4 ]; do
#read user input
read choice
# bash nested if/else
if [ $choice -eq 1 ]
then
echo "You have chosen word: Antivirus GUI"
apt show clamtk 2>/dev/null | egrep '^Description|^Download'
read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
echo "apt-get clamtk" >> pkglist
else
echo "Input not understood"
continue
fi
else
if [ $choice -eq 2 ] ; then
echo "You have chosen package: Firewall GUI"
apt show gufw 2>/dev/null | egrep '^Description|^Download'
read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
echo "apt-get gufw" >> pkglist
else
read choice
fi
else
if [ $choice -eq 3 ] ; then
echo "You have chosen package: Office"
apt show libreoffice 2>/dev/null | egrep '^Description|^Download'
read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
echo "apt-get libreoffice" >> pkglist
fi
else
echo "Please make a choice between 1-3 !"
echo "1. Antivirus GUI"
echo "2. Firewall GUI"
echo "3. Office application"
echo -n "Please choose a word [1,2 or 3]? "
choice=4
fi
fi
fi
done
谢谢@janos,这正是我想要的! :) 我想更改的唯一另一件事是创建脚本的目录(更默认),因此我可以在任何系统上使用它。 我还对每个选项进行了小幅调整,因此“否”提示现在也可以正常工作了。
1)
echo "You have chosen package: Antivirus GUI"
apt show clamtk 2>/dev/null | egrep '^Description|^Download'
while true; do
read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
echo "apt-get install -y clamtk" >> "$pkglist"
break
elif [[ "$response" =~ ^([nN][oO]|[nN])+$ ]]
then
echo "Cancelled"
break
else
echo "Input not understood"
fi
done
;;
【问题讨论】:
-
无论您想要完成什么,
chmod 777都是错误且危险的。 您应该弄清楚您的用例;但这实际上从不包括授予任何用户(包括匿名入侵者)对您文件的完全写入权限。 -
请永远不要使用
chmod 777
标签: linux bash shell ubuntu scripting