Linux Shell学习简单小结
shelllinuxperformancefunctionfile
if-fi
[cpp] view plaincopyprint?
#! /bin/bash
# 删除文件 和 新建文件
file=readme
function delFile(){
if [ -e ./$file ];then
rm -f ./$file
echo "del $file ..."
fi
}
function addFile(){
if [ ! -f ./$file ];then
touch $file
echo "add $file ..."
fi
}
delFile
addFile
#! /bin/bash # 删除文件 和 新建文件 file=readme function delFile(){ if [ -e ./$file ];then rm -f ./$file echo "del $file ..." fi } function addFile(){ if [ ! -f ./$file ];then touch $file echo "add $file ..." fi } delFile addFile
Result:
(没有readme文件)
[work shell]$ sh if_e.sh
add readme ...
[workshell]$ sh if_e.sh
del readme ...
add readme ...
===================================================
if-else-fi
[cpp] view plaincopyprint?
#! /bin/bash
echo "press y hello"
read str
if [ "$str" = "y" ] || [ "$str" = "Y" ];then
echo "hello"
else
echo "bye.."
fi;
#! /bin/bash echo "press y hello" read str if [ "$str" = "y" ] || [ "$str" = "Y" ];then echo "hello" else echo "bye.." fi;
Result:
[work shell]$ sh if.sh
press y hello
y
hello
[work shell]$ sh if.sh
press y hello
n
bye..
===================================================
if-elif-else-if(函数传参1)
[python] view plaincopyprint?
# !/bin/sh
type=1 # 1, 2, 3(a,abc,123)
function getVal(){
echo "$1"
}
if [ "$type" == "1" ]; then
for((i=0;i<4;i++))
do
eval getVal $i
done
elif [ "$type" == "2" ]; then
echo "type"
else
echo "none"
fi
# !/bin/sh type=1 # 1, 2, 3(a,abc,123) function getVal(){ echo "$1" } if [ "$type" == "1" ]; then for((i=0;i<4;i++)) do eval getVal $i done elif [ "$type" == "2" ]; then echo "type" else echo "none" fi
Result:
yanggang@barry$ ./param.sh
0
1
2
3
===================================================
if-elif-else-if(函数传参2)
[python] view plaincopyprint?
# !/bin/sh
#type=1 # 1, 2, 3(a,abc,123)
function getVal(){
echo "$1"
}
function inputVal(){
if [ "$1" == "1" ]; then
for((i=0;i<4;i++))
do
eval getVal $i
done
elif [ "$1" == "2" ]; then
echo "type"
else
echo "none"
fi
}
inputVal 1 # 1 is a param
# !/bin/sh #type=1 # 1, 2, 3(a,abc,123) function getVal(){ echo "$1" } function inputVal(){ if [ "$1" == "1" ]; then for((i=0;i<4;i++)) do eval getVal $i done elif [ "$1" == "2" ]; then echo "type" else echo "none" fi } inputVal 1 # 1 is a param
Result:
yanggang@barry$ ./param.sh
0
1
2
3
===================================================
case 条件
mysql
[python] view plaincopyprint?
#!/bin/bash
case "$1" in
start)
echo "mysql start..."
/etc/init.d/mysqld start
;;
stop)
echo "mysql stop..."
/etc/init.d/mysqld stop
;;
restart)
echo "mysql restart..."
/etc/init.d/mysqld stop
/etc/init.d/mysqld start
;;
esac
exit 0
#!/bin/bash case "$1" in start) echo "mysql start..." /etc/init.d/mysqld start ;; stop) echo "mysql stop..." /etc/init.d/mysqld stop ;; restart) echo "mysql restart..." /etc/init.d/mysqld stop /etc/init.d/mysqld start ;; esac exit 0
httpd(apache)
[python] view plaincopyprint?
#!/bin/bash
case "$1" in
start)
echo "apache start..."
/etc/init.d/httpd start
;;
stop)
echo "apache stop..."
/etc/init.d/httpd stop
;;
restart)
echo "apache restart..."
/etc/init.d/httpd restart
;;
status)
echo "apache status"
/etc/init.d/httpd status
;;
esac
exit 0
#!/bin/bash case "$1" in start) echo "apache start..." /etc/init.d/httpd start ;; stop) echo "apache stop..." /etc/init.d/httpd stop ;; restart) echo "apache restart..." /etc/init.d/httpd restart ;; status) echo "apache status" /etc/init.d/httpd status ;; esac exit 0
===================================================
awk
[python] view plaincopyprint?
#! /bin/bash
# 统计type类型文件,总共含有多少行
type="*.h *.cpp"
result="result.txt"
# 统计函数
function cal_lines()
{
rm -f $result #删除文件
for file in `ls $type`; do
wc -l $file >> $result #新建并追加统计行数到文件
done
awk ‘{total+=$1} END {print total}‘ $result >> $result #awk累加第一列,相加结果为total
}
cal_lines #调用函数
cat $result #查看结果
#! /bin/bash # 统计type类型文件,总共含有多少行 type="*.h *.cpp" result="result.txt" # 统计函数 function cal_lines() { rm -f $result #删除文件 for file in `ls $type`; do wc -l $file >> $result #新建并追加统计行数到文件 done awk ‘{total+=$1} END {print total}‘ $result >> $result #awk累加第一列,相加结果为total } cal_lines #调用函数 cat $result #查看结果
Result:
[work]$ sh cal_lines.sh
ls: *.h: No such file or directory
91 test_performance_server.cpp
178 test_performance_ubclient1.cpp
230 test_performance_ubclient2_common_async.cpp
204 test_performance_ubclient2_common_block.cpp
206 test_performance_ubclient2_common_nonblock.cpp
191 test_performance_ubclient2_common_single_block.cpp
193 test_performance_ubclient2_common_single_nonblock.cpp
237 test_performance_ubclient2_nshead_async.cpp
220 test_performance_ubclient2_nshead_block.cpp
218 test_performance_ubclient2_nshead_nonblock.cpp
192 test_performance_ubclient2_nshead_single_block.cpp
192 test_performance_ubclient2_nshead_single_nonblock.cpp
2352
===================================================
linux实现两个文件内容相加(3种解法)
a.txt(10行) b.txt(9行)
a.txt b.txt
解法一
[python] view plaincopyprint?
awk ‘NR==FNR{a[NR]=$1; b[NR]=$2}
NR > FNR{print $1 + a[FNR], $2+b[FNR]}‘ a.txt b.txt
awk ‘NR==FNR{a[NR]=$1; b[NR]=$2} NR > FNR{print $1 + a[FNR], $2+b[FNR]}‘ a.txt b.txt
运行结果:
[work]$ sh cal_ab1.sh
1 83
1 77
0 128
24 195
1 130
68 227
5 132
197 233
9 146
--------------------------------------------------------------------------------------
解法二
[python] view plaincopyprint?
paste a.txt b.txt > c.txt
while read a b c d; do
echo $((a+c)) $((b+d))
done < c.txt
paste a.txt b.txt > c.txt while read a b c d; do echo $((a+c)) $((b+d)) done < c.txt
运行结果:
[work]$ sh cal_ab2.sh
1 83
1 77
0 128
24 195
1 130
68 227
5 132
197 233
9 146
0 8
--------------------------------------------------------------------------------------
解法三
[python] view plaincopyprint?
paste a.txt b.txt | awk ‘{print $1+$3 "/t" $2+$4}‘
paste a.txt b.txt | awk ‘{print $1+$3 "/t" $2+$4}‘
运行结果:
[work]$ sh cal_ab3.sh
1 83
1 77
0 128
24 195
1 130
68 227
5 132
197 233
9 146
0 8
--------------------------------------------------------------------------------------
评析:
解法一,结果不准确,只输出了前9行
解法二,结果正确,但不够简洁
解法三,结果正确,简洁
===================================================
while循环
[python] view plaincopyprint?
# !/bin/sh
TOP_NUM=800
index=0
function url_down(){
while [ $index -le $TOP_NUM ]
do
echo $index
index=`expr $index + 24`
done
}
url_down
# !/bin/sh TOP_NUM=800 index=0 function url_down(){ while [ $index -le $TOP_NUM ] do echo $index index=`expr $index + 24` done } url_down
运行结果:
yanggang@barry$ ./tmp.sh
0
24
48
72
96
120
144
168
192
216
240
264
288
312
336
360
384
408
432
456
480
504
528
552
576
600
624
648
672
696
720
744
768
792
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。