shell脚本的简单练习

来源:互联网 发布:java调用支付宝接口 编辑:程序博客网 时间:2024/05/29 13:37

[root@localhost shell]# cp /etc/passwd ./

[root@localhost shell]# head -1 passwd 
root:x:0:0:root:/root:/bin/bash
[root@localhost shell]# head -1 passwd  | awk -F: '{print $3,$6,$7}'
0 /root /bin/bash
[root@localhost shell]# head -1 passwd  | awk -F: '{print $0}'
root:x:0:0:root:/root:/bin/bash

[root@localhost shell]# head -1 passwd  | awk -F: '{print $3":"$6":"$7}'
0:/root:/bin/bash
[root@localhost shell]# head -1 passwd 
root:x:0:0:root:/root:/bin/bash
[root@localhost shell]# head -1 passwd | cut -d: -f2
x
[root@localhost shell]# head -1 passwd | cut -d: -f3,6,7
0:/root:/bin/bash
[root@localhost shell]# head -1 passwd | cut -d: -f3,6,7| tr ":" " "
0 /root /bin/bash
[root@localhost shell]# awk 'BEGIN{FS=":"}/^root/{print $1}' passwd 
root

[root@localhost shell]# awk -F: '/^root/{print $1}' passwd  
root


[root@localhost shell]# awk -F:  'BEGIN{print"********"}/^root/ {print $1,"a",$7} END {print "======"}' passwd
********
root a /bin/bash
======

[root@localhost shell]# awk -F: '{if ($7~"/bin/bash") print
 $0}' passwd 
root:x:0:0:root:/root:/bin/bash

[root@localhost shell]# awk -F: '{if ($7~"/bin/bash") print
 $1;else print $2}' passwd 
root
x
x


[root@localhost shell]# cat test.txt 
aa bb a b aaa aaaa bbbb  bb
ab ab abc bbb bbbb bb aa 
aa aaa aaa aaaa aaaaa bbbbb
bb bbab ab abbb abbab aa 
bbbb bbb bbb bbbbb bbbbb aab
aa bb
ab aabb

[root@localhost shell]# awk 'gsub("aa","bb"){print $0}' test.txt 
bb bb a b bba bbbb bbbb  bb
ab ab abc bbb bbbb bb bb 
bb bba bba bbbb bbbba bbbbb
bb bbab ab abbb abbab bb 
bbbb bbb bbb bbbbb bbbbb bbb
bb bb
ab bbbb

[root@localhost shell]# awk 'gsub("bb","cc")' test.txt 
aa cc a b aaa aaaa cccc  cc
ab ab abc ccb cccc cc aa 
aa aaa aaa aaaa aaaaa ccccb
cc ccab ab accb accab aa 
cccc ccb ccb ccccb ccccb aab
aa cc
ab aacc

[root@localhost shell]# awk 'gsub("\\ybb\\y","cc")' test.txt 
aa cc a b aaa aaaa bbbb  cc
ab ab abc bbb bbbb cc aa 
cc bbab ab abbb abbab aa 
aa cc

  awk  'BEGIN {
for(j=1;j<=12;j++)
{ flag=0;
  printf "\n%d月份\n",j;
        for(i=1;i<=31;i++)
        {
        if (j==2&&i>28) flag=1;
        if ((j==4||j==6||j==9||j==11)&&i>30) flag=1;
        if (flag==0) {printf "%02d%02d ",j,i}
        }
}
}'

1月份
0101 0102 0103 0104 0105 0106 0107 0108 0109 0110 0111 0112 0113 0114 0115 0116 0117 0118 0119 0120 0121 0122 0123 0124 0125 0126 0127 0128 0129 0130 0131 
2月份
0201 0202 0203 0204 0205 0206 0207 0208 0209 0210 0211 0212 0213 0214 0215 0216 0217 0218 0219 0220 0221 0222 0223 0224 0225 0226 0227 0228 
3月份

……………………………………………………

[root@localhost shell]# sed -n /root/p  passwd 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost shell]# 

[root@localhost shell]# awk '{print $0}' fstab             
LABEL=/                 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
LABEL=SWAP-sda2         swap                    swap    defaults        0 0
##

[root@localhost shell]# awk '{print $0}' fstab | sed '2,$'d
LABEL=/                 /                       ext3    defaults        1 1


[root@localhost shell]# cat test.txt 
aa bb a b aaa aaaa bbbb  bb
ab ab abc bbb bbbb bb aa 
aa aaa aaa aaaa aaaaa bbbbb
bb bbab ab abbb abbab aa 
bbbb bbb bbb bbbbb bbbbb aab
aa bb
ab aabb

[root@localhost shell]# sed -i s/aa/ABC/g test.txt 
[root@localhost shell]# cat test.txt 
ABC bb a b ABCa ABCABC bbbb  bb
ab ab abc bbb bbbb bb ABC 
ABC ABCa ABCa ABCABC ABCABCa bbbbb
bb bbab ab abbb abbab ABC 
bbbb bbb bbb bbbbb bbbbb ABCb
ABC bb
ab ABCbb

[root@localhost shell]# sed /aa/'a S' test.txt 
aa bb a b aaa aaaa bbbb  bb
S
ab ab abc bbb bbbb bb aa 
S
aa aaa aaa aaaa aaaaa bbbbb
S
bb bbab ab abbb abbab aa 
S
bbbb bbb bbb bbbbb bbbbb aab
S
aa bb
S
ab aabb
S


[root@localhost shell]# sed 3'a abc' test.txt   在第三行后面添加abc
aa bb a b aaa aaaa bbbb  bb
ab ab abc bbb bbbb bb aa 
aa aaa aaa aaaa aaaaa bbbbb
abc
bb bbab ab abbb abbab aa 
bbbb bbb bbb bbbbb bbbbb aab
aa bb
ab aabb


[root@localhost shell]# cat 3.sh




对比一下echo和EOF输出的格式。
















原创粉丝点击