linux 预备知识

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值