linux 几个简单shell脚本demo

来源:互联网 发布:搜索引擎优化实战培训 编辑:程序博客网 时间:2024/05/29 13:18

linux 几个简单shell脚本demo


#!/bin/bashtest=$(env | grep USER | cut -d "=" -f 2)   if [ "$test"==root ]  then     echo "current user is root"fi    # 当前用户是



#!/bin/bashread -t 30 -p "please input a dir:" dirif [ -d "$dir" ]  then    echo "is dir"  else   echo "no no dir"fi#输入的 是否目录

#!/bin/bashtest=$(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/bashtest=$(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 startfi# 重启apace服务

#!/bin/bash#字符界面加减乘除计算器read -t 30 -p "please input num1: " num1read -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   fifiecho " $num1 $ope $num2 = $sum "#输出数值运算的结果


0 0
原创粉丝点击