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

简介: 《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脚本调试。

作者:铭毅天下

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

相关文章
|
10天前
|
安全 Shell Linux
探索Linux命令chsh:更改用户的默认shell
`chsh`是Linux命令,用于更改用户的默认登录shell。它涉及用户环境配置和系统安全,允许用户选择更适合自己的shell以提升效率。命令有交互式选项和参数如`-s`来指定新shell。在使用时要注意新shell的可执行性、权限问题及选择合适的shell。例如,要更改为bash,用户可运行`chsh`后按提示操作,而root用户能用`sudo chsh -s /bin/zsh john`为用户`john`设定zsh。在更改前,确认shell路径、权限,并了解不同shell的特点。
|
16小时前
|
机器学习/深度学习 Unix Java
技术笔记:Linux之Shell脚本编程(一)
技术笔记:Linux之Shell脚本编程(一)
|
18小时前
|
网络协议 算法 Linux
技术笔记:Linux学习:TCP粘包问题
技术笔记:Linux学习:TCP粘包问题
|
18小时前
|
缓存 Linux 编译器
技术笔记:Linux程序包管理
技术笔记:Linux程序包管理
|
20小时前
|
Linux API 调度
技术笔记:Linux内核跟踪和性能分析
技术笔记:Linux内核跟踪和性能分析
|
21小时前
|
关系型数据库 MySQL Linux
技术笔记:Linux命令的返回值
技术笔记:Linux命令的返回值
|
22小时前
|
数据可视化 Shell Linux
技术笔记:Linux学习笔记1
技术笔记:Linux学习笔记1
|
1天前
|
Shell Linux
【linux】进程替换的应用|shell解释器的实现
【linux】进程替换的应用|shell解释器的实现
7 0
|
3天前
|
Shell
Shell脚本之条件语句if总结
Shell脚本之条件语句if总结
|
1月前
|
Shell 索引
shell脚本入门到实战(四)- 数组
shell脚本入门到实战(四)- 数组