原脚本文件
[root@localhost script]# cat test
1 2 50 90 1050 796 228
a b c d e f g h i j k l
good high tall full qualify happy cool
1、预先设置每一行的分割符号为两个换行
[root@localhost script]# awk 'BEGIN { ORS="/n/n" }; 1' test
1 2 50 90 1050 796 228/n/n/n/na b c d e f g h i j k l/n/n/n/ngood high tall full qualify happy cool/n/n
2、最后的字段加入一个换行符
[root@localhost script]# awk 'NF {print $0 "/n"}' test
1 2 50 90 1050 796 228/n
a b c d e f g h i j k l/n
good high tall full qualify happy cool/n
3、显示当前行,所在文件中的行号
[root@localhost script]# awk '{print FNR "/t" $0}' test
1/t1 2 50 90 1050 796 228
2/t
3/ta b c d e f g h i j k l
4/tgood high tall full qualify happy cool
4、当前行在每次处理文件的行号
[root@localhost script]# awk '{print NR "/t" $0}' test
1/t1 2 50 90 1050 796 228
2/t
3/ta b c d e f g h i j k l
4/tgood high tall full qualify happy cool
5、使用简单样式输出
[root@localhost script]# awk '{printf ("%5d : %s/n",NR,$0)}' test
1 : 1 2 50 90 1050 796 228/n 2 : /n 3 : a b c d e f g h i j k l/n 4 : good high tall full qualify happy cool/n #行号占用5位,不足补空格
6、显示行号
[root@localhost script]# awk '{print NR}' test
1
2
3
4
7、计算行数,效果类似wc -l
[root@localhost script]# awk 'END {print NR}' test
4
8、计算每一行的和
[root@localhost script]# awk '{s=0;for (i=1;i<=NF;i++)s=s+$i;print s}' test
2217
0
0
0
9、计算文件中所有字段的和
[root@localhost script]# awk '{for (i=1;i<NF;i++) s=s+$i};END {print s}' test
1989
10、将每个字段用其绝对值代替
[root@localhost script]# awk '{for (i=1;i<NF;i++) if ($i < 0) $i = -$i;print}' test
1 2 50 90 1050 796 228
a b c d e f g h i j k l
good high tall full qualify happy cool
11、计算文件中总的字段和(例如计算单词数)
[root@localhost script]# awk '{total = total + NF};END {print total}' test
26
12、计算匹配指定信息的总行数
[root@localhost script]# awk '/Linux/ {n++}; END {print n+0}' test
0
使用test1文件
[root@localhost script]# cat test1
1 23 566 90 189 290 6666 13569
27 89 289 367 4899 10999 209999 3000000
26 39 409 366 758 629 590 256
1024 2048 3072 4096 5120 6144 7168 8192
13、计算文件所有字段的总和
[root@localhost script]# awk '{for (i=1;i<=NF;i++) s=s+$i};END {print s}' test1
3288000
14、计算每一行的和
[root@localhost script]# awk '{s=0;for(i=1;i<=NF;i++)s=s+$i;print s}' test1
21394
3226669
3073
36864
15、显示当前行的字段数,并输出当前行
[root@localhost script]# awk '{print NF ":" $0}' test1
8:1 23 566 90 189 290 6666 13569
8:27 89 289 367 4899 10999 209999 3000000
8:26 39 409 366 758 629 590 256
8:1024 2048 3072 4096 5120 6144 7168 8192
16、显示每行最后一个字段的内容
[root@localhost script]# awk '{print $NF}' test1
13569
3000000
256
8192
17、显示最后一行的最后一个字段
[root@localhost script]# awk '{field = $NF};END {print field}' test1
8192
18、显示字段数大于4的行
[root@localhost script]# awk 'NF > 4' test1
1 23 566 90 189 290 6666 13569
27 89 289 367 4899 10999 209999 3000000
26 39 409 366 758 629 590 256
1024 2048 3072 4096 5120 6144 7168 8192
19、显示最后一个字段大于4的行
[root@localhost script]# awk '$NF > 4' test1
1 23 566 90 189 290 6666 13569
27 89 289 367 4899 10999 209999 3000000
26 39 409 366 758 629 590 256
1024 2048 3072 4096 5120 6144 7168 8192
字符串反向输出
20、编辑脚本使用字符串反转
vim 1.sh
#!/bin/bash
STR=$1
LEN=${#STR}
for ((i=$LEN;i>=0;i--))
do
echo -n "${STR:i:1}"
done
echo -e "\n"
21、使用rev命令,对字符串进行反转
[root@localhost script]# echo ABCD | rev
DCBA
22、使用sed命令s参数来替换
[root@localhost script]# echo 'ABC'|sed 's/\(.\)\(.\)\(.\)/\3\2\1/g'
CBA
23、使用awk命令
[root@localhost script]# echo ABC | awk '{for(i=1;i<=length;i++){line=substr($0,i,1) line}} END {print line}'
CBA
24、使用python实现
[root@localhost script]# echo ABCD | python -c 'print raw_input() [::-1]'
DCBA
25、创建用户student1到student50,指定组为student组!而且每个用户需要设定一个不同的密码!
[root@localhost ~]# cat studentadd.sh
#!/bin/bash
for i in `seq 1 50`
do
useradd student$i -G student
echo "student$i" |passwd --stdin student$i
done
26、 编写shell脚本,将/usr/local/src目录下大于100k的文件转移到/tmp目录下:
27、打印最后一个字段
[root@localhost ~]# echo -e "line1 f1 f2 f3\n line2 f4 f5 f6"|awk '{print $NF}'
f3
f6
28、打印倒数第二个字段
[root@localhost ~]# echo -e "line1 f1 f2 f3\n line2 f4 f5 f6"|awk '{print $(NF-1)}'
f2
f5
29、一个每一行中第一个字段值累加的例子:
[root@localhost ~]# seq 5|awk 'BEGIN{ sum=0; print "总和: " }{ print $1"+";sum+=$1 } END {print "等于"; print sum}'
总和:
1+
2+
3+
4+
5+
等于
15
30、输出1-100相加的总和
[root@localhost ~]# seq 1 100 |awk 'BEGIN {sum=0} {sum+=$1} END{print sum}'
5050
31、查找/root目录下大于1M的文件并删除它
[root@localhost ~]# find /root -type f -size +1M |xargs rm -rf
32、使用awk 找出文件大于10K的文件,并打印其文件名
[root@localhost ~]# ll|sed 1d|awk '{if($5 > 10000) print $9}'
test1
test2
33、计算两个数的商
#!/bin/bash
var1=10
var2=20
var3=$(expr $var2 / $var1)
echo The result is $var3
执行结果:
root@iZ2ze5amm8uy6o4jjpvtybZ:~/script# ./test6.sh
The result is 2
34、使用方括号进行算数运算
root@iZ2ze5amm8uy6o4jjpvtybZ:~/script# cat test7.sh
#!/bin/bash
var1=100
var2=200
var3=$[ 5 * ( $var1 + $var2 ) ]
echo "$var3"
计算结果:
root@iZ2ze5amm8uy6o4jjpvtybZ:~/script# ./test7.sh
1500
35、使用方括号计算两个数的商
root@iZ2ze5amm8uy6o4jjpvtybZ:~/script# cat test8.sh
#!/bin/bash
var1=100
var2=50
var3=$[ $var1 / $var2 ]
echo $var3
36、查看用户zhangwenqiang是否存在于系统中
#!/bin/bash
testuser=zhangwenqiang
if grep $testuser /etc/passwd
then
echo "The bash files for user $testuser are:"
ls -a /home/$testuser/.b*
else
echo "The user $testuser does not exist on the system"
fi
37、检测用户和其下的目录是否存在
#!/bin/bash
testuser=hello
if grep $testuser /etc/passwd
then
echo "$testuser is exist on the system"
elif
ls -d /home/$testuser
then
echo "The user $testuser is not exist on the system"
echo "However, $testuser has a directory."
fi
38、测试几个数字的大小
#/bin/bash
if grep $testuser /etc/passwd
then
echo "$testuser is exist on the system"
elif
ls -d /home/$testuser
then
echo "The user $testuser is not exist on the system"
echo "However ,$testuser has a directory."
fi
39、查看字符串var1是否大于字符串var2
#!/bin/bash
var1=baseball
var2=hockey
if [ $var1 > $var2 ]
then
echo "$var1 is greater than $var2"
else
echo "$var1 is less than $var2"
fi
40、判断字符串是否为空
var1=testing
var2=
if [ -n $var1 ];then
echo "The string '$var1' is not empty"!
else
echo "The string '$var2' is empty"!
fi
if [ -z $var2 ];then
then
echo "The string '$var1' is empty"
else
echo "Ths string "$var2" is not empty"
fi
41、遍历下一个城市的名称
#!/bin/bash
for i in shanghai beijing hangzhou guangzhou shenzheng
do
echo "The next city is $i"
done
43、从命令读取值
#!/bin/bash
file="/root/test"
for city in $(cat $file)
do
echo next is a beautiful $city
done
44、判断目录下的文件是目录,还是文件
#!/bin/bash
for file in /root/*
do
if [ -d $file ];then
echo "$file is a directory"
elif [ -f $file ];then
echo "$file is a file"
else
echo "No such file"
fi
done
45、使用for循环输出数列
for (( i=1 ; i<=10 ; i++ ))
do
echo "The next number is $i "
done
46、使用多个变量
#!/bin/bash
for (( a=10,b=1;b<=10;a--,b++ ))
do
echo $a - $b
done
47、使一个变量从100,递减为1
#!/bin/bash
a=100
while [ $a -gt 1 ]
do
echo "The next number is $a"
a=$[ $a - 1]
done
48、使用util语句循环
var1=100
until [ $var1 -eq 0 ]
do
echo $var1
var1=$[$var1 - 25]
done
49、使用循环嵌套
for (( a=1;a<=3;a++ ))
do
echo "Start Inner loop: $a"
for (( b=1;b<=3;b++ ))
do
echo "loop: $b"
done
done
50、使用until和while混合循环
var3=3
until [ $var3 -eq 0 ]
do
echo "The Outer loop is: $var3 "
var2=1
while [ $var2 -lt 5 ]
do
var3=$(echo scale=4; $var1 / $var2|bc)
echo "The inner loop is: $var3"
var2=$[$var2 + 1]
done
var1=$[$var1 - 1]
done
51、使用break 命令跳出循环
#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9
do
echo "The next number is $i"
if [ $i -eq 5 ];then
break
fi
done
52、 使用if语句退出for循环
#!/bin/bash
i=1
while [ $i -lt 5 ]
do
if [ $i -eq 3 ];then
break
fi
echo "The next number is $i"
i=$[$i + 1]
done
echo "End the while loop"
53、使用break跳出外部循环
#!/bin/bash
for (( a=1;a<=5;a++ ))
do
echo "The Outer loop is $a"
for (( b=1;b<=100;b++ ))
do
if [ $b -gt 4 ];then
break 2
echo "The Inner loop is $b"
done
done
54、使用continue语句
#!/bin/bash
for (( a=1;a<=100;a++ ))
do
if [ $a -gt 5 ] && [$a -lt 20];then
continure
fi
echo "The benifit number is $a"
done
55、定义继续循环的等级
#!/bin/bash
for (( a=1;a<=5;a++ ))
do
for (( b=1;b<=10;b++ ))
do
if [ $b -gt 5] && [ $b -lt 8 ];then
continue 2
fi
c=$[$a * $b]
echo "$a multiple $b is $c"
done
done
56、 查看目录下文件的数量
#!/bin/bash
file="/root/local/bin"
for i in $file
do
number=`ls -l $file|grep "^-"|wc -l`
done
echo "There are $number files in the directory."
57、查找并显示文件下的可执行文件
#!/bin/bash
IFS=:
for folder in $PATH
do
echo "$folder :"
for i in $folder
do
if [ -x $i ];then
echo "$i"
fi
done
done
58、添加几个用户
#!/bin/bash
input="user.csv"
IFS=“,”
while IFS="," read -r userid,name
do
echo "adding $userid"
useradd -c $name -m $userid
done < $input
59、求斐波那契数列的值
#!/bin/bash
factorial=1
for (( number=1;number<=$1;number++ ))
do
factorial=$[$factorial * $number]
done
echo $factorial
60、输入两个数并做乘法运算
#!/bin/bash
echo "Please input a number $1"
echo "Please input a number $2"
multinumber=$[$1 * $2]
echo "$1 multiply $2 is $multinumber."
61、查看脚本的名称
#!/bin/bash
echo "The script name is $0"
62、显示不包含路径的脚本名称
#!/bin/bash
name=$[basename $0]
echo "The script number is $name."
63、检测是否存在数据
#!/bin/bash
if [ -n "$1" ];then
echo "Hello $1,nice to meet you!"
else
echo "Sorry ,your don't identify yourself."
fi
64、统计脚本携带参数的个数
#!/bin/bash
echo "There are $# parameters for the script."
65、输入两个数并求它们的和
#!/bin/bash
if [ $# -ne 2 ];then
echo "Please input two numbers."
else
total=$[ $1 + $2 ]
echo "The total value is $total."
fi
66、显示脚本最后的一个参数
#!/bin/bash
params=$#
echo The last parameter is $params
echo The last parameter is ${!#}
67、显示脚本的所有参数
#!/bin/bash
echo
echo "The script all parameter is $*"
echo "The script all parameter is $@"
echo
68、显示$@与$*的区别
echo
count=1
for param in $*
do
echo "The \$* parameter #$count = $param"
count=$[$count + 1]
done
echo
count=1
for param in $@
do
echo "The \$@ parameter #$count = $param
count=$[$count + 1]
done
69、移动变量
#!/bin/bash
count=1
echo
while [ -n "$1" ]
do
echo "The next parameter is #$count is $1"
count=$[$count + 1]
shift
done
70、使用shift移动多个位置
#!/bin/bash
echo "The orgin parameter : $*"
shift 2
echo "Here's the new parameter $1"
71、使用case语句与shift语句相结合
#!/bin/bash
while [ -n "$1" ]
do
case $1 in
-a) echo "Find -a option" ;;
-b) echo "Find -b option";;
-c) echo "Find -c option";;
*) echo "$1 is not an option";;
esac
shift
done
72、遍历给定的参数,跳过破折号
#!/bin/bash
while [ -n "$1" ]
do
case $1 in
-a) echo "Find -a option" ;;
-b) echo "Find -b option";;
-c) echo "Find -c option";;
--) break
shift ;;
*) echo "$1 is not an option" ;;
esac
count=1
for param in $@
do
echo "$count : $param"
count=$[$count + 1]
done
73、写一个程序,计算自己来到这个世界的天数
#!/bin/bash
echo "Please input your age"
read age
echo "You live in this world for $[ 365 * $age ] days"
74、写一个程序,输出你的姓名
#!/bin/bash
echo "Please Input your name"
read -p firstname lastname
echo "Your name is $lastname $firstname"
75、特殊REPLY的使用
#!/bin/bash
read -p "input your name"
echo
echo "hello $REPLY,nice to meet you."
76、使用-t 指定一个定时计,在规定的时间,没有输入变量的,设定一个提出的状态
#!/bin/bash
if read -t 5 -p "echo please input your name:" name
then
echo "Hello $name ,nice to meet you."
else
echo "Sorry,you input is too slow."
fi
77、编辑一个判断的程序
#!/bin/bash
if read -n1 -p “please input your choice: Y|N" answer
then
case $answer in
Y|y)
echo "Continue on" ;;
N|n)
echo "Good Bye"
esac
fi
78、输入你的密码
#!/bin/bash
read -s -p "Please input your password Y|N" pass
echo
echo "Is your password $pass"
79、统计脚本中的文件的数量
#!/bin/bash
count=1
cat /root/script/test | while read line
do
Line: $count line
count=$[ $count + 1]
done
echo "Finish process the file"
80、使用从文本读入文件并输出
#!/bin/bash
exec 0 < testfile
count=0
while read line
do
Line: #$count $line
count=$[ $count + 1 ]
done
echo "Have finished process the file"
81、使用tee命令多重重定向
#!/bin/bash
date | tee test
82、使用Trap命令忽略信号 SIGINT
#!/bin/bash
trap "echo 'sorry I have trapped Ctrl-C' " SIGINT
count=1
while [ $count -le 10 ]
do
echo " Loop: #$count "
sleep 1
count=$[ $count + 1 ]
done
echo "This is the end of script."
83、用trap命令捕获脚本的退出
#!/bin/bash
trap "echo 'Goodbye..'" EXIT
count=1
while [ $count -le ]
do
echo "Loop: #$count " EXIT
sleep 1
count=$[ $count + 1 ]
done
84、修改或移除捕获
#!/bin/bash
trap "echo 'Sorry ...Ctrl-C is trapped'" SIGINT
count=1
while [ $count -le 5 ]
do
echo "The first Loop: #$count"
sleep 1
count=$[ $count + 1 ]
done
trap "echo 'I modified the trap' " SIGINT
count=1
while [ $count -le 6 ]
do
echo "The second Loop: #$count"
count=$[ $count + 1 ]
done
echo "The end of script."
85、查看系统分配给脚本的PID
#!/bin/bash
echo "Script process ID: $$"
count=1
while [ $count -le 10 ]
do
echo "Loop: #$count"
count=$[ $count + 1]
done
echo "End of script ..."
86、利用&将另外一个作业作为后台进程启动,出于简化的目的,脚本的输出被重定向到文件中,避免出现在屏幕上。jobs显示这些进程、进程的PID及其状态。
[root@system1 script]# ./6.sh > test.out &
[1] 1873
[root@system1 script]# jobs
[1]+ Running ./6.sh > test.out &
[root@system1 script]# jobs -l
[1]+ 1873 Running ./6.sh > test.out &
[root@system1 script]# cat test.out
The script process id is 1873
Loop: #1
Loop: #2
Loop: #3
Loop: #4
Loop: #5
Loop: #6
Loop: #7
Loop: #8
Loop: #9
Loop: #10
Completed the program.
[1]+ Done ./6.sh > test.out
87、使用bg重启一个后台的作业
[root@system1 script]# ./6.sh
The script process id is 1995
Loop: #1
Loop: #2
^Z
[1]+ Stopped ./6.sh
[root@system1 script]# bg
[1]+ ./6.sh &
Loop: #3
[root@system1 script]# Loop: #4
Loop: #5
Loop: #6
^C
[root@system1 script]# Loop: #7
Loop: #8
Loop: #9
Loop: #10
^C
[root@system1 script]# Completed the program.
[1]+ Done ./6.sh
88、使用nice命令来设置命令启动时的优先级
[root@system1 script]# nice -n 10 ./6.sh > test2.out &
[1] 2137
[root@system1 script]# ps -p 2137 -o pid,ppid,ni,cmd
PID PPID NI CMD
2137 1671 10 /bin/bash ./6.sh
89、Nice 改变进程的优先级
[root@system1 script]# nice -n 10 ./6.sh > test2.out &
[1] 2137
[root@system1 script]# ps -p 2137 -o pid,ppid,ni,cmd
PID PPID NI CMD
2137 1671 10 /bin/bash ./6.sh
90、输出字段1,3,6,以制表符作为分隔符
awk -F: '{print $1,$3,$6}' OFS="\t" /etc/passwd
91、计算当前目录的文件的大小
ls -l|awk 'BEGIN{sum=0} !/^d/{sum+=$5} END{print "total size is",sum}'
total size is 66257
92、自定义awk输出
awk -F: '{print "Username: "$1 "\t\t Uid:" $3}' /etc/passwd
93、输出文件本身,并显示每行的行号
awk -F: '{print NR,$0}' /etc/passwd
94、依次打印行号,字段数,最后字段值,制表符,每行内容
awk -F: '{print NR,NF,$NF,"\t",$0}' /etc/passwd
95、显示第5行
awk -F: 'NR==5{print}' /etc/passwd
96、显示第5行和第6行
awk -F: 'NR==5||NR==6{print}' /etc/passwd
97、输出不匹配tcpdump的行
awk '!/tcpdump/{print $0}' /etc/passwd
98、输出匹配tcpdump或gdm的行
awk '/tcpdump|gdm/{print}' /etc/passwd
99、$1匹配的内容才显示
awk -F: '{if($1~/gdm/)print $1}' /etc/passwd
100、显示$1不匹配的内容
awk -F: '$1!~/gdm/{print $1}' /etc/passwd
101、显示组ID大于60000的用户名
awk -F: '$3>60000{print $1}' /etc/passwd
102、输出用户名为tcpdump的组的ID号
awk -F: '$1=="tcpdump"{print $3}' /etc/passwd
103、只有用户sync的组ID大于1时才显示组ID
awk -F: '$1~/sync/ && $3>1 {print $3}' /etc/passwd
104、显示用户名为tcpdump或者组ID大于1000的记录
awk -F: '$1~/tcpdump/ || $3>1000 {print }' /etc/passwd
105、输出组ID大于10000的用户记录
awk -F: '$3 > 10000' /etc/passwd
106、打印出组ID大于10000或者小于5的记录
awk -F: '$3 > 10000|| $3<5 {print} ' /etc/passwd
107、把用户名为tcpdump的用户的组ID加10输出
awk -F: '/tcpdump/{print $3+10}' /etc/passwd
108、显示实际内存剩余的空间,取整数
awk '/MemFree/{print int($2/1024)}' /proc/meminfo
109、显示/etc/passwd文件的第五行
sed -n '5p' /etc/passwd
110、把竖排的数字变成横排
seq 6|sed -e ':a;N;s/\n/ /g;ta'
111、把横排的数字变成竖排
seq 6|awk 'BEGIN{RS="\n";ORS=" "} {print $0}
112、把竖排的数字变成横排并求和
seq 6|awk 'BEGIN{RS="\n";ORS=" "} {print $0}|awk '{for(i=1;i<=NF;i++)sum+=$i;print sum}'
113、输出一串数字,删去第一行和第三行
seq 6|sed '{1d;3d}'
114、只显示/etc/passwd文件的最后一行
cat /etc/passwd|sed -n '$p'
115、在1.txt文件里1111后面插入2222数字和前面插入0000数字
[root@localhost ~]# cat 1.txt
1111
3333
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# sed -i '/^1111/a\2222' 1.txt ;cat 1.txt
1111
2222
3333
[root@localhost ~]# sed -i '/^1111$/i\0000' 1.txt ;cat 1.txt
0000
1111
2222
3333
116、使用sed获取主机的IP地址
[root@localhost ~]# ifconfig eth0|grep 'inet addr'|sed 's/^.*addr://g'|sed 's/Bcast.*$//g'
192.168.204.128
转载于:https://blog.51cto.com/shamereedwine/1981370