linux 几个简单shell脚本demo
#!/bin/bash
test=$(env | grep USER | cut -d "=" -f 2)
if [ "$test"==root ]
then
echo "current user is root"
fi
# 当前用户是
#!/bin/bash
read -t 30 -p "please input a dir:" dir
if [ -d "$dir" ]
then
echo "is dir"
else
echo "no no dir"
fi
#输入的 是否目录
#!/bin/bash
test=$(df -h | grep sda1 | awk '{print $5}' | cut -d "%" -f 1)
echo "$test"
if [ "$test" -ge "2" ];then
echo "disk sda1 gt 2"
fi
#diskwarn 硬盘大小警告
#!/bin/bash
test=$(ps aux | grep httpd | grep -v grep)
if [ -n "$test" ]
then
echo "httpd is ok"
else
echo "httpd in not start"
/etc/rc.d/init.d/httpd start
fi
# 重启apace服务
#!/bin/bash
#字符界面加减乘除计算器
read -t 30 -p "please input num1: " num1
read -t 30 -p "please input num2: " num2
#通过read命令接收要计算的数值,并赋予变量
read -t 30 -p "please input a operator: " ope
#接收符号
if [ -n "$num1" -a -n "$num2" -a -n "$ope" ]
#第一层判断, 变量中都有值
then
test1=$(echo $num1 | sed 's/[0-9]//g')
test2=$(echo $num2 | sed 's/[0-9]//g')
#判断是否为数值
if [ -z "$test1" -a -z "$test2" ]
#第二层判断,用来判断num1 num2 为数值
#如果变量test1 test2的值为空 则证明num1 num2是数字
then
#如果test1和test2是数字 则执行以下命令
if [ "$ope" == '+' ]
#第三层判断用来确认运算符
then
sum=$(( $num1 + $num2 ))
elif [ "$ope" == '-' ]
then
sum=$(( $num1 - $num2 ))
elif [ "$ope" == '*' ]
then
sum=$(( $num1 * $num2 ))
elif [ "$ope" == '/' ]
then
sum=$(( $num1 / $num2 ))
else
echo "please enter a vaild symobl"
exit 10
#输入无效的运算符
fi
else
#如果test1 test2不为空,说明num1和num2不是数值
echo "please enter a vald value"
#提示输入有效的数值
exit 11
fi
fi
echo " $num1 $ope $num2 = $sum "
#输出数值运算的结果