目 录
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