Shell程序荟萃

--------------纯输出----------------1.sh

#!/bin/sh

echo -e "Hello!world!\a\n"
exit 0

--------------获得用户输入----------------2.sh

#!/bin/sh

read -p "Please input your name:" name
echo -e "\nHello!$name"
exit 0

--------------日期计算昨天今天明天----------------3.sh

#!/bin/sh

yesterday=$(date --date='1 days ago' +%Y/%m/%d)
today=$(date +%Y%m%d)
tomorrow=$(date -d '1 days' +%Y-%m-%d)

echo -e "Yesterday is $yesterday"
echo -e "Today is $today"
echo -e "Tomorrow is $tomorrow"
exit 0

--------------100以内加减法计算游戏----------------4.sh

#!/bin/sh

declare -i rnd1=$RANDOM
declare -i rnd2=$RANDOM

op1=$(( $rnd1%100 ))
op2=$(( $rnd2%100 ))

if [ $op2 -gt $op1 ]; then
   op3=$op2
   op2=$op1
   op1=$op3
fi

op4=$(( $op1%2 ))

if [ $op4 == 1 ]; then
   result=$(( $op1+$op2 ))
   read -p "$op1+$op2=" resp

   if [ $result == $resp ]; then
      echo " v"
   else
      echo " x"
   fi
else
   result=$(( $op1-$op2 ))
   read -p "$op1-$op2=" resp

   if [ $result == $resp ]; then
      echo " v"
   else
      echo " x"
   fi
fi

exit 0
--------------待续----------------

posted @ 2013-09-03 22:50  逆火狂飙  阅读(152)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东