shell之语句控制器、运算命令

1.脚本中的语句控制器

exit                       #退出脚本,退出值为n

break                    #退出当前循环

continue               #提前结束循环内部的命令,但不终止循环

@@输出1-10,但不输出4

方法一

  vim file1.sh

#######


 sh file.sh


方法二

vim file.sh

#########


 sh  file1.sh


2.运算方式及运算符号:




3.常用的运算命令:


$()    #先执行括号里的内容

$[]    #用于整数运算

${}    #声明变量

实验:

 (( a=1+1 ))

 echo $a

 a=1+1

 echo $a


 let a=1+1

 echo $a


 echo `expr 1+1`

 echo `expr 1 + 1`


 bc              ##bc为linux下的计算器

 bc <<EOF


 echo $[1+1]

 echo $[2**3]

 echo $[2*3]


@@ 10s倒计时

 vim time.sh

################


 sh time.sh


@@ 1min 10s 倒计时

方法一

vim time1.sh

######


 sh time1.sh


方法二

vim time2.sh

######


 sh time1.sh

 



@@制作计算器

方法一:(只能计算实数)

vim calculator.sh

#############



sh calculator.sh


方法二:(对浮点数也可以)

vim calculator.sh

##########


 sh calculator.sh


@@批量处理管理用户及其密码

vim useradd_create.sh                

##########

#!/bin/bash

Auto_Connect()

{

/usr/bin/expect <<EOF | grep -E "authenticity | ECDSA | connecting | Warning | spawn | passwd" -v

set timeout 5

spawn ssh [email protected].$NUM "$1"

expect {

       "yes/no"  { send "yes\r";exp_continue }

       "password:" { send "westos\r" }

}

expect eof

EOF

}

for NUM in 30

do

   ping -c1 -w1 172.25.254.$NUM &> /dev/null &&{

   Max_Line=`awk 'BEGIN{N=0}{N++}END{print N}' $1`

   for Line_Num in `seq 1 $Max_Line`

   do

       USERNAME=`sed -n ${Line_Num}p $1`

       PASSWORD=`sed -n ${Line_Num}p $2`

       User_Check=`Auto_Connect "useradd $USERNAME"`

       [ -n "$User_Check" ]&&{

       echo $User_Check

       }||{

       Auto_Connect "echo $PASSWORD | passwd --stdin $USERNAME"

       }

    done

}|| echo 172.25.254.$NUM is down

done

 sh useradd_create.sh userfile passfile

猜你喜欢

转载自blog.csdn.net/love_sunshine_999/article/details/80849559
今日推荐