一、脚本执行
scripts脚本执行时,bash会根据下面的规则判断执行的步骤:
1、如果读取到一个回车符号( CR ),就尝试开始执行该行命令;
2、如同前面 bash命令提到的,指令间的多个空白会被忽略掉;
3、空白行也将被忽略掉,并且tab也是不会被理会的;
4、至于如果一行的内容太多,则可以使用 \ 来延伸至下一行;
5、此外,使用最多的 # 可做为批注,任何加在 # 后面的字符,将全部被视为批注文字而被忽略。
二、在撰写脚本的好习惯
1、第一行宣告使用的 shell 为何?(如果不声明使用默认的shell解释,通常应该声明);
#!/bin/sh
#!/bin/bash
2、注明该 script 的内容功能、版本信息、作者、创建文件日期等;
3、每一个大步骤的主要功能(也顺便提供自己未来修改时使用)。
三、脚本执行的方法
1、一种是将该脚本文件改成可以执行的属性,如chmod 755 scripts.file ,然后执行该脚本文件;
2、另一种则是直接以 sh或bash 这个执行文件来执行 script 的内容,如:
sh scripts.file
bash scripts.file
四、卷标与运算符
declare 声明变量内容命令
语法: [test @ray]# declare [-afirx]
参数说明:
-a :定义为数组 array
-f :定义为函数 function
-i :定义为整数 integer
-r :定义为只读
-x :定义为通过环境输出变量 (export命令)
范例: [ray@ray]# declare -i a=3
[ray@ray]# declare -i b=5
[ray@ray]# declare -i c=$a*$b
[ray@ray]# echo $c
15
五、脚本逻辑判断表达式
如何判定某个文件或目录,或者是如何判定程序应该朝向那个方向行进?
1、关于文件与目录的检测
-f 常用。检测文件是否存在
-d 常用。检测目录是否存在
-L 检测是否为一个 symbolic link 的文件
-e 检测某个东西是否存在
2、关于文件的属性检测
-r 检测是否为可读的属性
-w 检测是否为可以写入的属性
-x 检测是否为可执行的属性
-s 检测是否为非空白文件
3、两个文件之间的判断与比较 ;例如 test file1 -nt file2
-nt 第一个文件比第二个文件新
-ot 第一个文件比第二个文件旧
-ef 第一个文件与第二个文件为同一个文件( link 之类的文件)
4、逻辑的 与(and) 或(or)
&& 逻辑的 AND 的意思
|| 逻辑的 OR 的意思
六、运算符号
七、条件判断语句
条件判断一:if then fi 的方式 :
if [ 条件判断一 ] && (||) [ 条件判断二 ]; then
执行内容程序
elif [ 条件判断三 ]
执行第二段内容程序
else
执行第三段内容程序
fi
示例:
[root@ray]# vi if_then_fi.sh
#!/bin/bash
echo “Press y to continue”
read yn
if [ “$yn” = “y” ]; then
echo “script is running...” ;
else
echo “stop!" ;
fi
条件判断二:使用 case ...esac 的方式
case 输入方式(string) in
输入方式一)
程序执行段 ;;
输入方式二)
程序执行段 ;; *)
echo “Usage: {输入方式一|输入方式二}"
exit 1
esac
输入方式(string)的格式主要有两种:
1、直接输入:就是以“执行文件 + 字符串 ”的方式来执行的, 字符串可以直接写成 $1 (在执行文件后面直接加入参数的第一个参数)
2、交互式:就是由屏幕输出可能的项目,然后让使用者输入,这个通常必须配合 read variable 然后 string 则写成 $variable的格式!
实例1:直接输入方式
[root@ray]# vi case_esac.sh
#!/bin/bash
echo "This program will print your selection!"
case $1 in
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "Usage {one|two|three}"
exit 1
esac
实例2:交互方式
[root@ray]# vi case_esac2.sh
#!/bin/bash
echo "Press your select one, two, three"
read number
case $number in
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "Usage {one|two|three}"
exit 1
esac
循环结构语句
①for (( 条件一; 条件二; 条件三 ))
②for variable in variable1 variable2 .....
③while [ condition1 ] && { || } [ condition2 ] ...
④until [ condition1 ] && { || } [ condition2 ] ...
[root@ray]# vi for_loop.sh
#!/bin/bash
# Using for and loop
declare -i s
for (( i=1; i<=100; i=i+1 ))
do
s=s+i
done
echo "The count is ==> $s"
[root@ray]# vi while_loop.sh
#!/bin/bash
# Using while and loop
declare -i i
declare -i s
while [ "$i" != "101" ]
do
s=s+i
i=i+1
done
echo "The count is ==> $s"
[root@ray]# vi until_loop.sh
#!/bin/bash
# Using until and loop
#
declare -i i
declare -i s
until [ "$i" = "101" ]
do
s=s+i
i=i+1
done
echo "The count is ==> $s"
[root@ray]# vi for_do_done_loop.sh
#!/bin/bash
# using for...do ....done
#
LIST="Tomy Jony Mary Geoge"
for i in $LIST
do
echo $i
done
[root@ray]# vi until_loop.sh
#!/bin/bash
# Using until
#
echo "Press Y/y to stop"
until [ "$yn" = "Y" ] || [ "$yn" = "y" ]
do
read yn
done
echo "Stop here"
综合示例:逻辑判断式
①先查看一下 /root/test/logical 这个名称是否存在;
②若不存在,则建立一个文件,使用 touch 来建立(建立的是0字节的一个文件),建立完成后离开;
③如果存在的话,判断该名称是否为文件,若为文件则将之删除后建立一个目录,目录名为 logical ,之后离开;
④如果存在的话,而且该名称为目录,则移除此目录!
⑤[test @test test]# vi logical.sh
•#!/bin/bash
if [ ! -e logical ]; then
touch logical
echo "Just make a file logical"
exit 1
elif [ -e logical ] && [ -f logical ]; then
rm logical
mkdir logical
echo "remove file ==> logical"
echo "and make directory logical"
exit 1
elif [ -e logical ] && [ -d logical ]; then
rm -rf logical
echo "remove directory ==> logical"
exit 1
else
echo "Does here have anything?"
fi
综合示例:显示主机上的端口是否开启
[test @test test]# vi port.sh
ssh=`netstat -an|grep LISTEN|grep :22`
if [ "$ssh" != "" ]; then
echo "SSH is running"
else
echo "SSH is NOT running"
fi