Linux下创建交互式shell脚本对话框(让你的脚本不再是枯燥的代码)

1、创建一个消息框
#!/bin/bash
whiptail --title "51cto-mailinpaopao" --msgbox "5c1to-Create a message box . Choose Ok to continue." 10 60

运行shell脚本,效果如图:
 


2、创建一个可选择yes or no 的消息框
#!/bin/bash
if (whiptail --title "Test Yes/No Box" --yesno "Choose between Yes and No." 10 60) then   
    echo "You chose Yes. Exit status was $?."
else   
    echo "You chose No. Exit status was $?."
fi

运行shell脚本效果如图:
 

还一种写法

#!/bin/bash
if (whiptail --title "Which one do you like !!" --yes-button "51cto" --no-button "mailinpaopao"  --yesno "Which one do you like? " 10 60) then        echo "You chose 51cto Exit status was $?."
else   
    echo "You chose mailinpaopao Exit status was $?."
fi

效果如图:
 

3、创建一个表单输入框

#!/bin/bash
PET=$(whiptail --title "test an input" --inputbox "What is your name?" 10 60 NULL  3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then   
    echo "Your name is:" $PET
else   
    echo "You chose Cancel."
fi

效果如图:
 

4、创建一个密码框

#!/bin/bash
PASSWORD=$(whiptail --title "Test a password input" --passwordbox "Enter your password and choose Ok to continue." 10 60 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then   
    echo "Your password is:" $PASSWORD
else   
    echo "You chose Cancel."
fi

效果如图:
 

5、创建一个菜单栏来做选择

#!/bin/bash
OPTION=$(whiptail --title "Select your language" --menu "Choose your option" 15 60 4 \"1" "English" \"2" "French" \"3" "Chinese" \"4" "Japanese"  3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then   
    echo "Your chosen option:" $OPTION
else   
    echo "You chose Cancel."
fi

效果图:
 

6、创建radiolist对话框(这个做的就有点无聊了)
#!/bin/bash
DISTROS=$(whiptail --title "Test Checklist Dialog" --radiolist \"What is the Linux distro of your choice?" 15 60 4 \"debian" " Debian" OFF \"ubuntu" " Ubuntu" OFF \"centos" " CentOS" ON \"mint" " Mint" OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then   
    echo "The chosen distro is:" $DISTROS
else   
    echo "You chose Cancel."
fi

效果图:
 

7、创建一个进度条

#!/bin/bash
{ for ((i = 0; i <= 100; i+=20));
do sleep 1        
    echo $i   
done
} | whiptail --gauge "Please wait while installing" 6 60 0

效果(手速有限,可以自己体验,很有趣):
  
请使用浏览器的分享功能分享到微信等