Linux之bash脚本编程---if补充和for循环

简介:

if 单分支、双分支、多分支、嵌套if语句

for 列表表示

    1、给出列表

    2、{1..100}

    3、命令引用:

        1)$(ls DIR)

        2)$(1 1 100)

    4、glob

    5、$*,$@

condition   
declare -i -r -x

readonly

export ,env

set,printenv,env,export,readonly -p



bash:过程式编程,为了完成更复杂的任务,支持顺序执行、选择执行、循环执行

  顺序执行:从左而右,依次执行命令。

  选择执行:依据condition(条件)的执行状态结果,选择执行不同的代码片段。

  循环执行:依据condition(条件)的执行状态结果,决定是否进入循环。

  

condition:

   ture: 表示条件状态结果为0

   false: 表示条件执行状态结果非0


if

   单分支、双分支、多分支、嵌套if语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
1、单分支结构
if  condition;  then
     if -ture
fi
 
2、双分支结构
if  condition;  then
     if -ture
else
     if - false
fi
 
3、多分支结构
if  condition;  then
     if -ture
elif  condition;  then
     if -ture
elif  condition;  then
     if -ture
     ....
else
     all- false
fi
 
4、嵌套 if 语句
if  condition;  then
     if  condition;  then
         if -ture
     fi
fi
 
if  condition;  then
     if  condition;  then
         if -ture
     fi
else
     if  condition;  then
         if -ture
     fi   
fi


for


列表循环

格式

1
2
3
4
5
6
7
8
9
10
11
12
13
1、格式一
for  变量名  in  列表;  do
     循环体
done
 
2、格式二
for  变量名  in  列表
do
     循环休
done
 
3、格式三,命令行中
for  变量名  in  列表;  do  循环体;  done

