shell 常用语句

###########for 语句 #############
例1:
输出10秒倒计时
vim y.sh
{
#!/bin/bash
for SEC in {10…1}
do
echo -n “After ${SEC}s is end”
echo -ne “\r”
sleep 1
done
}
例2:
vim y1.sh
{
#!/bin/bash
[ -e “/test/host_ip” ] &&{
echo /test/host_ip is exist !!
echo “[O]verwrite [B]ackup [S]kip”
read -p "Please input your action: " WORD
ACTTON=echo $WORD | tr 'a-z' 'A-Z'
[ “KaTeX parse error: Expected 'EOF', got '&' at position 17: …CTTON" = "O" ] &̲&{ …ACTTON” = “B” ] &&{
mv /test/host_ip /test/host_ip.bak
}
[ "KaTeX parse error: Expected 'EOF', got '&' at position 17: …CTTON" = "S" ] &̲&{ …ip &> /dev/null &&{
echo 172.25.254.$ip >> /test/host_ip
}
done
}

############while 语句 ######################
vim w.sh
{
#!/bin/bash
while true
do
read -p “Please input word:” WORD
echo $WORD
done
}

###########until 语句 ########################
vim u.sh
{
#!/bin/bash
until false
do
read -p "Please input word: " WORD
echo $WORD
done
}

例:
倒计时1分10秒

vim l.sh
{
#!/bin/bash
SEC=10
MIN=1
for ((SEC=10;SEC>=0;SEC–))
do
while [ “SEC"="0"a"SEC" = "0" -a "SEC"="0"−a"MIN” = “0” ] #当分和秒都为0时退出
do
exit 0
done

    while [ "$SEC" = "0" ]         #当秒为0时执行while语句中内容
    do
            echo -ne "After $MIN:$SEC is end !!"
            echo -ne "\r"
            sleep 1
            SEC=59        
            ((MIN--))              #当秒为59时分自减1
    done
    echo -ne "After $MIN:$SEC is end !!"
    echo -ne "\r"
    sleep 1

done
}

############ if 语句 #######################
例:
当输入 -a 时建立文件
-d 时删除文件
-p 时移动文件
输入其他时报错 : Please input -a | -d | -p fllowing filectrl.sh

vim filectrl.sh
{
#!/bin/bash
CLO(){
echo -e “\033[${1}m$2\033[0m”
} #设定颜色函数,方便调用

NAME(){ #设定函数用于循环

    read -p "Please input your action: " WORD
    if  [ "$WORD" = "-a"  ]     #判断输入是否为-a后执行-a的操作
    then
           read -p " Please input filename: " FILE
            [ -e "$FILE" ] &&{          
                    CLO  31  "$FILE is exit"     #判断所要建立的文件是否存在,存在时提醒文件已经存在
            }||
            {
                    touch $FILE
                    CLO 32 "$FILE touch successful "   
            }    #文件不存在时,建立文件,建立后提示文件建立成功
    elif [ "$WORD" = "-d" ]    #判断输入是否为-d后执行-d的操作
    then
            read -p "Please input delete filename: " FILE
            [ -e "$FILE" ]  &&{      #如果文件存在就执行删除命令,删除后提示删除成功
                    rm -rf  $FILE
                    CLO 32  "$FILE  is deleted  "
            }||
            {
                    
		CLO 31 "$FILE is not exit !!!"  #文件不存在提示文件不存在
            }
    elif [ "$WORD" = "-p" ]
    then
            read -p "Please input move filename: " FILE
            [ -e "$FILE" ] ||{
                    CLO 31 "$FILE is not exit !!!"
            }&&
            {
		mv  $FILE /mnt
                    CLO 32 "$FILE is moved"
            }    #文件存在执行复制命令,文件不存在提示文件不存在
    else
                    CLO 31 "Please input -a | -d | -p fllowing filectrl.sh"
                    exit 0   #输入的不是-a  -d  -p 会退出
    fi
    NAME

}
NAME
}

############## case 语句 ################################
例:
当输入 -a 时建立文件
-d 时删除文件
-p 时移动文件
输入其他时报错 : Please input -a | -d | -p fllowing filectrl.sh

vim case.sh
{
#!/bin/bash
Check_File()
{
if [ -e “$1” ]
then
$2
$3
fi
}

if [ “$#” -lt “2” ]
then
echo “Error:Please input option [-a|-p|-d] and filename following $0”
exit 0
fi

case $1 in
-a|a|-A|A)
Check_File $2 “echo $2 is exist !!” exit
touch $2
;;
-d|d|-D|D)
Check_File $2 “rm -rf $2” exit
echo “$2 is not exis”
;;
-p|p|-P|P)
Check_File $2 “cp -rp $2 /mnt” exit
echo “$2 is not exist”
;;
*)
echo “Please input -a|-d|-p following $0 !!”
esac
}

############ expect #########################
例:
找出1-10号机中开着的主机,并将主机名 ip 存放到/mnt/host_list下

vim auto.sh
{
#!/bin/bash
Auto_Connect()
{
/usr/bin/expect <<EOF
spawn ssh root@$1 KaTeX parse error: Can't use function '\r' in math mode at position 32: …no" { send "yes\̲r̲" ; exp_continu…NUM &> /dev/null
if [ “?&quot;=&quot;0&quot;]thenecho&quot;AutoConnect172.25.254.?&quot; = &quot;0&quot; ] then echo &quot;`Auto_Connect 172.25.254.?"="0"]thenecho"‘AutoC​onnect172.25.254.NUM hostname | tail -n 1` A 172.25.254.$NUM”>> /mnt/host_list
fi
done
sed ‘s/^M//g’ -i /mnt/host_list

}

^M : ctrl+v ctrl+M

上一篇:Android错误:Please configure Android SDK


下一篇:Linear Mixed Models