shell脚本

shell脚本:

脚本,并不是正式的编程语言,只能在liunx中运行。他是一些命令的集合。脚本可以帮助我们方便地管理服务器、我们可以指定一个任务计划,定时去执行某个脚本。例如: 我们可以使139邮箱在发送时在发送短信给用户。

在编辑脚本的时候建议把文件放在 /ust/local/sbin/ 目录下。饭方便查找。贾创建的脚本尽量用 ,sh 结尾。方便与其他文件进行区分。

创建最简单的脚本。

[root@elk ~]04:36:35 # cd /usr/local/sbin/
[root@elk sbin]04:43:29 # vi first.sh
[root@elk sbin]05:16:50 # cat first.sh 
#!/bin/bash
date
echo "Hello word"

执行脚本的两种方法:

[root@elk sbin]17:02:47 # sh first.sh 
2021年 04月 26日 星期一 17:03:39 CST
Hello word
[root@elk sbin]04:44:55 # chmod +x first.sh
[root@elk sbin]04:45:26 # ./first.sh 
2021年 04月 26日 星期一 04:45:34 CST
Hello word

显示脚本的执行过程。

[root@elk sbin]04:45:34 # sh -x first.sh 
+ date
2021年 04月 26日 星期一 04:46:45 CST
+ echo 'Hello word'
Hello word

date 命令常用的几种用法。

date +%Y 表示四位数字格式打印年份。
date +%y 表示两位数字格式打印年份 。
date +%m 表示月份。
date +%d 表示日期。
date +%H 表示小时。
date +%M 表示分钟。
date +%S 表示秒。
date +%w 表示星期。结果显示0 这是表示周日。

查看年月和时间:

[root@elk sbin]04:49:13 # date +"%Y-%m-%d %H:%M:%S"
2021-04-26 04:49:24

一天前的日期 :

[root@elk sbin]04:51:37 # date -d "-1 hour " +%d
26

一个小时的:

[root@elk sbin]04:52:17 # date -d "-1 hour " +%H
03

一分钟的 :

[root@elk sbin]05:00:58 # date -d "-1 min " +%M
15

shell 脚本中的变量
-早教本周内
变量可以快速地更改不想要的路径可以使脚本不臃肿。格式 : 变量名 = 变量的值 在脚本中引用变量需要$

[root@elk sbin]18:34:26 # cat variable.sh 
#! /bin/bash

## In this script we will use variables.
## Wruteb bu Aming 2021-4-26

d= `date +%H:%M:%S`
echo "The script begin at $d."
echo "Now we'll sleep 2 seconds."
sleep 2 
d1= date +%H:%M:%S
echo "The script end at $d1."
[root@elk sbin]16:31:23 # sh  variable.sh 
The script begin at 16:31:24.
Now we'll sleep 2 seconds.
The script end at 16:31:26.

数学运算:(数学计算要用 拼 [ ] 括起来,且前面要加$ )

[root@elk sbin]18:43:51 # cat sum.sh 
#! /bin/bash
## Fro get the sum of two numbers.
## Aming 2021-4-26

a=1
b=2
sum=$[$a+$b]
echo "$a+$b=$sum"
[root@elk sbin]18:43:39 # sh sum.sh 
1+2=3

和用户交互(read 命令用于和用户交互他把用户输入的字符串作为变量值)

[root@elk sbin]19:02:41 # cat read.sh 
#! /bin/bash
## Using  'read' in shell script.
## Aming 2020-4-26

read -p "Please input a number :" x
read -p "Please input another number :" y
sum=$[$x + $y]
[root@elk sbin]19:02:27 # sh read.sh 
Please input a number :3
Please input another number :4
The sum of the two numbers is :7

shell 脚本预设变量

[root@elk sbin]00:51:40 # cat option.sh 
#! /bin/bash
sum=$[$1+$2]
echo "sum=$sum"
[root@elk sbin]00:51:36 # sh -x option.sh 1 2
+ sum=3
+ echo sum=3
sum=3

1 2 是脚本预设,脚本预设是没有限制的。-$0 表示脚本名

[root@elk sbin]00:46:53 # cat option.sh 
#! /bin/bash

echo "$1 $2 $0"
[root@elk sbin]00:46:44 # sh -x option.sh 1 2
+ echo '1 2 option.sh'
1 2 option.sh

shell 的逻辑判断 (不带else)

格式
if ((判断语句)); then
command
fi

[root@elk sbin]01:10:25 # cat if1.sh 
#! /bin/bash
read -p "Please input your score: " a
if ((a<6)) ;then
	echo "You didn't pass ehr exam."
fi
[root@elk sbin]01:05:02 # sh if1.sh 
Please input your score: 4
You didn't pass ehr exam.
[root@elk sbin]01:05:15 # sh if1.sh 
Please input your score: 8

带有else的

if ((判断语句)) ;then
command
else
command
fi

[root@elk sbin]01:18:27 # cat if2.sh 
#! /bin/bash
read -p "Please input your score:" a
if ((a<6)); then
	echo "You didn't pass the exam."
else 
	echo "Good! You passed the exam."
if
[root@elk sbin]01:19:23 # sh if2.sh 
Please input your score:4
You didn't pass the exam.

[root@elk sbin]01:19:29 # sh if2.sh 
Please input your score:7
Good! You passed the exam.

带有 elif : (&& 并且 。|| 或者)

具体格式:
if ((判断语句1)); then
command
elif ((判断语句2)); then
command
else
command
fi

