Linux 一些简单的Shell实例
?
?
[root@localhost temp]# cat a.sh#!/bin/bash#helloecho '你好'echo 'Hello World!'[root@localhost temp]# ./a.sh你好Hello World![root@localhost temp]# cat b.sh#!/bin/bash#bianliang b.sha=123b=1.23c=xyzd=efgh xyze='efgh xyz'echo $aecho $becho $cecho $decho $e[root@localhost temp]# ./b.sh./b.sh: line 6: xyz: command not found1231.23xyzefgh xyz[root@localhost temp]# cat c.sh#!/bin/bash#c.shecho $1echo $2echo $3echo $4[root@localhost temp]# ./c.sh a b c d eabcd[root@localhost temp]# cat e.sh#!/bin/bash#e.shecho 开始read ai=$[ $a % 2 ]echo -e $a"\c"if test $i -eq 0 then echo 是偶数else echo 是奇数fiecho 结束[root@localhost temp]# ./e.sh开始33是奇数结束[root@localhost temp]# cat f.sh#!/bin/bash#e.shecho 开始i=$[ $1 % 2 ]echo -e $1"\c"if test $i -eq 0 then echo 是偶数else echo 是奇数fiecho 结束[root@localhost temp]# ./f.sh 8开始8是偶数结束[root@localhost temp]# cat g.sh#!/bin/bash#g.shif test -z $1 then echo "请输入一个文件名"else if test -w $1 then echo "可写" else echo "不可写" fi if test -x $1 then echo "可执行" else echo "不可执行" fi if test -z $2 then echo "参数2要输入" elif test $2 -eq 1 then echo "输入1" elif test $2 -eq 2 then echo "输入2" else echo "输入"$2 fifi[root@localhost temp]# ./g.sh a.sh 2可写可执行输入2[root@localhost temp]# cat h.sh#!/bin/bash#h.shfor char in a b c d edo echo $chardonefor strdo echo $strdone[root@localhost temp]# ./h.shabcde[root@localhost temp]# cat i.sh#!/bin/bash#i.shfiles=`ls *.sh`for sh in $filesdo txt=`echo $sh | sed "s/.sh/.txt/"` cp $sh $txt echo $txtdone[root@localhost temp]# ./i.sha.txtb.txtc.txtd.txte.txtf.txtg.txth.txti.txtj.txtk.txt[root@localhost temp]# cat j.sh#!/bin/bash#j.shfor i in 1 2 3 4 5 6 7 8 9do for j in 1 2 3 4 5 6 7 8 9 do if test $i -ge $j then echo -e $j \* $i = $[ $j * $i ]" \c" fi done echo ""done[root@localhost temp]# ./j.sh1 * 1 = 1 1 * 2 = 2 2 * 2 = 4 1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49 1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64 1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81 [root@localhost temp]# cat k.sh#!/bin/bash#k.shecho -e "请输入一个1-100之间的整数:\c"flag=0until [ $flag -eq 1 ]do read num expr $num "+" 10 &> /dev/null if test $? -ne 0 then echo -e $num"不是整数,请重新输入:\c" else if test $num -ge 1 -a $num -le 100 then flag=1 else echo -e $num"不在1-100之间,请重新输入:\c" fi fidonesum=0i=1until test $i -gt $numdo sum=$[$sum+$i] i=$[$i+1]doneecho ""echo "从1到"$num"的总和为:"$sum[root@localhost temp]# ./k.sh请输入一个1-100之间的整数:aa不是整数,请重新输入:0 0不在1-100之间,请重新输入:101101不在1-100之间,请重新输入:100.01100.01不是整数,请重新输入:20从1到20的总和为:210[root@localhost temp]#
?
?