shell基本语法的介绍与使用


本文摘自PHP中文网,作者PHP中文网,侵删。

一、变量

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.测试举例

  数字比较测试:    

1

2

3

[root@bogon ~]# [[ 2 > 1 ]][root@bogon ~]# echo $?0

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

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

  字符串测试

1

2

3

[root@bogon ~]# [ "abc" = "abc" ][root@bogon ~]# echo $?0

[root@bogon ~]# [[ "abc" = "abc" ]][root@bogon ~]# echo $?0

[root@bogon ~]# (("abc" = "abc"))[root@bogon ~]# echo $?1

1

2

[root@bogon ~]# [[ a = a && 1 < 2 ]][root@bogon ~]# echo $?0

[root@bogon ~]# [[ a = a && 1 < 2 ]][root@bogon ~]# echo $?0

1

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

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

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

三、流程控制

1.if 分支机构

  1)验证用户账号密码:

1

2

3

4

5

input your name : zhangcan

input password : 123login successful

[root@bogon ~]# ./usertest.sh input your name : hha

input password : hag

user or password error

1

2

3

#! /bin/bashuser='zhangcan'password='123'read -p 'input your name : ' name

read -p 'input password : ' codeif [ $name = $user -a $code = $password ];then

        echo 'login successful'elseecho 'user or password error'fi~

  2)判断成绩档次

1

2

3

4

5

#!/bin/bash #根据用户输入的成绩,判断所属档次,并输出给用户read -p 'input your score : ' scoreif  [ $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

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

1

2

3

4

5

6

7

#!/bin/bashwhile :

do

    read -p 'input your file : ' fileif [ -z $file ];thencontinueelsebreakfi

doneif [ -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"elseecho "$file type unkonw"fi

3.for循环

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

  do

  echo $i

  done

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

1

2

3

4

5

6

#!/bin/bashfor 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下每种文件类型的数量

1

2

3

4

5

6

7

8

9

10

11

12

13

#!/bin/bashdir='/dev'for i in $(ls $dir)

doif [ -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:输出一个九九乘法表

1

2

3

4

5

6

#!/bin/bashfor ((i=1;i<=9;i++))

dofor ((j=1;j<=i;j++))

    do

        echo -n "$i*$j=$[$i*$j]"done

    echo

done

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

1

2

3

4

5

6

7

8

9

10

11

12

#!/bin/bashuser='zhangcan'password='123'tag=truewhile $tag

do

    read -p 'input your name : ' name

    read -p 'input your password : ' codeif [[ $name = $user ]] && [[ $code = $password ]];then

        echo 'login successful'while $tag

        do

            read -p '>>: ' cmdif [[ $cmd = 'quit' ]];then

                tag=falseelse$cmd

            fi

        done

    fi

done

              

以上就是shell基本语法的介绍与使用的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

实例分享shell字符截取命令之cut命令

shell script到底是什么?怎么使用?

linux默认的shell是什么

linux中bash是什么意思?

shell基本语法的介绍与使用

详细讲解linux shell中的printf的用法

linux中shell脚本怎么运行

shell编程--grep命令如何用?

netstat基本用法的介绍

shell脚本命令示例

更多相关阅读请进入《shell》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...