Shell脚本语法

一 Shell中的数值运算及运算方式差异

1.数值运算

[root@localhost ~]# aa=11
[root@localhost ~]# bb=22
#给变量aa和变量bb赋值

 

方式一:

[root@localhost ~]# dd=$(expr $aa + $bb)
#dd的值是aa和bb的和。注意“+”号左右两侧必须有空格

方式二:

[root@localhost ~]# ff=$(( $aa+$bb ))

方式三:

[root@localhost ~]# gg=$[ $aa+$bb ]

eg:

表达式求值:

$ expr1 + 3
$ expr2 –1
$ expr1 \* 3 使用*要加转义符
$ echo `expr1 + 3`

 
注意:表达式中,数字和运算符指教要有空格,出现在语句中要加反引号

2.运算符优先级

3.运行方式差异

#脚本内容如下
[pingguo@localhostwork]$ cat sh01.sh
#!/bin/bash
read -p "please input your first name:" firstName
read -p "please input your last name:" lastName
echo -e "\nyourfull name is:"$firstName$lastName

方式一:

#执行脚本之前
[pingguo@localhostwork]$ echo $firstName$lastName #无该变量
[pingguo@localhostwork]$ sh sh01.sh
please input your first name:zhang
please input your last name:san
your full name is:zhangsan
#执行脚本之后
[pingguo@localhostwork]$ echo $firstName$lastName #无该变量
[pingguo@localhostwork]$

原因:

当使用直接运行的方法来处理时,系统会给予一支新的bash 让我们来运行脚本里面的命令,因此你的firstname, lastname等变量其实是在下图中的子程序bash 内运行的。当脚本运行完毕后,子程序bash 内的所有数据便被移除,因此上表的练习中,在父程序底下echo $firstname时就看不到任何东西了。

方式二:

[pingguo@localhostwork]$ source sh01.sh
please input your first name:zhang
please input your last name:san
your full name is:zhangsan
[pingguo@localhostwork]$ echo $firstName$lastName
zhangsan
[pingguo@localhostwork]$

原因:

 

二 Shell中的判断式及控制流程语句

1、判断式

1.条件判断命令test
test n1 -参数 n2(或–参数表达式)
真返回0,假返回1。

·test语句的等价形式[ 1 –lt4 ]
注意:表达式与中括号间有空格。

整数比较运算符

eg:

test 1–lt 4 #判断1<4
echo $? #结果为0

 

2.文件测试
-f存在且是普通文件
-d存在且是目录
-s存在且字节数大于0
-r存在且可读
-w存在且可写
-x存在且可执行
eg:

test -d “mydoc”
判断mydoc是否是目录

3、字符串测试
test s 字符串s非空
test s1 = s2字符串s1等于s2
test s1 != s2字符串s1不等于s2
test-z s字符串长=0,即为空串
test -n字符串长>0
4、其他参数
-a逻辑与
-o逻辑或
!逻辑非

2.控制流程

seq命令:产生1-9的数字序列

1.分支结构

if

if [ $# -eq 0 ]
then
     echo “输入了0个参数”
elif[ $# -gt 1 ]
then echo “输入了多个参数”
else echo “输入了1个参数”
fi

case

case “$#” in
0)echo “输入了0个参数”;;
1)echo “输入了1个参数”;;
*)echo “输入了多个参数”;;
esac

 注意:每个分支条件后必须以两个分号结尾

2.循环结构

for循环

#输出100内10的倍数
#for i in {1..9} #大括号中不能有变量
for i in `seq1 9`
do
       echo `expr$i\* 10`
done
或写  echo $(expr$i\* 10)

 注意:乘号前加转义符,与数字间要有空格。

while循环

#求1到100的和
i=1
sum=0
while [ $i –le 100 ]
do
  sum=$[ $sum+$i ]
  i=$[ $i+1 ]
done
echo $sum

 

#等待特定用户登录,每30秒确认一次
printf"Enter username:"
read user
while true
do
      if who |grep"$user" > /dev/null
      then
            break
      fi
      sleep 30
done

3. break可指定跳出几层循环

 eg:

#!/bin/bash
str=("a" "b" "c" "d")

for i in ${str[@]}
    do
          echo -n $i
          for j in `seq10`
              do
                   if [ $j -eq 5 ];then
                        break
                   fi
              echo -n $j
              done
           echo
    done

 

4.read

read 变量1 [变量2 …]
选项:

-p:指定读取值时的提示符;
read –p “please intput a num:”num

需注意:

可以从键盘上读取多个变量的值,用户输入数据时,以空格或者Tab键作为分隔。
如果输入的数据个数不够,则从左到右对应赋值,没有输入的变量为空;
如果输入的数据个数超了,则从左到右对应赋值,最后一个变量被赋予剩余的所有数据。

三 Shell中的函数

1.定义函数的格式为:

functions 函数名()# function可有可无

{

命令1

. . .

}

2.eg:

#!/bin/sh
# func1.sh
hello ()
{
echo "Hello there today's date is `date`“
}
echo "now going to the function hello”
hello
echo “back from the function"

3. 参数可以传递给函数,并由脚本进行访问:
fname arg1 arg2;#传递参数

四 Shell数组

1.数组定义

方式一:

可以在单行中使用一列值来定义一个数组:
array_var= (1 2 3 4 5 6)
#这些值将会存储在以0为起始索引的连续位置上
方式二:

将数组定义成一组“索引-值”:
array_var[0]="test1"
array_var[1]="test2"
array_var[2]="test3"
array_var[3]="test4"
array_var[4]="test5"

2.打印数组所有值及长度

echo ${array_var[*]}
test1 test2 test3 test4 test5

 个数为长度

$*——调用shell程序时所传送的全部参数的单字符串,“参数1”“参数2”…形式保存的参数

echo ${array_var[@]}
test1 test2 test3 test4 test5

$@“参数1参数2”…形式保存的参数

#打印数组长度:
echo ${#array_var[*]}
5

 3.关联数组

关联数组的下标和值称为键值对,它们是一一对应的关系。在关联数组中,键是唯一的,值可以不唯一。

注意:在使用关联数组之前,需要使用命令declare -A array 进行显示声明

eg:

name=(jim tom lucy)
declare -A phone
phone=([jim]=135 [tom]=136 [lucy]=158)
for i in `evalecho {0..$((${#name[*]}-1))}`
   do
         echo ${name[i]} phone number is ${phone["${name[i]}"]}
   done

 结果:

jimphone number is 135
tom phone number is 136
lucyphone number is 158

 

 

             语法               描述
        ${!array[*]}       取关联数组所有键
        ${!array[@]}       取关联数组所有键
        ${array[*]}
      取关联数组所有值
        ${array[@]}       取关联数组所有值
        ${#array[*]}       关联数组的长度
        ${#array[@]}       关联数组的长度
posted @ 2018-03-21 15:34  ·卿欢·  阅读(304)  评论(0编辑  收藏  举报