[root@elk sbin]04:32:40 # cat if3.sh 
#! /bin/bash

read -p "Please input your score :" a
if ((a<8));then
	echo "You didn't pass the exam."
elif((a>=8))&&((a<10));then 
	echo "Good! You pass the exam."
else 
	echo "Very good ! Your score is very high!"
fi
[root@elk sbin]04:32:25 # sh if3.sh 
Please input your score :4
You didn't pass the exam.
[root@elk sbin]04:32:28 # sh if3.sh 
Please input your score :9
Good! You pass the exam.
[root@elk sbin]04:32:34 # sh if3.sh 
Please input your score :11
Very good ! Your score is very high!

判断大小 除了(()) 。还可以是 [ ] .但是不能使用 > , < , = 这样的符号 。

要是用 -It (小于),

-gt (大于),

-le(小于或者等于),

-ge(大于或者等于),

-eq(等于),

-ne (不等于)。

[root@elk sbin]05:07:15 # cat if4.sh 
a=2;
if [ $a -lt 4 ]; then
	echo ok;
fi
[root@elk sbin]05:02:33 # sh if4.sh 
ok

[ ] 括号的内测必须加空格与变量分开。echo 后面不加 “ ”

和文档相关的判断,shell 中的if 还经常用于判断文档属性比如判断是普通文件还是目录。文件是否有读,写,执行权限。

-e 判断文件或者目录是否此存在。
-d 判断是不是目录以及是否存在。
-f 判断是不是普通文件以及是否存在。
-r 判断是否有读的权限,
-w 判断是否有写的权限了。
-x 判断是否可执行

[root@elk sbin]05:32:05 # cat if5.sh 
if [ -d /home/ ];then
	echo ok;
fi
if [ -f /home/ ];then
	echo yes;
fi
if [ -r /usr/local/sbin/if1.sh ]; then 
	echo r;
fi
[root@elk sbin]05:32:03 # sh if5.sh 
ok
r

注释:因为、home是目录不是文件所以不输出。

case逻辑判断

*格式:
case 变量 in
value1 )
command
;;
value2)
command
;;
)
command
;;
esac

[root@elk sbin]16:55:15 # cat case.sh 
#!/bin/bash

read -p "Input a number: " a
b=$[$a%2]
case $b in 
    1)
	echo "The number is odd."
	;;
    0) 
	echo "The number is even."
	;;
    *)
	echo "It's not a number!"
	;;
esac
[root@elk sbin]16:37:16 # sh case.sh 
Input a number: 4
The number is even.
[root@elk sbin]16:55:10 # sh case.sh 
Input a number: 3
The number is odd.

shell 中的循环

for 循环
格式:
for 变量名 in 循环条件 :do
command
done

[root@elk sbin]17:12:41 # cat for.sh 
#! /bin/bash 

for i in `seq 1 5`; 
   do
	echo $i
   done
[root@elk sbin]17:12:40 # sh for.sh 
1
2
3
4
5

循环条件的其他用法。

方法一。条件的命令

[root@elk sbin]17:34:02 # cat for2.sh 
#! /bin/bash

for i in `ls`; 
   do
	echo $i; 
   done

[root@elk sbin]17:33:57 # sh for2.sh 
case.sh
first.sh
for2.sh
for.sh
if1.sh
if2.sh
if3.sh

方法二 , 字符串或数字用空格隔开。

[root@elk sbin]17:40:18 # cat for3.sh 
#! /bin/bash/

for i in t e w a 1 3 ;
  do
	echo $i ;
  done
[root@elk sbin]17:40:04 # sh for3.sh 
t
e
w
a
1
3

while 循环

格式:
while 条件; do
command
done

[root@elk sbin]18:04:39 # cat while1.sh 
#! /bin/bash

a=5 
while [ $a -ge 1 ];do
	echo $a
	a=$[$a-1]
done
[root@elk sbin]18:03:43 # sh while1.sh 
5
4
3
2
1

实现死循环

[root@elk sbin]18:04:51 # cat while2.sh 
#! /bin/bash

while :;do
	command
	echo "甜";
done
[root@elk sbin]18:07:09 # sh while2.sh
甜
甜
甜
^C                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

shell 脚本中的函数。

自定义函数格式:
function 函数名()
{
command1
command2
}

[root@elk sbin]18:21:44 # cat func.sh 
#! /bin/bash

function sum()
{
   sum=$[ $1+$2 ]
   echo $sum
}

sum $1 $2
[root@elk sbin]18:21:41 # sh func.sh 1 2
3

shell中的中断和继续

break 终止命令

[root@elk sbin]19:18:36 # cat bread.sh 
#! /bin/bash

for i in `seq 1 5`
do
	echo $i
	if [ $i == 3 ]
	then
		break
	fi
	echo $i
done
echo bbbb
[root@elk sbin]19:18:32 # sh bread.sh 
1
1
2
2
3
bbbb

continue 终止本次循环

[root@elk sbin]22:47:13 # cat continue.sh 
#! /bin/bash

for i in `seq 1 5`
do 
	echo $i
	if [ $i == 3 ]
	then
		continue
	fi
	echo $i
done
[root@elk sbin]22:46:59 # sh continue.sh 
1
1
2
2
3
4
4
5
5

exit 直接退出脚本

[root@elk sbin]23:03:52 # cat exit.sh 
#! /bin/bash

for i in `seq 1 5`
do 
	if [ $i == 3 ]
	then
		exit
	fi
	echo $i
done
[root@elk sbin]23:03:50 # sh exit.sh 
1
2
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值