1 if statement
The format of the IF statement:
If condition then command fi
If condition then command else command fi
If condition then command elif condition then command else command fi
Example:
Determines whether the current user is root, and if so, performs/home/test directory compression
#! /bin/bash
If ["$ (id-u)"-eq "0"];then
Tar czf/home/test.tar.gz/home/test
Fi
Require the user to enter a password and determine if the password is correct
#! /bin/bash
Read-p "Enter a passwd:" passwd
If ["$passwd" = = "123456"];then
echo "ok!"
Else
echo "error! Please input your passwd again "
Fi
Judging the score, less than 60 shows a failure, greater than 60 less than 70 display generally, greater than 70 less than 85 shows good, greater than 85 excellent
#! /bin/bash
if [$1-ge];then
echo "Excellent"
elif [$1-ge];then
echo "Good"
elif [$1-ge];then
echo "General"
Else
echo "Failed"
Fi
Case statement
It is a replacement solution for multiple if statements and is easy to read and write
Format 1:
Case $ variable name in
Mode 1)
Command
;;
Mode 2)
Command
;;
Mode 3)
Command
;;
*)
Esac
Format 2:
Case $ variable name in
Mode 1| Mode 2)
Command
;;
Mode 3| Mode 4)
Command
;;
Mode 5| mode 6)
Command
;;
*)
Esac
Example:
Process/var/log log directory, backup only Wednesday Friday data, delete Tuesday Thursday data
#/bin/bash
date=$ (Date +%a)
time=$ (Date +%y+%m+%d)
Case DATE in
wed| Fri)
tar-czf/home/log/${time}_log_tar.gz/var/log/& >/dev/null
;;
tue| Thu)
Rm-rf/var/log/*.2016-*.log
;;
*)
Esac
Determine whether the input is a number or a letter
#/bin/bash
Case $ in
[A-z] | [A-z])
echo "You have type a character"
;;
[[:d Igit:]])
echo "This is a number"
;;
*)
Esac
This article is from the "Practical Linux knowledge and Skills sharing" blog, please be sure to keep this source http://superleedo.blog.51cto.com/12164670/1889263
Shell Judgment Statement--instance interpretation