shell基本语法,shell语法


一、变量

1.变量的命名规则:以字母或下划线开头,后面跟数字,字母或下划线,最好不要随便命名,要做到看见变量名能猜出其含义

2.变量赋值: x=100

      echo $x

 删除变量:unset x

3.定义变量名的边界用大括号 

[root@bogon ~]# egon_salary=20000
[root@bogon ~]# echo ${egon_salary}yuan
20000yuan

4.bash中不必声明数据类型,默认都是字符型

二、运算符

1.算术运算符:+ - * / %

[root@bogon ~]# echo $[5%2]
1

2.赋值运算符:=,+=,-=,*=,/=,%=

[root@bogon ~]# x=10
[root@bogon ~]# ((x+=1))
[root@bogon ~]# echo $x
11

3.关系运算符:<,>,!=,==,>=,<=,||,&&

关系运算符常与(( ))连用,[]可以达到同样的结果,但(( ))不能判断一个文件的类型,判断文件类型必须要用到[],[]又和test命令效果一样

用$?查看命令执行结果,结果为0代表真,非0代表假

[root@bogon ~]# x=10
[root@bogon ~]# ((x>=8))
[root@bogon ~]# echo $?
0

4.shell里的计算器

之前说过用$[]可以进行一些简单的运算,但是如果涉及到小数的运算,就需要用到shell里面的计算器了

首先要安装软件,yum install -y bc

[root@bogon ~]# res=$(echo 'scale=2;1/3' |bc -l |cut -d'.' -f2)
[root@bogon ~]# echo ${res}%
33%

5.test 命令测试

  test 

     -n str 字符串长度不为零

     -z  str 字符串长度为零

     -b 文件存在且为块文件

     -d 文件存在且为目录文件

     -e 文件存在

     -f 文件存在且为普通文件

     -h 文件存在且为链接文件(同 -L)

     -s 文件存在且大于零字节

   文件之间的比较

    file1 -nt file2  file1 的创建时间比file2晚

    file1 -ot file2  file1 的创建时间比file2早  

   整数之间的比较

    int1 -ne int2 int1和int2不相等

    int1 -eq int2 int1和int2 相等

    int1 -lt int2  int1小于int2

    int1 -le int2 int1小于等于int2

    int1 -gt int2 int1大于int2

    int1 -ge int2 int1大于等于int2

  字符串之间比较

    str1 = str2 str1和str2相等

    str1 !=str2 str1和str2不相等

  表达式之间的比较

    expression1 -a expression2  表达式1与表达式2都为真

    expression1 -o expression2 表达式1或表达式2为真

6.测试举例

  数字比较测试:    

[root@bogon ~]# [[ 2 > 1 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# ((20>10))
[root@bogon ~]# echo $?
0
[root@bogon ~]# ((20<10))
[root@bogon ~]# echo $?
1

  字符串测试

[root@bogon ~]# [ "abc" = "abc" ]
[root@bogon ~]# echo $?
0
[root@bogon ~]# [[ "abc" = "abc" ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# (("abc" = "abc"))
[root@bogon ~]# echo $?
1
[root@bogon ~]# [[ a = a && 1 < 2 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# [[ a = a && 1 < 2 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# (( a = a || 1 > 2 ))
[root@bogon ~]# echo $?
1
[root@bogon ~]# [[ a = a || 1 > 2 ]]
[root@bogon ~]# echo $?
0

单纯比较数字,用(( ))

除了单纯数字之外的比较,用[[ ]]

三、流程控制

1.if 分支机构

  1)验证用户账号密码:

input your name : zhangcan
input password : 123
login successful
[root@bogon ~]# ./usertest.sh 
input your name : hha
input password : hag
user or password error
#! /bin/bash
user='zhangcan'
password='123'

read -p 'input your name : ' name
read -p 'input password : ' code
if [ $name = $user -a $code = $password ];then
        echo 'login successful'
else
        echo 'user or password error'
fi
~                                             

  2)判断成绩档次

#!/bin/bash 
#根据用户输入的成绩,判断所属档次,并输出给用户
read -p 'input your score : ' score
if  [ $score -ge 90 ];then
    echo '优秀'
elif [ $score -ge 70 -a $score -lt 90 ];then
    echo '良好'
elif [ $score -ge 60 -a $score -lt 70 ];then
    echo '及格'
elif [ $score -lt 60 ];then
    echo '较差'
fi

2.while循环    

  while(条件)

  do 

  命令

  done

示例:判断用户输入的文件是何种类型

#!/bin/bash
while :
do
    read -p 'input your file : ' file
    if [ -z $file ];then
        continue
    else
        break
    fi
done
if [ -f $file ];then
    echo "$file is regular file"
elif [ -b $file ];then
    echo "$file is block file"
elif [ -d $file ];then
    echo "$file is directory file"
else
    echo "$file type unkonw"
fi

3.for循环

  for i in {1..10}      #in后面不一定是数字,只要是有返回结果的命令都可以

  do

  echo $i

  done

示例1:写一个脚本,测试子网内可以使用的IP

#!/bin/bash
for i in {1..50}
do
    ping -c1 192.168.16.$i &> /dev/null  # -c1表示ping一次
    if [ $? -ne 0 ];then
        echo "192.168.16.$i successful"
        echo "192.168.16.$i" >> ~/ipavailable.txt
    fi
done
~                

示例2:统计/dev下每种文件类型的数量

#!/bin/bash
dir='/dev'
for i in $(ls $dir)
do
    if [ -h $dir/$i ];then
        ((link+=1))
    elif [ -f $dir/$i ];then
        (( rfile+=1))
    elif [ -d $dir/$i ];then
        ((directory+=1))
    elif [ -b $dir/$i ];then
        (( block+=1 ))
    else
        (( typeunknow+=1))
    fi
done
echo 'block' $block
echo 'regular file' $rfile
echo 'directory' $directory
echo 'link' $link
echo 'unknow' $typeunknow

4.嵌套循环

示例1:输出一个九九乘法表

#!/bin/bash
for ((i=1;i<=9;i++))
do
    for ((j=1;j<=i;j++))
    do
        echo -n "$i*$j=$[$i*$j]"
    done
    echo
done

示例2:验证用户登陆账号密码,登陆成功后可以执行命令,当输入quit时退出

#!/bin/bash
user='zhangcan'
password='123'
tag=true
while $tag
do
    read -p 'input your name : ' name
    read -p 'input your password : ' code
    if [[ $name = $user ]] && [[ $code = $password ]];then
        echo 'login successful'
        while $tag
        do
            read -p '>>: ' cmd
            if [[ $cmd = 'quit' ]];then
                tag=false
            else
                $cmd
            fi
        done
    fi
done

 

 

                         

相关内容