美文网首页
【linux】shall 脚本

【linux】shall 脚本

作者: 鸦言 | 来源:发表于2020-07-15 19:40 被阅读0次

shall,是一些命令的集合

约定:凡是自定义的脚本建议放到/usr/local/sbin/目录下,这样做的目的是,一来可以更好的管理文档;二来以后接管你的管理员都知道自定义脚本放在哪里,方便维护。

例子1:test.sh (cat输出内容为vim输入内容)

vim test.sh
cat test.sh

#! /bin/bash
## This is my first shell script.
## Writen by Shen 2020-07-21.
date
echo "Hello World."

sh test.sh

Sat Jul 11 12:59:47 CST 2020
Hello World.

Shell脚本通常都是以.sh 为后缀名的,test.sh中第一行一定是 “#! /bin/bash”它代表的意思是,该文件使用的是bash语法。如果不设置该行,那么你的shell脚本就不能被执行。’#’表示注释,在前面讲过的。后面跟一些该脚本的相关注释内容以及作者和创建日期或者版本等等。当然这些注释并非必须的,如果你懒的很,可以省略掉,但是笔者不建议省略。因为随着你工作时间的增加,你写的shell脚本也会越来越多,如果有一天你回头查看你写的某个脚本时,很有可能忘记该脚本是用来干什么的以及什么时候写的。所以写上注释是有必要的。另外系统管理员并非你一个,如果是其他管理员查看你的脚本,他看不懂岂不是很郁闷。该脚本再往下面则为要运行的命令了。

例子2:默认vim文档无执行权限,可以添加执行权限,"./filename" 执行脚本。

chmod +x test.sh

./test.sh

Sat Jul 11 13:07:52 CST 2020
Hello World.

使用 sh + x + *.sh 可以查看脚本的执行过程,方便调试。

sh -x test.sh

+ date
Sat Jul 11 14:55:25 CST 2020
+ echo Hello World.
Hello World.

date 用于获得系统时间,%Y表示年,%m表示月,%d表示日期,%H表示小时,%M表示分钟,%S表示秒(区分大小写)

date "+%Y%m%d %H%M%S"

20200711 150121

date -d 可以打印 n 天/月/年 前或者 n 天/月/年 后的日期。

date -d "-1 day" "+%Y%m%d"

20200710

date + %w 打印星期几

date +%w

6

shell 脚本中的变量

变量来代替长且出现频率高的命令或路径。赋予变量时用的不是引号而是 "`"。

vim test2.xh

cat test2.sh 

#! /bin/bash
# In this script we will use variables.
# Writen by Shen.
d=`date +%H:%M:%S`
echo "the script begin at $d"
echo "now we will sleep 2 seconds."
sleep 2
d=`date +%H:%M:%S`
echo "the script end at $d"

sh test2.sh 

the script begin at 15:34:20
now we will sleep 2 seconds.
the script end at 15:34:22

这里(下面)有个有趣的发现

使用 shell 计算两个数的和

cat test3.sh 

#! /bin/bash
a=1
b=2
sum=$[$a+$b]
echo "sum is $sum"

sh test3.sh 

sum is $[1+2]

bash test3.sh 

sum is 3

sh 输出结果为 $[1+2],而 bash 输出结果为 3.

原因是Ubuntu系统 sh 指向的是 dash shell(一个小巧的shell,功能没有 bash 强大),而在 centOS 上,sh 指向的是 bash shell。

shall 脚本与用户互动

read 命令,read x 表示变量 x 的值需要用户通过键盘输入得到。

例子:

cat test.sh 

#! /bin/bash
echo "please input a number:"
read x
echo "please input anather number:"
read y
sum=$[$x+$y]
echo "the sum of two number is: $sum."

bash test.sh 

please input a number:
4
please input anather number:
6
the sum of two number is: 10.

上述步骤更简练的实现方法:
echo 和 read,整合为 read -p

cat test2.sh 

#! /bin/bash
read -p"please input a number:" x
read -p"please input another number:" y
sum=$[$x+$y]
echo "the sum of two number is: $sum."

bash test2.sh 

please input a number:9
please input another number:4
the sum of two number is: 13.

shell 脚本的预设变量

例子:此处 $0 表示脚本的名字,$1 表示第一个输入变量 ...

cat test3.sh 

#! /bin.bash
echo "$0 $1 $2"

bash test3.sh 12

test3.sh 12 

bash test3.sh 12 36

test3.sh 12 36

shell 脚本中的逻辑判断

例子: if 语句

具体格式:

if 判断语句一 ; then
command
elif 判断语句二; then
command
else
command
fi

cat iftest.sh 

#! /bin/bash
read -p "please input your score:" a
if ((a<60)) ; then
    echo "you didn't pass the exam."
else
    echo "Good!, you passed the exam."
fi

bash iftest.sh 

please input your score:45
you didn't pass the exam.

同样可以使用 elif 语句,&& 表示 并且 ,|| 表示 或者。

在判断数值大小除了可以用”(( ))”的形式外,还可以使用”[ ]”。但是就不能使用>, < , = 这样的符号了,要使用 -lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)。

shell 脚本判断档案属性

-e :判断文件或目录是否存在
-d :判断是不是目录,并是否存在
-f :判断是否是普通文件,并存在
-r :判断文档是否有读权限
-w :判断是否有写权限
-x :判断是否可执行

例子:

if [ -f test2.sh ] ; then echo ok; fi

ok

if [ -d /home ] ; then echo ok; fi

ok

if [ -f test5.sh ] ; then echo ok; fi

例子:case 语句

具体格式为:

case 变量 in
value1)
command
;;
value2)
command
;;
value3)
command
;;
*)
command
;;
esac

上面的结构中,不限制value的个数,*则代表除了上面的value外的其他值。下面笔者写一个判断输入数值是奇数
或者偶数的脚本。

cat cash.sh 

#! /bin/bash
read -p "please input a number:" n
a=$[$n%2]
case $a in

1)
    echo "the number is odd."
    ;;
0)
    echo "the number is even."
    ;;
esac

bash cash.sh 

please input a number:56
the number is even.

bash cash.sh 

please input a number:23
the number is odd.

shell 脚本中的循环

例子:for 循环

基本机构:
for 变量名 in 循环的条件; do
command
done

cat for.sh 

#! /bin/bash
for i in `seq 1 5`; do
    echo $i
done

bash for.sh 

1
2
3
4
5

例子:while 循环

基本格式为:

while 条件; do
command
done

cat while.sh 

#! /bin/bash
a=10
while (( $a > 3 )); do
    echo "$a"
    a=$[$a-1]
done

bash while.sh 

10
9
8
7
6
5
4

shall 脚本中的函数

shell 脚本中,函数一定要写在最前面,防止出现还没出现就被调用的错误

在shell脚本中要用:

function 函数名() {
command
}

这样的格式去定义函数。

例子:sum()

cat fun.sh 

#! /bin/bash
function sum(){
    sum=$[$1+$2]
    echo $sum
}
sum $1 $2

bash fun.sh 5 6

11

相关文章

网友评论

      本文标题:【linux】shall 脚本

      本文链接:https://www.haomeiwen.com/subject/lfxicktx.html