shell脚本编程基础之练习篇。
-
1、编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息。
#!/bin/bashif [ $# -ne 1 ]then echo "请输入一个参数" exitelse echo "参数正确" newfile=$1fi#echo `grep "^#\!" ${newfile}`if ! grep "^#\!" ${newfile} &>/dev/nullthencat >>${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=0for index in { 1..100}doif [ $[ ${index}%2 ] == 0 ]; then #let sum+=${index} sum=`expr ${ sum} + ${index}`fidoneecho "sum=${sum}"let sum=0for num in $(seq 1 100); doif [ $[ $num % 2 ] == 0 ]; then sum=`expr $sum + $num`fidoneecho "sum=$sum"
- 判断输入的参数个数,如果为两个参数则相加并输出相加后的值
#!/bin/bashif [ $# -eq 2 ]then echo "参数个数 $#\n" echo "参数相加 $1 + $2 = `expr $1 + $2`"else echo "参数个为 $#,本脚本需要两个参数" fi
- 用while\for循环降序输出1~5的值
#!/bin/shnum=5while test $num != 0do echo "$num" num=`expr $num - 1`doneecho "*****************************"num=5while (($num != 0))do echo "$num" num=`expr $num - 1`doneecho "*****************************"for num in { 5..1}do echo "$num"doneecho "*****************************"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 $# == 3then echo "参数个数$#,参数:$@"case $1 in +) num=`expr $2 + $3` ;; -) num=`expr $2 - $3` ;; x) num=`expr $2 \* $3` ;; /) num=`expr $2 \/ $3` ;; *) echo "只允许+ - x /这几个运算符" ;;esacecho "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.66b=8.67c=`echo $a + $b | bc`echo "$a + $b = $c"
- 打印以下各种图形效果
122333444455555****************************112123123412345****************************|_||_|||_||||_|||||_**************************** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *************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" doneecho ""doneecho "****************************"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 doneecho -n "_"echo ""doneecho "****************************"for (( i=1; i<=5; i++ ))do for (( j=1; j<=i; j++ )) do echo -n " *" done echo ""donefor (( i=5; i>=1; i-- ))do for (( j=1; j<=i; j++ )) do echo -n " *" done echo ""doneecho "************lengxing****************"max=6for ((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 ""donefor ((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