现在的位置: 首页 > 综合 > 正文

《Linux Shell脚本攻略》 笔记 之预备知识

2016年02月22日 ⁄ 综合 ⁄ 共 3584字 ⁄ 字号 评论关闭
《Linux Shell脚本攻略》笔记 之预备知识

【题记】:正如2014年年总总结写的一样,2014年看了很多书,但没有总结,没有很好的消化吸收。尤其对于Shell脚本,没有很好的实践与积累,都是遇到再翻书,效率极低。所以2015年元旦放假3天,发奋图强一把,实践敲了一遍脚本并整理出《linux Shell脚本攻略》的笔记。2015当以此为标杆,反刍其他技术书中以及平时遇到问题的知识点,消化吸收之

一、BASH基础篇
1.type命令 区分内置或者外置命令
2.两种脚本给变量命名的方法:
var1=`uname -r`
var2=$(uname -r)
3.$? 记录回传的错误码;
=0代表成功;
=非0代表失败。非0的退出状态

4.cut 切割功能
eg: last | cut -d ' ' -f 7 //-d代表分割字符; -f取第几段; 
eg: export | cut -c 1-20 //-c以字符为单位取出固定字符区间;

5.sort,uniq
eg: last | cut -d ' ' -f 1 | sort | uniq -c  //-c统计次数

6.wc
wc -l    //统计行数
wc -w  //统计字数(单词数)
wc -m  //统计字符数(字母个数)
//eg1:
[root@localhost program_test]# cat banana.txt 

banana

apple1  apple2

ORANGE

root@localhost program_test]# cat banana.txt | wc
      3       4      29
//eg2:
[root@localhost program_test]# cat 香蕉.txt
香蕉

苹果1 苹果2

橘子

[root@localhost program_test]# cat 香蕉.txt | wc

      3       4      30
7. cat hello.c | tr -s "  " "+" //tr替换
cat hello.c | tr -d " "  //tr删除

8.join 
//eg:
[root@localhost program_test]# cat right.txt

1 yang man

2 guo man

3 zhang woman

[root@localhost program_test]# cat left.txt


yang 100 95 74

zhang 100 100 100

[root@localhost program_test]# join -t " " -1 2 right.txt -2 1 left.txt

join: file 1 is not in sorted order

yang 1 man 100 95 74

zhang 3 woman 100 100 100

二、正则表达式基础
1、grep 用法
[root@localhost program_test]# grep -n '^[a-z]' left.txt    //以字母开头

1:yang 100 95 74

2:zhang 100 100 100

4:piao nen hui

5:si mi da 500 2590

[root@localhost program_test]# grep -n '^[^a-z]' left.txt  //不以字母开头

3:300 400 500

2、grep -n '^$' left.txt      //空白行 -n加行号
3、.* 零个或多个任意字符
4、3到5个0的串 搜索
[root@localhost program_test]# grep -n '.0\{3,5\}' left.txt 
6:2000

7:30000

8:400000

9:500000

10:6000000

11:70000000

12:800000000
13:9000000000

5、Sed管道命令,功能:替换、删除、新增、选取特定行。
1)选取特定行
[root@localhost program_test]# nl left.txt | sed -n '5,7p'

     4  100

     5  2000

     6  30000

[root@localhost program_test]# nl left.txt

     1  yang 100 95 74

     2  zhang 100 100 100

     3  300 400 500

      

     4  100

     5  2000

     6  30000

     7  400000
2)删除特定行
nl left.txt | sed '2,5d'
3)特定行新增
nl left.txt | sed '5a profyang'
4)删除特定字符
[root@localhost program_test]# ifconfig eth0 | grep 'inet addr'

          inet addr:192.168.119.128  Bcast:192.168.119.255  Mask:255.255.255.0

[root@localhost program_test]# ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'

192.168.119.128  Bcast:192.168.119.255  Mask:255.255.255.0

5)sed -i //直接修改文件内容,直接全局修改文件,所包含的项会全部修改!
[root@localhost program_test]# sed -i 's/300/yang300zhang/g' left.txt 

6、awk以行为一次的处理单位
eg:
cat /etc/passwd | awk 'BEGIN {FS=":"} $3 > 20  {print $1 "\t\t\t" $3}'

三、Shell脚本基础
2.、$#:代表后接的参数 "个数"
     $@:代表每个独立的变量
[root@localhost program_test]# nl parm.sh

     1  #!/bin/bash       

     2  echo "The script name is : $0"      

     3  echo "Total parm is : $#"       

     4  echo "Whole parm are: $@"      

     5  echo "first parm is : $1"

     6  echo "second parm is : $2"

     7  echo "third parm is : $3"

[root@localhost program_test]# ./parm.sh parm1 parm2 parm3

The script name is : ./parm.sh

Total parm is : 3

Whole parm are: parm1 parm2 parm3

first parm is : parm1

second parm is : parm2

third parm is : parm3

3、if语句的语法格式
if [ 条件表达式 1 ]; then
elif [ 条件表达式2 ]; then
else
fi

4、case语句的语法规则
case $变量名称 in
"first value")
program1
;;
"second value")
program2
;;
*)
exit 1
;;
esac

5、shell脚本的执行是从上到下,从左到右的,因此shell script的function设置一定要放到程序的最前面。

6、while语句的语法规则
while [ condition ]
do
prog1
done

until [ condition ]
do 
prog2
done

eg:
[root@localhost program_test]# cat sum.sh
#!/bin/bash

i=0;
sum=0;

while [ "$i" -ne  "100" ]
do
sum=$(($sum+$i))
i=$(($i+1))
done

echo "1+2+3+99 = $sum"

7、for语句的语法规则1
eg:
[root@localhost program_test]# cat sum_for.sh
#!/bin/bash

i=0;
sum=0;

for((i=0; i<100; i=i+1))
do
sum=$(($sum+$i))
done

echo "1+2+3+99 = $sum"
for语句的语法规则2
[root@localhost program_test]# cat users_recurse.sh

users=$(cat /etc/passwd | cut -d ":" -f1)

for user in $users

do

   id $user;

done

8. until语句的语法规则
[root@localhost program_test]# cat until.sh
#!/bin/bash
x=0
until [ $x -eq 9 ]; do
let x=x+1
echo $x
done


9. sh-x进行shell脚本调试。

作者:铭毅天下

转载请标明出处,原文地址:http://blog.csdn.net/laoyang360/article/details/42365303

如果感觉本文对您有帮助,请点击支持一下,您的支持是我坚持写作最大的动力,谢谢!

抱歉!评论已关闭.