ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Linux下创建交互式shell脚本对话框(让你的脚本不再是枯燥的代码)

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

Linux操作系统 作者:luashin 时间:2016-03-22 13:16:19 0 删除 编辑
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

效果(手速有限,可以自己体验,很有趣):
  

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9034054/viewspace-2061698/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2015-02-18

  • 博文量
    290
  • 访问量
    1151030