Linux之部分shell脚本练习

时间:2014-05-10 04:08:25   收藏:0   阅读:475

#键盘键入两个整数,并计算它们之和
#!/bin/bash
#
#echo -n "Input two Intergers:"
read -t 5 -p "Input two Intergers[100 and 200]:" A B
[ -z $A ] && A=100
[ -z $B ] && B=200
echo "$A plus $B is:$[$A+$B]"

#归档文件的脚本
#!/bin/bash
#
read -p "Three files:" FILE1 FILE2 FILE3
read -p "Destination:" DEST
read -p "Compress[gzip|bzip2|xz]:" COMP

case $COMP in
gzip)
   tar -zcf ${DEST}.tar.gz $FILE1 $FILE2 $FILE3
   ;;
bzip2)
   tar -jcf ${DEST}.tar.bz2 $FILE1 $FILE2 $FILE3
   ;;
xz)
   tar -cf ${DEST}.tar $FILE1 $FILE2 $FILE3
   xz ${DEST}.tar
   ;;
*)
   echo "Unknown."
   exit 9
   ;;
esac

脚本编程:
   顺序结构
   选择结构
       if
       case
   循环结构
       for
       while
       until
while循环:试用于循环次数未知的场景
语法:
   while CONDITION; do
       statement
       ...
   done

#计算100以内和
#!/bin/bash
#
declare -i I=1
declare -i SUM=0

while [ $I -le 100 ]; do
   let SUM+=$I
   let I++
done

echo $SUM

#输入字符串,提示要修改成大写的,如果是quit,则退出。
#!/bin/bash
#
read -p "Input something:" STRING

while [ $STRING != ‘quit‘ ];do
   echo $STRING | tr ‘a-z‘ ‘A-Z‘
   read -p "Input something:" STRING
done

#查看用户hadoop是否登录
#!/bin/bash
#
who | grep "hadoop" &>/dev/null
RETVAL=$?

while [ RETVAL -ne 0 ];do
   echo "date ,hadoop is not logged"
   sleep 5
   who | grep "hadoop" &>/dev/null
   RETVAL=$?
done

echo "hadoop  is logged in."


----------------------------
<<EOF
(内容)
EOF

把EOF替换成其东西
意思把内容当作标准输入传给程序
----------------------------
显示一个菜单给用户
第一版
#!/bin/bash、
#
cat << EOF
d|D) show disk usages.
m|M) show memory usages.
s|S) show swap usages.
EOF

read -p "your choice:" CHOICE
case $CHOICE in
d|D)
   echo "DISK usages:"
   df -Ph
   ;;
m|M)
   echo "MEMORY usages:"
   free -m | grep "Men"
   ;;
s|S)
   echo "SWAP usages:"
   free -m | grep "swap"
   ;;
*)
   echo "Unknown"
   exit 9
esac

esac

第二版
#!/bin/bash
#
cat << EOF
d|D) show disk usages.
m|M) show memory usages.
s|S) show swap usages.
EOF

read -p "your choice:" CHOICE
while [ $CHOICE == ‘quit‘];do    
   case $CHOICE in
   d|D)
       echo "DISK usages:"
       df -Ph
       ;;
   m|M)
       echo "MEMORY usages:"
       free -m | grep "Men"
       ;;
   s|S)
       echo "SWAP usages:"
       free -m | grep "swap"
       ;;
   *)
       echo "Unknown.."
       ;;
   esac
   read -p "Again,your choice: " CHOICE
done

知识点
man echo  
echo -e "\033[1mHello\003[0m,world"
echo -e "\033[2mHello\003[0m,world"
echo -e "\033[31mHello\003[0m,world"
echo -e "\033[41mHello\003[0m,world"
echo -e "\033[32;41mHello\003[0m,world"
echo -e "\033[1;37mHello\003[0m,world"

扩展题目:给用户颜色的显示
echo -n -e "\033[31myour choice:\033[0m"
read CHOICE
while [ $CHOICE == ‘quit‘];do    
   case $CHOICE in
   d|D)
       echo "DISK usages:"
       df -Ph
       ;;
   m|M)
       echo "MEMORY usages:"
       free -m | grep "Men"
       ;;
   s|S)
       echo "SWAP usages:"
       free -m | grep "swap"
       ;;
   *)
       echo "Unknown.."
       ;;
   esac
   read -p "Again,your choice: " CHOICE
done

Linux之部分shell脚本练习,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!