请教:如何在每次暂停时增加“Y” “N”选择是否执行本段命令?

sh/bash/dash/ksh/zsh等Shell脚本
回复
Points
帖子: 83
注册时间: 2010-02-19 16:13

请教:如何在每次暂停时增加“Y” “N”选择是否执行本段命令?

#1

帖子 Points » 2012-04-21 10:33

代码: 全选

#! /bin/bash
function pause(){
        read -n 1 -p "$*" INP
        if [[ $INP != '' ]] ; then
                echo -ne '\b \n'
        fi
}
 
echo “请设置root密码”
pause 'Press [Enter] key to continue...'
sudo passwd root

echo “同步Reaver设置”
pause 'Press [Enter] key to continue...'
Folder=~/.config/reaver
if [ -d "$Folder" ]; then
sudo rm -fr /usr/local/etc/reaver
sudo ln -fsT ~/.config/reaver /usr/local/etc/reaver
else
mkdir -p "$Folder"
cp -at /usr/local/etc/reaver ~/.config/reaver
fi

echo “设置本地源”
pause 'Press [Enter] key to continue...'
Folder="/media/iData/Backup/Ubuntu/Packages"
if [ -d "$Folder" ]; then
sudo rm -fr /var/cache/apt/archives
sudo ln -fsT "/media/iData/Backup/Ubuntu/Packages" "/var/cache/apt/archives"
else
mkdir -p "$Folder"
fi

#sources.list
 sudo add-apt-repository ppa:kernel-ppa/pre-proposed
 sudo add-apt-repository ppa:ubuntu-wine/ppa
 sudo add-apt-repository ppa:tualatrix/ppa
 sudo add-apt-repository ppa:ubun-囗囗囗/ppa
 sudo add-apt-repository ppa:deluge-team/ppa
 sudo add-apt-repository ppa:hydr0g3n/ppa

 sudo add-apt-repository ppa:thopiekar/oneiric-dev

 sudo add-apt-repository ppa:jason-scheunemann/ppa
 sudo add-apt-repository ppa:team-xbmc/unstable
 sudo add-apt-repository ppa:jonoomph/openshot-edge

echo “更新系统”
pause 'Press [Enter] key to continue...'
sudo apt-get update
sudo apt-get dist-upgrade

echo “任意键退出”
pause 'Press any key to continue...'
请教:如何在每次暂停时增加“Y” “N”选择是否执行本段命令?“y"执行本段,”n"跳到下一段? :em09
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 请教:如何在每次暂停时增加“Y” “N”选择是否执行本段命令?

#2

帖子 aerofox » 2012-04-21 11:19

像你的 pause 函数中一样,检测 $INP 就行了。
Points
帖子: 83
注册时间: 2010-02-19 16:13

Re: 请教:如何在每次暂停时增加“Y” “N”选择是否执行本段命令?

#3

帖子 Points » 2012-04-21 14:47

aerofox 写了:像你的 pause 函数中一样,检测 $INP 就行了。
不会写呀,给个样本吧!!求救 :em06
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 请教:如何在每次暂停时增加“Y” “N”选择是否执行本段命令?

#4

帖子 lilydjwg » 2012-04-21 15:03

read 命令应该可以做到这个,至少 zsh 的可以:

代码: 全选

read -q ans && mv -i $i $new
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 请教:如何在每次暂停时增加“Y” “N”选择是否执行本段命令?

#5

帖子 aerofox » 2012-04-21 16:24

Points 写了:
aerofox 写了:像你的 pause 函数中一样,检测 $INP 就行了。
不会写呀,给个样本吧!!求救 :em06
看来这个脚本不是你自己写的了。

代码: 全选

pause 函数就是样本呀。
pause '是否XXXXXX...(Y/n)'
if [[ "$INP" == [Yy] ]]; then
    #执行本段操作
fi
Points
帖子: 83
注册时间: 2010-02-19 16:13

Re: 请教:如何在每次暂停时增加“Y” “N”选择是否执行本段命令?

#6

帖子 Points » 2012-04-21 19:21

aerofox 写了:
Points 写了:
aerofox 写了:像你的 pause 函数中一样,检测 $INP 就行了。
不会写呀,给个样本吧!!求救 :em06
看来这个脚本不是你自己写的了。

代码: 全选

pause 函数就是样本呀。
pause '是否XXXXXX...(Y/n)'
if [[ "$INP" == [Yy] ]]; then
    #执行本段操作
fi
脚本是照自已需要改的,代码的功能是Google来的样本如:

代码: 全选

pause
Points
帖子: 83
注册时间: 2010-02-19 16:13

Re: 请教:如何在每次暂停时增加“Y” “N”选择是否执行本段命令?

#7

帖子 Points » 2012-04-22 13:23

代码: 全选

pause '安装Virtual Tools...(Y/n)'
if [[ "$INP" == [Yy] ]]; then

 sudo apt-get install virtualbox

cd ~ && wget -t 0 -w 31 -c "http://dl-sh-ctc-2.pchome.net/08/b7/VMware-Workstation-Full-8.0.2-591240.x86_64.bundle" --ref="http://download.pchome.net/system/sysenhance/down-10771-1.html" | sudo sh VMware*bundle
#chmod +x VMware*bundle
#sudo sh VMware*bundle

fi
再问下怎么弄成选1安vbox 选2安VM :em03
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 请教:如何在每次暂停时增加“Y” “N”选择是否执行本段命令?

#8

帖子 aerofox » 2012-04-22 21:40

代码: 全选

if [[ "$INP" == 1 ]]; then
    # 安装 vbox
elif [[ "$INP" == 2 ]]; then
    # 安装 VM
fi
已经有了一个 if 的例子,结合这个例子再看一下 man bash,应该是没坏处的。
回复