《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脚本基础

1、test  //检测文件及相关属性

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脚本调试。


相关文章
|
1天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
|
2天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
10 1
|
2天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
11 1
|
3天前
|
Shell Linux
【Linux】进程实践项目(更新中) — 自主shell编写
前几篇文章,我们学习进程的相关知识:进程概念,进程替换,进程控制。熟悉了进程到底是个什么事情,接下来我们来做一个实践,来运用我们所学的相关知识。这个项目就是手搓一个shell模块,模拟实现Xshell中的命令行输入。
10 1
|
3天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
3天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
3天前
|
Shell Linux 信息无障碍
5 个有用的 Linux Shell 转义序列
5 个有用的 Linux Shell 转义序列
|
5天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
23 5
|
5天前
|
Shell 程序员 数据安全/隐私保护
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
|
5天前
|
存储 Shell C语言
shell脚本 编程 变量 基本入门(详解)
shell脚本 编程 变量 基本入门(详解)