如何在shell脚本中提示输入?

时间:2015-11-09 13:45:36

标签: shell menu sh

尝试创建一个脚本,它将在路由器上安装一些软件包,并在某些时候会要求用户输入所需的交换大小。一切都好,但是在第4点,脚本将退出,我想在选择1,2或3后不返回菜单并保持打开并继续运行代码的最后一部分,如果我选择4。

#!/bin/sh
# Some lines with code here...
# Swap file
while :
do
    clear
    echo Router model       
    cat "/proc/sys/kernel/hostname"
    echo "-----------"
    echo " SWAP FILE"
    echo "-----------"
    echo "Choose swap file size"
    echo "1. 256MB"
    echo "2. 512MB (recommended)"
    echo "3. 1024MB"
    echo "4. Skip, I already have a swap partition"
    echo "   or I don't want to create it right now"
        # get input from the user 
    read -p "Enter your choice [ 1 - 4 ] " choice
        # make decision using case..in..esac 
    case $choice in
        1)

            echo -e "$INFO Creating a 256MB swap file..."
            echo -e "$INFO This could take a while, be patient..."
            dd if=/dev/zero of=/opt/swap bs=1024 count=256144
            mkswap /opt/swap
            chmod 0600 /opt/swap
            read -p "Press [Enter] key to continue..." readEnterKey
            ;;
        2) 
            echo -e "$INFO Creating a 512MB swap file..."
            echo -e "$INFO This could take a while, be patient..."
            dd if=/dev/zero of=/opt/swap bs=1024 count=524288
            mkswap /opt/swap
            chmod 0600 /opt/swap            
            read -p "Press [Enter] key to continue..." readEnterKey
            ;;
        3)
            echo -e "$INFO Creating a 1024MB swap file..."
            echo -e "$INFO This could take a while, be patient..."
            dd if=/dev/zero of=/opt/swap bs=1024 count=1048576
            mkswap /opt/swap
            chmod 0600 /opt/swap
            read -p "Press [Enter] key to continue..." readEnterKey
            ;;
        4)
            exit 1
            ;;
        *)
            echo "Error: Invalid option..." 
            read -p "Press [Enter] key to continue..." readEnterKey
            ;;
    esac    
done

# Here will be the rest of the script

3 个答案:

答案 0 :(得分:0)

exit 1替换为break

这样的事情:

    1)
        echo -e "Some conditions"
        ;;
    2)
        break
        ;;
    *)
        echo "Error: Invalid option..."
        read -p "Press [Enter] key to continue..." readEnterKey
        ;;

我不确定你是否会问不同的东西,但这会让你多次输入选项1,只输入一次,或者#34;其他的" (然后输入)多次。

答案 1 :(得分:0)

实际上,break是目前为止最好的方法,这是我的工作脚本

# Swap file
while :
do
    clear
    echo Router model   
    cat "/proc/sys/kernel/hostname"
    echo "---------"
    echo "SWAP FILE"
    echo "---------"
    echo "Choose swap file size (Highly Recommended)"
    echo "1. 256MB"
    echo "2. 512MB (recommended)"
    echo "3. 1024MB"
    echo "4. Skip this step, I already have a swap file / partition"
    echo "   or I don't want to create one right now"
    read -p "Enter your choice [ 1 - 4 ] " choice
    case $choice in
        1)
            echo -e "$INFO Creating a 256MB swap file..."
            echo -e "$INFO This could take a while, be patient..."
            dd if=/dev/zero of=/opt/swap bs=1024 count=256144
            mkswap /opt/swap
            chmod 0600 /opt/swap
            swapon /opt/swap
            read -p "Press [Enter] key to continue..." readEnterKey
            break
            ;;
        2) 
            echo -e "$INFO Creating a 512MB swap file..."
            echo -e "$INFO This could take a while, be patient..."
            dd if=/dev/zero of=/opt/swap bs=1024 count=524288
            mkswap /opt/swap
            chmod 0600 /opt/swap
            swapon /opt/swap
            read -p "Press [Enter] key to continue..." readEnterKey
            break
            ;;
        3)
            echo -e "$INFO Creating a 1024MB swap file..."
            echo -e "$INFO This could take a while, be patient..."
            dd if=/dev/zero of=/opt/swap bs=1024 count=1048576
            mkswap /opt/swap
            chmod 0600 /opt/swap
            swapon /opt/swap
            read -p "Press [Enter] key to continue..." readEnterKey
            break
            ;;
        4)
            break
            ;;
        *)
            echo "ERROR: INVALID OPTION!"
            echo "Press 1 to create a 256MB swap file"
            echo "Press 2 to create a 512MB swap file (recommended)"
            echo "Press 3 to create a 1024MB swap file"
            echo "Press 4 to skip swap creation (not recommended)" 
            read -p "Press [Enter] key to continue..." readEnterKey
            ;;
    esac    
done

我可以简化一下吗?

答案 2 :(得分:-1)

您只需使用读取命令。

#!/bin/bash
echo "Choose your option: "
read answser
# The value is now stored in the variable answer
# To check this, you can use an echo command
echo $answer

我希望我帮忙!

修改

似乎我误解了你的问题。

因为我想帮助你,所以这是我的更正。如果要退出整个脚本,可以像使用 exit 一样使用 exit 。如果您只想退出某个循环/构造,可以使用 break 。这适用于您的情况。

另一种解决方案是省略输入4的大小写选项! 看起来有点像这样:

input=4
case $input in
  1) some stuff
  2) some stuff
  3) some stuff
esac
echo "Notice that I did not define any action for the value 4!"