shell script

一  每隔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
原文地址:https://www.cnblogs.com/iloveyoucc/p/2451643.html