Linux shell脚本编程基础之练习篇
shell脚本编程基础之练习篇。
-
1、编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息。
#!/bin/bash if [ $# -ne 1 ] then echo "请输入一个参数" exit else echo "参数正确" newfile=$1 fi #echo `grep "^#\!" ${newfile}` if ! grep "^#\!" ${newfile} &>/dev/null then cat >>${newfile}<<EOF #!/bin/bash # Author: Inert Your Name here. #Date & Time: `date +"%F %T"` #Description: Please Edit here. EOF fi vi +5 ${newfile}
将脚本改个名字例如:newshfile,将其放置在/bin/目录下,那么你的系统就多了一个新的newshfile命令了
-
2、求100以内偶数的和
#!/bin/bash # Author: Inert Your Name here. #Date & Time: 2015-06-02 20:37:07 #Description: Please Edit here. let sum=0 for index in {1..100} do if [ $[ ${index}%2 ] == 0 ]; then #let sum+=${index} sum=`expr ${sum} + ${index}` fi done echo "sum=${sum}" let sum=0 for num in $(seq 1 100); do if [ $[ $num % 2 ] == 0 ]; then sum=`expr $sum + $num` fi done echo "sum=$sum"
- 判断输入的参数个数,如果为两个参数则相加并输出相加后的值
#!/bin/bash if [ $# -eq 2 ] then echo "参数个数 $#\n" echo "参数相加 $1 + $2 = `expr $1 + $2`" else echo "参数个为 $#,本脚本需要两个参数" fi
- 用while\for循环降序输出1~5的值
#!/bin/sh num=5 while test $num != 0 do echo "$num" num=`expr $num - 1` done echo "*****************************" num=5 while (($num != 0)) do echo "$num" num=`expr $num - 1` done echo "*****************************" for num in {5..1} do echo "$num" done echo "*****************************" for ((num=5;$num>0;num=`expr $num - 1`)) do echo "$num" done
- 加减乘除运算
#!/bin/bash # Author: Inert Your Name here. #Date & Time: 2015-06-02 21:32:51 #Description: Please Edit here. if test $# == 3 then echo "参数个数$#,参数:$@" case $1 in +) num=`expr $2 + $3` ;; -) num=`expr $2 - $3` ;; x) num=`expr $2 \* $3` ;; /) num=`expr $2 \/ $3` ;; *) echo "只允许+ - x /这几个运算符" ;; esac echo "num=$num" else echo "参数个数为3个,分别为\"运算符 参数1 参数2\"" fi
- 浮点数的运算
echo 5.12 + 2.5 | bc
#!/bin/bash # Author: Inert Your Name here. #Date & Time: 2015-06-02 22:07:28 #Description: Please Edit here. a=5.66 b=8.67 c=`echo $a + $b | bc` echo "$a + $b = $c"
- 打印以下各种图形效果
1 22 333 4444 55555 **************************** 1 12 123 1234 12345 **************************** |_ ||_ |||_ ||||_ |||||_ **************************** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ************lengxing**************** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#!/bin/bash # Author: Inert Your Name here. #Date & Time: 2015-06-02 22:24:41 #Description: Please Edit here. for ((num=1;$num<=5;num=`expr $num + 1`)) do for((index=$num;$index>0;index=`expr $index - 1`)) do echo -n "$num" done echo "" done echo "****************************" for ((num=1;$num<=5;num=`expr $num + 1`)) do for((index=1;$index<=$num;index=`expr $index + 1`)) do echo -n "$index" done echo "" done echo "****************************" for ((num=1;$num<=5;num=`expr $num + 1`)) do for((index=1;$index<=$num;index=`expr $index + 1`)) do if [ $index%2 == 0 ]; then echo -n " " else echo -n "|" fi done echo -n "_" echo "" done echo "****************************" for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n " *" done echo "" done for (( i=5; i>=1; i-- )) do for (( j=1; j<=i; j++ )) do echo -n " *" done echo "" done echo "************lengxing****************" max=6 for ((i=1; i<=$max; i++)) do for ((j=$max-i; j>0; j--)) do echo -n " " done for ((k=1; k<=i; k++)) do echo -n " *" done echo "" done for ((i=1; i<=$max; i++)) do for ((k=1; k<=i; k++)) do echo -n " " done for ((j=$max-i; j>0; j--)) do echo -n " *" done echo "" done
扫描公众号,关注更多信息
---------------------------------------------------------------------------------我是分割线--------------------------------------------------------------------------to be a better me, talk is cheap show me the code
版权所有,转载请注明原文链接。
文中有不妥或者错误的地方还望指出,以免误人子弟。如果觉得本文对你有所帮助不妨【推荐】一下!如果你有更好的建议,可以给我留言讨论,共同进步!
再次感谢您耐心的读完本篇文章。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------