shell script: script learning examples (a)

Direct display of the contents of the script, the results are no longer listed.

1. determines whether a directory exists, if there is a new directory (5)

#!/bin/bash
read -p "请输入一个目录:" dir
if      [ -d $dir ]
then
        echo "目录已存在"
else
        echo "目录不存在,正在创建"
        mkdir -p $dir
        echo "创建完成"
fi

2. script to determine the root partition utilization rate exceeds 80%, if more than 80% of the alert administrators

#!/bin/bash
disk=`df -h | awk 'NR==2 {print $5}' | cut -d% -f 1`
if      [ $disk -gt 80 ]
then
        echo    "警告!!内存使用率已超过80%"
fi

3. Using the positional parameter calculation add two numbers, subtraction, multiplication, and division

#!/bin/bash
var1=$(expr $1 + $2)
echo    "相加等于$var1"
var2=$(expr $1 - $2)
echo    "相减等于$var2"
var3=$(expr $1 \* $2)
echo    "相乘等于$var3"
var4=$(expr $1 % $2)
echo    "相除等于$var4"

4. Enter a specified IP and determines whether the survival of the IP, if there is why, if it is not how to survive

#!/bin/bash
read -p "请输入一个ip:" ip
ping -c 1 $ip   &>/dev/null
if [ $? -eq 0 ]
then
        echo    "ip主机存活"
else
        echo    "ip主机未存活,请尽快查看主机情况"
fi

5. Install the httpd service by local yum, whether written script detects httpd service is running (5)

#!/bin/bash
yum     install -y httpd        &>/dev/null
systemctl       start httpd     &>/dev/null
netstat -anpt | grep httpd      &>/dev/null
if      [ $? -eq 0 ]
then
        echo    "httpd已经启动"
else
        echo    "httpd没有运行"
fi

6. Enter a specified fraction, a result of this determination, if the score is greater than 60 outputs, a result output is less than 60

#!/bin/bash
read -p "请输入一个数:" sum
if      [ $sum -ge 60 ]
then
      echo    "你及格了"
else
      echo    "没及格"
fi
Published 68 original articles · won praise 8 · views 5717

Guess you like

Origin blog.csdn.net/qq_42534026/article/details/104480364