Linux Shell 经典实例(1-30)

这篇博客详细介绍了Linux Shell脚本的各种实用技巧和案例,包括参数传递、文件操作、循环控制、字符串处理、函数应用、系统监控、邮件通知等,旨在帮助读者提升Shell编程能力,实现自动化任务。
摘要由CSDN通过智能技术生成

目  录

1、如何向脚本传递参数

2、如何在脚本中使用参数

3、获得脚本文件名

4、获得文件最后一行

5、获得文件第一行

6、如何获得每行第三个参数

7、for循环遍历目录

8、while循环

9、until循环

10、拼接字符串

11、函数相加

12、判断某个文件存在

13、获得文本的第10行 

14、 管道与bc预算

15、检查命令是否执行成功,0代表成功,非0都是执行失败。

16、传参检查

17、定期备份日志

18、一键安装LAMP

19、监听内存及磁盘容量发告警邮件

20、发邮件

21、猜数字游戏

22、校验权限自动部署vsftp

23、添加用户

24、输入三个数并进行升序排序

25、剪刀石头布

26、主机状态扫描 

27、九九表

28、监控网卡ens160手法数据 

29、批量添加用户(默认密码)

30、批量添加用户(带密码)


shell编程就是对一堆Linux命令的逻辑化处理。

就好比:

       excel中的vba

       window中的批处理

       word中的宏

总之就是将机械重复的操作步骤,打包成一个自动运行的脚本,不用每次都人工操作了。

1、如何向脚本传递参数


[wulei@obj02 ~]$ sudo sh ./show1.sh "aa"
aa
[wulei@obj02 ~]$ sudo sh ./show0.sh "aa" 
./show0.sh


[wulei@obj02 ~]$ sudo sh ./show0.sh 1.txt
./show0.sh
[wulei@obj02 ~]$ sudo sh ./show1.sh 1.txt 
1.txt
[wulei@obj02 ~]$ 


#源码展示
[wulei@obj02 ~]$ cat show0.sh 
#!/bin/bash
echo $0

[wulei@obj02 ~]$ cat show1.sh 
#!/bin/bash
echo $1

[wulei@obj02 ~]$ cat 1.txt 
I'm 1.txt
[wulei@obj02 ~]$ 

2、如何在脚本中使用参数

第一个参数 : $1,第二个参数 : $2,其中$0就是脚本本身


# 执行
[wulei@obj02 ~]$ sudo sh copy.sh 1.txt /tmp/

# 检查
[wulei@obj02 ~]$ ll /tmp/ |grep 1.txt   
-rw-r--r--. 1 root  root       10 Apr 11 00:36 1.txt


# 源码
[wulei@obj02 ~]$ cat copy.sh 
#!/bin/bash
cp $1 $2
[wulei@obj02 ~]$ 

3、获得脚本文件名

使用$0就可以获得脚本文件名

[root@obj02 wulei]# ./selfname.sh 
./selfname.sh
[root@obj02 wulei]# sh selfname.sh 
selfname.sh

#源码
[root@obj02 wulei]# cat selfname.sh 
#!/bin/bash
echo $0

4、获得文件最后一行

[wulei@obj02 ~]$ tail -1 1.txt 
the end

[wulei@obj02 ~]$ cat 1.txt 
I'm 1.txt
the end

5、获得文件第一行

[wulei@obj02 ~]$ head -1 1.txt 
I'm 1.txt
[wulei@obj02 ~]$ cat 1.txt 
I'm 1.txt
the end

6、如何获得每行第三个参数

[wulei@obj02 ~]$ cat /etc/passwd |awk -F ":" '{print $3}'
0
1
2
3
4
5
6
7
8
11
12
14
99
......

7、for循环遍历目录

