Linux基础系列:常用命令(8)_shell script

一、什么是shell script

  将OS命令堆积到可执行的文件里,由上至下的顺序执行文本里的OS命令 就是脚本了.
  再加上些智能(条件/流控)控制,就变成了智能化脚本了

二、变量命名规则

  以字母或下划线开头,剩下的部分可以是:字母、数字、下划线.

遵循下述规范:

  1.以字母开头
  2.使用中划线或者下划线做单词的连接
  3.同类型的用数字区分
  4.对于文件最好加上拓展名
    例如: sql_bak.tar.gz,log_bak.tar.bz2 

定义变量名的边界

[root@MiWiFi-R3-srv ~]# rest_mem=20
[root@MiWiFi-R3-srv ~]# echo ${rest_mem}%
20%

(()) 比较

测试操作 [] 

1   # x=1

2   # [ $x -gt 1 ]

3   # echo $? 4   0 

=    :字符是否相等

!=    :字符不相等

-eq    :判断数字是否相等

-ne    :判断数字不相等

-gt    :判断一个数字大于另一个数字

-ge    :判断一个数字大于等于另一个数字

-lt      :判断一个数字小于另外一个数字

-le      :判断一个数字小于等于另外一个数字

 

-b      :文件存在并且是一个块设备文件

-d      :文件存在并是一个目录文件

-e      :文件存在

-f       :文件存在,并且是一个普通文件

-a      :与

-o      :或

三、流程控制

if 循环     用户验证:

1 #!/bin/bash
2 read -p 'please input your name:' username
3 read -p 'please input your passwd:' passwd
4 
5 if [ $username = 'alex' -a $passwd = 'alex3417' ];then
6     echo 'login successful'
7 else     
8     echo 'login err'
9 fi

猜年龄:

 1 #!/bin/bash
 2 age=57
 3 read -p 'please input oldboy age' oldboy_age
 4 if [ $oldboy_age -gt $age ];then
 5     echo 'too older'
 6 elif [ $oldboy_age -lt $age ];then
 7     echo 'too yanger'
 8 else
 9     echo 'congratulation!'
10 fi

while循环   验证文件类型

 1 while :
 2 do
 3     read -p 'input you file: ' file
 4     if [ -z $file ];then
 5         continue
 6     else
 7         break
 8     fi
 9 done
10     if [ -b $file ]; then
11         echo "$file is block file"
12     elif [ -f $file ]; then
13         echo "$file is regular file"
14     elif [ -d $file ]; then
15         echo "$file is direvtory file"
16     else 
17         echo '$file type unkown'
18     fi

while和if循环      猜年龄(2)

 1 #!/bin/bash
 2 age=57
 3 while :
 4 do
 5     read -p 'please input oldboy age:' oldboy_age
 6     if [ -z $oldboy_age ];then
 7         echo 'age is null'
 8         continue
 9     fi
10     if [ $oldboy_age -gt $age ];then
11         echo 'too older'
12     elif [ $oldboy_age -lt $age ];then
13         echo 'too yanger'
14     else
15         echo 'congratulation!'
16         break
17     fi
18 done

for 循环  输出奇数

1 #!/bin/bash
2 num=0
3 for i in {1..10}
4 do
5     if ((i%2 == 0));then
6         continue
7     fi
8     echo '======'$i
9 done

for循环   测试局域网有效ip

1 #!/bin/bash  
2 for i in {1..20}
3 do
4     ping -c1 192.168.16.$i &> /dev/null    #-c1 只ping一次; &> /dev/null 将无用信息写入空文件
5 if [ $? -eq 0 ];then 6 echo "192.168.16.$i" 7 fi 8 done

while 、for、if嵌套循环    统计/dev下每种类型文件的数量:

 1 #!/bin/bash
 2 while :
 3 do
 4     read -p 'your dir:' dirname
 5     if [ -z $dirname ];then
 6         continue
 7     else
 8         break
 9     fi
10 done
11 for i in $(ls $dirname)
12 do
13     if [ -h $dirname/$i ];then
14         ((link_file+=1))
15     elif [ -f $dirname/$i ];then
16         ((putong_file+=1))
17     elif [ -d $dirname/$i ];then
18         ((dir_file+=1))
19     fi
20 done
21 echo "pu tong wen jian shu:$putong_file"
22 echo "mu lu wen jian shu:$dir_file"
23 echo "lian jie wen jian shu:$link_file"

两层循环    输出九九乘法表

1 #!/bin/bash
2 for i in {1..9}
3 do
4         for ((j=1;j<=i;j++))
5         do
6                 echo -n "$i*$j= $[$i*$j]  "
7         done
8         echo
9 done

登录验证完善版

 1 #!/bin/bash
 2 user='egon'
 3 passwd='123'
 4 tag=true
 5 while $tag
 6 do
 7         read -p 'please input your name:' username
 8         read -p 'please input your passwd:' pas
 9         if [[ $username = $user ]] && [[ $pas = $passwd ]];then
10                 echo 'login successful'
11                 while $tag
12                 do
13                         read -p '>> : ' cmd
14                         if [[ $cmd = 'quit' ]];then
15                                 tag=false
16                         else
17                                 $cmd
18                         fi
19 
20 
21                 done
22         else
23                 echo 'login err'
24         fi
25 done

 

转载于:https://www.cnblogs.com/hedeyong/p/6945153.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值