循环使用bash脚本

时间:2017-11-26 11:22:56

标签: linux bash shell ubuntu scripting

这是我第一次涉足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,这正是我想要的! :)我唯一想改变的是脚本创建的目录(更默认),所以我可以在任何系统上使用它。 我还对每个选项做了一个小调整,所以现在也可以使用“No”提示。

 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
            ;;

1 个答案:

答案 0 :(得分:2)

case语句可以帮助简化菜单的实现。

要重复步骤,您可以使用无限循环:

while true; do
    # ...
done

应用上述建议,然后一些, 脚本可以更正和改进:

#!/bin/bash

pkglist=/home/aarone/pkglist.txt 
if [[ ! -e "$pkglist" ]]; then
    echo "Making package list script"
    echo "#!/bin/bash" > "$pkglist"
    chmod -R 777 "$pkglist"
else
    echo Package list script already exists. Exiting.
    exit 1
fi

while true; do
    echo "1. Antivirus GUI" 
    echo "2. Firewall GUI" 
    echo "3. MariaDB" 
    echo "x. Exit"
    printf "Please choose a A package [1, 2 or 3]? "
    read choice

    case "$choice" in
        1)
            echo "You have chosen word: 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 clamtk" >> "$pkglist" 
                    break
                else
                    echo "Input not understood"  
                fi
            done
            ;;

        2)
            echo "You have chosen package: Firewall GUI"
            apt show gufw 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 gufw" >> "$pkglist" 
                    break
                else 
                    echo "Input not understood"  
                fi
            done
            ;;

        3)
            echo "You have chosen package: Office"
            apt show libreoffice 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 libreoffice" >> "$pkglist" 
                    break
                else 
                    echo "Input not understood"  
                fi
            done
            ;;

        *)
            break
    esac
done
相关问题