[wulei@obj02 ~]$ ll
total 28
-rw-rw-r--. 1 wulei wulei 2437 Apr 11 01:00 1.txt
-rw-r--r--. 1 root  root    15 Apr 11 00:48 argsnum.sh
-rw-rw-r--. 1 wulei wulei   21 Apr 11 00:35 copy.sh
drwxr-xr-x. 2 wulei wulei    6 Mar 27 04:33 Desktop
drwxr-xr-x. 2 wulei wulei    6 Mar 27 04:33 Documents
drwxr-xr-x. 2 wulei wulei    6 Mar 27 04:33 Downloads
drwxr-xr-x. 2 wulei wulei    6 Mar 27 04:33 Music
drwxrwxr-x. 2 wulei wulei   26 Mar 30 22:44 notes
drwxr-xr-x. 2 wulei wulei    6 Mar 27 04:33 Pictures
drwxr-xr-x. 2 wulei wulei    6 Mar 27 04:33 Public
-rwxrwxr-x. 1 wulei wulei   20 Apr 11 00:43 selfname.sh
-rw-rw-r--. 1 wulei wulei   20 Apr 11 00:27 show0.sh
-rw-rw-r--. 1 wulei wulei   20 Apr 11 00:27 show1.sh
drwxr-xr-x. 2 wulei wulei    6 Mar 27 04:33 Templates
-rw-rw-r--. 1 wulei wulei   33 Apr 11 00:54 th3perlin.sh
drwxr-xr-x. 2 wulei wulei    6 Mar 27 04:33 Videos
drwxr-xr-x. 3 wulei wulei  182 Mar 30 19:37 vmtools

[wulei@obj02 ~]$ for i in $(ls);do echo item:  $i; done;
item: 1.txt
item: argsnum.sh
item: copy.sh
item: Desktop
item: Documents
item: Downloads
item: Music
item: notes
item: Pictures
item: Public
item: selfname.sh
item: show0.sh
item: show1.sh
item: Templates
item: th3perlin.sh
item: Videos
item: vmtools
[wulei@obj02 ~]$ 

8、while循环

[wulei@obj02 ~]$ sudo sh whiletest.sh 
The counter is 0
The counter is 1
The counter is 2
The counter is 3
The counter is 4
The counter is 5
The counter is 6
The counter is 7
The counter is 8
The counter is 9

[wulei@obj02 ~]$ cat whiletest.sh 
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done

9、until循环

[wulei@obj02 ~]$ sudo sh untiltest.sh 
COUNTER 20
COUNTER 19
COUNTER 18
COUNTER 17
COUNTER 16
COUNTER 15
COUNTER 14
COUNTER 13
COUNTER 12
COUNTER 11
COUNTER 10

[wulei@obj02 ~]$ cat untiltest.sh     
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
[wulei@obj02 ~]$ 

10、拼接字符串

[wulei@obj02 ~]$ v1="hello";v2="world";v3=${v1}${v2};echo $v3
helloworld

11、函数相加

[wulei@obj02 ~]$ v1=1;v2=2;let v3=v1+v2;echo $v3
3

[wulei@obj02 ~]$ a=5;b=6;echo $(($a+$b))
11

[wulei@obj02 ~]$ a=5;b=6;echo $[$a+$b]
11

[wulei@obj02 ~]$ a=5;b=6;expr $a + $b
11

[wulei@obj02 ~]$ a=5;b=6;echo $a + $b|bc
11

[wulei@obj02 ~]$ a=5;b=6;awk 'BEGIN{print '"$a"'+'"$b"'}'
11

12、判断某个文件存在

[wulei@obj02 ~]$ ls
1.txt       Desktop    filedetect.sh  notes     selfname.sh  Templates     Videos
argsnum.sh  Documents  funcdemo.sh    Pictures  show0.sh     th3perlin.sh  vmtools
copy.sh     Downloads  Music          Public    show1.sh     untiltest.sh  whiletest.sh


#执行
[wulei@obj02 ~]$ sudo sh filedetect.sh 1.txt 
1.txt File exists
[wulei@obj02 ~]$ sudo sh filedetect.sh 2.txt 
2.txt File not exists


#源码
[wulei@obj02 ~]$ cat filedetect.sh 
#!/bin/bash
if [ -f $1 ]
then
echo "$1 File exists"
else
echo "$1 File not exists"
fi


13、获得文本的第10行 

#获得文本前10行
[wulei@obj02 ~]$ head -10 1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

#管道重定向给tail取最后一行
[wulei@obj02 ~]$ 
[wulei@obj02 ~]$ 
[wulei@obj02 ~]$ 
[wulei@obj02 ~]$ head -10 1.txt |tail -1
operator:x:11:0:operator:/root:/sbin/nologin

14、 管道与bc预算

[wule
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值