列表表示方法

    1、给出列表

    2、{首位..尾数} ,例如 1到100表示为" {1..100}"

    3、命令引用:

        1)$(ls DIR)

        2)$(expr [首数 [步长]] 尾数),例如 1到100表示为"$(1 1 100)"

    4、通配符, glob . 例如 /var目录下一级子目录中的所有文件和目录的绝对路径。/var/*

    5、变量引用。 $* 向脚本传递的所有参数,整体。$@向脚本传递的所有参数,每个独立

    

脚本格式

1
2
3
4
#!/bin/bash
# Version: major.minor.release
# Author:
# Desc:


脚本语法检测

1
# bash -n file.sh


调试脚本

1
# bash -x file.sh


记录脚本退出状态码

1
INTEVAL=$? (变量引用实现赋值)


用户邮箱位置

1
/var/mail


定义变量的类型

1
整型:  # declare -i var


定义变量

1
2
3
4
5
6
7
8
1、环境变量
# env var
# declare -x var
# export var
 
2、只读变量
# declare -r var
# readonly var

显示本地和环境变量

1
# set

显示环境变量

1
2
3
# export
# env
# printenv


显示只读变量

1
# readonly -p


示例一:交互式给出一个文件路径,判断文件的类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
# Version: 0.0.1
# Author: Lcc.org
# Description:testing
 
read  -t 5 -p  'Enter a file path: '  filename
 
if  [ -z  "$filename"  ];  then
     echo  "Enter a file path"
     exit  1
fi
 
if  [ ! -e $filename ];  then
     echo  "No such file."
     exit  2
fi
 
if  [ -f $filename ];  then
     echo  "Common file."
elif  [ -h $filename ];  then
     echo  "Symbolic file."
elif  [ -d $filename ];  then
     echo  "Directory."
else 
     echo  "Other type."
fi


示例二:添加10个用户,user1 ,,....user10,密码同用户名(只有root能改密码)。

**只有root能修改密码**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
1、列表,直接给出
#!/bin/bash
# Version: 0.0.2
# Author: Lcc.org
# Description: add user
 
##避免执行命令的用户非root用户,非root用户是不能修改密码的。
if  [ $UID - ne  0 ];  then
   echo  "Only root."
   exit  1
fi
 
##以给出列表的方式,循环。当列表循环完毕时,循环结束
for  in  user1 user2 user3 user4 user5 user6 user7 user8 user9 user10;  do
 
   ## 判断用户是否存在。
   if  id  $i &>  /dev/null then
     ## 执行状态结果为0时,条件为真时,说明什么呢?
     echo  "$i exist"
   else
         ## 用户不存在时,即可添加用户
     if  useradd  $i 2>  /dev/null then
       ## 添加不成功时, 组合中,对命令或测试条件取反。
       echo  "$i is outside the law"
     else
       ## 用户名,正常,能正常添加用户,则可以给其添加密码
           echo  "$i"  passwd  --stdin $i >  /dev/null  2>&1
           ## 数值测试添加密码的执行状态结果。
       if  [ $? - ne  0 ];  then
         ## 结果不为0,表示执行不成功
         echo  "password is not legal"
       fi
     fi
   fi
done
 
2、{1..10}
#!/bin/bash
# Version: 0.0.3
# Author: Lcc.org
# Description: {}表示列表
 
[ ! $UID - eq  0 ] &&  echo  "Only root."   &&  exit  1
 
for  in  {1..10}
do
   id  user$i &>  /dev/null
   if  [ $? - eq  0 ];  then
     echo  "user$i exist"
   else
     useradd  user$i 2>  /dev/null
     [ $? - ne  0 ] &&  echo  "UserName is not legal"  &&  continue
     echo  "user${i}"  passwd  --stdin user${i} &>  /dev/null
     [ $? - ne  0 ] &&  echo  "Password is not legal"
     echo  "Add user user$i finished"
   fi
done
3、命令引用
#!/bin/bash
# Version: 0.0.4
# Author: Lcc.org
# Description:  $(seq 10)表示列表
 
[ ! $UID - eq  0 ] &&  echo  "Only root."   &&  exit  1
 
for  in  $( seq  10)
do
   id  user$i &>  /dev/null
   if  [ $? - eq  0 ];  then
     echo  "user$i exist"
   else
     useradd  user$i 2>  /dev/null
     [ $? - ne  0 ] &&  echo  "UserName is not legal"  &&  continue
     echo  "user${i}"  passwd  --stdin user${i} &>  /dev/null
     [ $? - ne  0 ] &&  echo  "Password is not legal"
     echo  "Add user user$i finished"
   fi
done
4、特殊变量
#!/bin/bash
# Version: 0.0.5
# Author: Lcc.org
# Description: $*,$@表示列表
 
[ ! $UID - eq  0 ] &&  echo  "Only root."   &&  exit  1
 
for  in  $*
do
   id  $i &>  /dev/null
   if  [ $? - eq  0 ];  then
     echo  "$i exist"
   else
     useradd  $i 2>  /dev/null
     [ $? - ne  0 ] &&  echo  "UserName is not legal"  &&  continue
     echo  "${i}"  passwd  --stdin ${i} &>  /dev/null
     [ $? - ne  0 ] &&  echo  "Password is not legal"
     echo  "Add user $i finished"
   fi
done



示例三:判断/var/目录下每个文件的类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
方法一
#!/bin/bash
# Version: 0.0.6
# Author: Lcc.org
# Description: file type
 
for  in  /var/ *;  do
   if  [ -f $i ];  then
       echo  "Common file."
   elif  [ -L $i ];  then
       echo  "Symbolic file."
   elif  [ -d $i ];  then
       echo  "Directory."
   else
     echo  "Other type"
   fi
done
 
方法二:
#!/bin/bash
# Version: 0.0.7
# Author: Lcc.org
# Description: Galaxy 
 
cd  /var
for  in  $( ls  /var );  do
   if  [ -f $i ];  then
     echo  "Common file."
   elif  [ -L $i ];  then
     echo  "Symbolic file."
   elif  [ -d $i ];  then
     echo  "Directory."
   else
     echo  "Other type"
   fi
done
 
方法三:
#!/bin/bash
# Version: 0.0.8
# Author: Lcc.org
# Description: Add DIR
 
for  in  $( ls  /var );  do
     if  [ -f  /var/ $i ];  then
         echo  "Common file"
     elif  [ -L  /var/ $i ];  then
         echo  "Symbolic file"
     elif  [ -d  /var/ $i ];  then
         echo  "Directory"
     else
         echo  "Other type"
     fi
done


示例四:tcp协议下处于ESTABLISH状态的有多少个,LiSTEN有多少个,有几种状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
方法一:
#!/bin/bash
# Version: 0.0.9
# Author: Lcc.org
# Description: statusTCP
 
declare  -i listen=0
declare  -i established=0
declare  -i other=0
 
for  in  $( netstat  -tan |  grep  '^tcp\>'  tr  -s  ' '  cut  -d ' '  -f6);  do
     if  "$i"  ==  "LISTEN"  ];  then
         let  listen++
     elif  "$i"  ==  "ESTABLISHED"  ];  then
         let  established++
     else
         let  other++
     fi
done
 
echo  -e  "LISTEN statu: $listen\nESTABLISHED statu: $established\nOther status: $other\nTotal type: $(netstat -tan | grep '^tcp\>' | tr -s ' ' | cut -d' ' -f6 | sort -u | wc -l)"
方法二:
# netstat -tan | grep '^tcp\>' | awk -v FS=' ' '{ARRAY[$NF]++}END{for(i in ARRAY){print i,ARRAY[i]}}'
LISTEN 12
CLOSE_WAIT 1
ESTABLISHED 3









本文转自 lccnx 51CTO博客,原文链接:http://blog.51cto.com/sonlich/1955978,如需转载请自行联系原作者
目录
相关文章
|
8天前
|
Oracle Java 关系型数据库
Linux下JDK环境的配置及 bash: /usr/local/java/bin/java: cannot execute binary file: exec format error问题的解决
如果遇到"exec format error"问题,文章建议先检查Linux操作系统是32位还是64位,并确保安装了与系统匹配的JDK版本。如果系统是64位的,但出现了错误,可能是因为下载了错误的JDK版本。文章提供了一个链接,指向Oracle官网上的JDK 17 Linux版本下载页面,并附有截图说明。
Linux下JDK环境的配置及 bash: /usr/local/java/bin/java: cannot execute binary file: exec format error问题的解决
|
2月前
|
Unix Shell Linux
在Linux中,什么是 BASH?
在Linux中,什么是 BASH?
|
3月前
|
存储 Shell Linux
Linux|创建和使用 Bash 别名
Linux|创建和使用 Bash 别名
43 6
|
2月前
|
Shell Linux
在Linux中,哪⼀个bash内置命令能够进行数学运算?
在Linux中,哪⼀个bash内置命令能够进行数学运算?
|
2月前
|
缓存 Shell Linux
在Linux中,bash shell 中的 hash 命令有什么作用?
在Linux中,bash shell 中的 hash 命令有什么作用?
|
2月前
|
人工智能 物联网 Shell
在Linux中,BASH 和 DOS之间的区别是什么?
在Linux中,BASH 和 DOS之间的区别是什么?
|
2月前
|
Unix Shell Linux
在Linux中,什么是Bash脚本,并且如何使用它。
在Linux中,什么是Bash脚本,并且如何使用它。
|
2月前
|
Shell Linux
在Linux中,使用bash shell实现条件判断和循环结构的例子是什么样的?
在Linux中,使用bash shell实现条件判断和循环结构的例子是什么样的?
|
2月前
|
Ubuntu 搜索推荐 Shell
如何在 Linux VPS 上自定义你的 Bash 提示符
如何在 Linux VPS 上自定义你的 Bash 提示符
13 0
|
4月前
|
机器学习/深度学习 Unix Java
技术笔记:Linux之Shell脚本编程(一)
技术笔记:Linux之Shell脚本编程(一)
47 0
下一篇
无影云桌面