把我以前爲別人寫的給你吧 不過我沒有註釋 說以下大致的功能 一 每隔30分鐘將cpu 內存 使用的情況寫入日誌 #!/bin/bash LOGPATH="." info() { echo "" >> $LOGPATH/log.txt echo "" >> $LOGPATH/log.txt echo -n "============" >> $LOGPATH/log.txt date >> $LOGPATH/log.txt #cpu mpstat -A >> $LOGPATH/log.txt echo "" >> $LOGPATH/log.txt #memory free -l >> $LOGPATH/log.txt echo "" >> $LOGPATH/log.txt #disk df -h >> $LOGPATH/log.txt echo "" >> $LOGPATH/log.txt # network # nload } echo "" > $LOGPATH/log.txt while [ 2 -gt 1 ]; do info; sleep 1800; done 二 做一個菜單 包誇4項功能 輸入數字 選擇相應的功能 第1項 顯示指定的信息(如用戶/shell等) 第2項 任意輸入兩個數,選擇加減乘除 做一運算 給出結果 第3項 輸入一串以0結尾的大寫字字母串 給出其中最小的一個字母(按ascii碼) 第4項 確認並退出 #!/bin/bash PS3='Input option(1-4):' stack="Display Cacluate Sort Quit" select choice in $stack; do if [ "$choice" = "Display" ]; then read -p "Input you want to display[name/path/shell/cal]" info if [ "$info" = "name" ]; then echo "$USER" elif [ "$info" = "path" ]; then echo "$HOME" elif [ "$info" = "shell" ]; then echo "$SHELL" elif [ "$info" = "cal" ]; then cal 2011 fi elif [ "$choice" = "Cacluate" ]; then read -p "Input one number: " num1 read -p "Input the other number: " num2 read -p "Input method[A/S/M/D]: " mod case $mod in 'A' ) echo "$num1 + $num2 is $((num1+num2))";; 'S' ) echo "$num1 - $num2 is $((num1-num2))";; 'M' ) echo "$num1 * $num2 is $((num1*num2))";; 'D' ) echo "$num1 / $num2 is $((num1/num2))";; * ) echo "wrong method";; esac elif [ "$choice" = "Sort" ]; then read -p "Input an string with 0 ended:" str res=`echo $str | awk -F"0" '{print $1}'` echo $res | awk -F "" '{ a=$1; for(i=2;i<NF;i++) if( $i < a) a=$i; print a}' elif [ "$choice" = "Quit" ]; then read -s -n1 -p "Do you really want to quit?(Y)" if [ "$REPLY" = "Y" ];then exit 0; fi else echo "Invalid selection, Please input number." fi done
Shell編程示例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.