shell single branch if statement

A single branch if conditional statement
if [conditional judgment];then
program
be
or
if [conditional judgment]
then
program
be
 
Two grammar analysis
1. The if statement ends with fi, which is different from the curly brackets used in general languages.
2. [Conditional judgment] is to use the test command to judge, so there must be a space between the brackets and the conditional judgment.
3. The program to be executed after the condition is followed by then can be placed after [] and separated by ";". You can also write in a newline, you don't need to use ";".
 
Three examples
#!/bin/bash
test=$(env | grep USER |cut -d "=" -f 2)
if [ "$test" == "root" ]
then
echo "you are root"
be
 
Four running results
you are root
 
Five judgments of partition utilization
#!/bin/bash
rate=$(df -h|grep "/dev/sda3"|awk '{print $5}'|cut -d "%" -f1)
if [ $rate -le 80 ]
then
echo "/dev/sda3 is not full"
be
 
Six running results
/dev/sda3 is not full

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326262966&siteId=291194637