Shell编程基础

#!/bin/sh

变量赋值和引用


#Object Assignment
studentName="leiwuluan";
score=111;

echo "$studentName score is $score";

if 语 句

#shell if...then else
tom="boy";
jierui="girl";

if [ "$tom" = "$jierui" ]; then
        echo "tom and jierui sex together!";
else
        echo "tom sex=$tom , jierui sex=$jierui, they sex dones't the same";
fi

case 语句

#shell case
flag="3";

case "$flag" in
        "1"*)
        echo "1";;
        "2"*)
        echo "2";;
        "3"*)
        echo "3";;
*)echo "other";;
esac

select 语句

#select

echo "What is your favourite os?";
#select var in "Linux" "Window" "Other" "Leiwuluan";do
#       break;
#done
#echo "$var is your favourite os!";

 

 

while/for 循环

#while for
for i in a b c d e;do
        echo "$i";
done


for i in $*;do
        if [ -f "$rpmpackage" ];then
                echo "============$rpmpackage==============";
        else
                echo "=====ERROR:connot read file $rpmpackage"
        fi
done

 

 

Shell里的一些特殊符号

引号

引号(单引号和双引号)可以防止通配符*的扩展:

 

 

Here Document

       当要将几行文字传递给一个命令时,用here documents是一种不错的方法。对每个脚本写一段帮助性的文字是很有用的,此时如果使用here documents就不必用echo函数一行行输出。Here document以 << 开头,后面接上一个字符串,这个字符串还必须出现在here document的末尾。下面是一个例子,在该例子中,我们对多个文件进行重命名,并且使用here documents打印帮助:

 

 

 

 

#!/bin/sh/
rm 1.txt 2.txt 3.txt
for i in {01,02,03,04,05};
do
        echo "/home/co_athena/sgh_day_log/allLogs/log1_6.log.2011-09-$i >> 1.txt";
	cat /home/co_athena/sgh_day_log/allLogs/log1_6.log.2011-09-$i >> 1.txt;
done

for i in {09,10,11,12};
do
        echo "/home/co_athena/sgh_day_log/allLogs/log1_6.log.2011-10-$i >> 2.txt";
	cat /home/co_athena/sgh_day_log/allLogs/log1_6.log.2011-10-$i >> 2.txt;
done

for i in {24,25,26,27};
do
        echo "/home/co_athena/sgh_day_log/allLogs/log1_6.log.2011-10-$i >> 3.txt";
	cat /home/co_athena/sgh_day_log/allLogs/log1_6.log.2011-10-$i >> 3.txt;
done

echo "线:31221858 PV UV";
cat 1.txt |grep "3802158" |awk -F">adid=" '{print $2}' |awk -F"<[|]>" '{print $1}' |grep -v null |wc -l
cat 1.txt |grep "380821858" |awk -F">adid=" '{print $2}' |awk -F"<[|]>" '{print $1}' |grep -v null |sort |uniq |wc -l 

echo "携程:3732125 PV UV";
cat 2.txt |grep "32129415" |awk -F">adid=" '{print $2}' |awk -F"<[|]>" '{print $1}' |grep -v null |wc -l
cat 2.txt |grep "372115" |awk -F">adid=" '{print $2}' |awk -F"<[|]>" '{print $1}' |grep -v null |sort |uniq |wc -l 

echo "网:4455754 PV UV";
cat 3.txt |grep "445214" |awk -F">adid=" '{print $2}' |awk -F"<[|]>" '{print $1}' |grep -v null |wc -l
cat 3.txt |grep "44553854" |awk -F">adid=" '{print $2}' |awk -F"<[|]>" '{print $1}' |grep -v null |sort |uniq |wc -l 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(linux,shell)