Linux shell脚本编程基础之练习篇

时间:2022-09-07 10:12:41

  shell脚本编程基础之练习篇。

  • 1、编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息。

#!/bin/bash
if [ $# -ne ]
then
echo "请输入一个参数"
exit
else
echo "参数正确"
newfile=$
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 + ${newfile}

将脚本改个名字例如:newshfile,将其放置在/bin/目录下,那么你的系统就多了一个新的newshfile命令了

  • 2、求100以内偶数的和

#!/bin/bash
# Author: Inert Your Name here.
#Date & Time: -- ::
#Description: Please Edit here.
let sum=
for index in {..}
do
if [ $[ ${index}% ] == ]; then
#let sum+=${index}
sum=`expr ${sum} + ${index}`
fi
done
echo "sum=${sum}" let sum=
for num in $(seq ); do
if [ $[ $num % ] == ]; then
sum=`expr $sum + $num`
fi
done
echo "sum=$sum"
  • 判断输入的参数个数,如果为两个参数则相加并输出相加后的值
#!/bin/bash
if [ $# -eq ]
then
echo "参数个数 $#\n"
echo "参数相加 $1 + $2 = `expr $1 + $2`"
else
echo "参数个为 $#,本脚本需要两个参数"
fi
  • 用while\for循环降序输出1~5的值
#!/bin/sh
num=
while test $num !=
do
echo "$num"
num=`expr $num - `
done echo "*****************************"
num=
while (($num != ))
do
echo "$num"
num=`expr $num - `
done echo "*****************************"
for num in {..}
do
echo "$num"
done echo "*****************************"
for ((num=;$num>;num=`expr $num - `))
do
echo "$num"
done
  • 加减乘除运算
#!/bin/bash
# Author: Inert Your Name here.
#Date & Time: -- ::
#Description: Please Edit here.
if test $# ==
then
echo "参数个数$#,参数:$@"
case $ in
+)
num=`expr $ + $`
;;
-)
num=`expr $ - $`
;;
x)
num=`expr $ \* $`
;;
/)
num=`expr $ \/ $`
;;
*)
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: -- ::
#Description: Please Edit here. a=5.66
b=8.67
c=`echo $a + $b | bc`
echo "$a + $b = $c"
  • 打印以下各种图形效果

****************************

****************************
|_
||_
|||_
||||_
|||||_
****************************
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
************lengxing****************
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
#!/bin/bash
# Author: Inert Your Name here.
#Date & Time: -- ::
#Description: Please Edit here. for ((num=;$num<=;num=`expr $num + `))
do
for((index=$num;$index>;index=`expr $index - `))
do
echo -n "$num"
done
echo ""
done echo "****************************"
for ((num=;$num<=;num=`expr $num + `))
do
for((index=;$index<=$num;index=`expr $index + `))
do
echo -n "$index"
done
echo ""
done echo "****************************"
for ((num=;$num<=;num=`expr $num + `))
do
for((index=;$index<=$num;index=`expr $index + `))
do
if [ $index% == ]; then
echo -n " "
else
echo -n "|"
fi
done
echo -n "_"
echo ""
done echo "****************************"
for (( i=; i<=; i++ ))
do
for (( j=; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done for (( i=; i>=; i-- ))
do
for (( j=; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done echo "************lengxing****************"
max=
for ((i=; i<=$max; i++))
do
for ((j=$max-i; j>; j--))
do
echo -n " "
done
for ((k=; k<=i; k++))
do
echo -n " *"
done
echo ""
done for ((i=; i<=$max; i++))
do
for ((k=; k<=i; k++))
do
echo -n " "
done
for ((j=$max-i; j>; j--))
do
echo -n " *"
done
echo ""
done