Logical judgment in the shell, if judgment file, directory attribute, some special usage of if judgment
Source: Internet
Author: User
logical judgment in the shell
Format 1: if condition; then statement; fi // If the condition is satisfied, then execute the statement
[[email protected] shell] # cat if1.sh
#! / bin / bash
a = 5
if [$ a -gt 3]
then
echo ok
fi
[[email protected] shell] # sh -x if1.sh
+ a = 5
+ ‘[‘ 5 -gt 3 ‘]’
+ echo ok
ok
Format 2: if condition; then statement; else statement; fi // If the condition is satisfied, then execute the statement, otherwise execute the else statement
[[email protected] shell] # cat if2.sh
#! / bin / bash
a = 1
if [$ a -gt 3]
then
echo ok
else
echo nook
fi
[[email protected] shell] # sh -x if2.sh
+ a = 1
+ ‘[‘ 1 -gt 3 ‘]’
+ echo nook
nook
Format 3: if…; then…; elif…; then…; else…; fi // If the condition is satisfied, the statement is executed, otherwise the elif condition is continued and the statement is executed. elif condition can write multiple
[[email protected] shell] # cat if3.sh
#! / bin / bash
a = 1
if [$ a -gt 3]
then
echo ok
elif [$ a -lt 3]
then
echo "less than 3"
else
echo nook
fi
[[email protected] shell] # sh -x if3.sh
+ a = 1
+ ‘[‘ 1 -gt 3 ‘]’
+ ‘[‘ 1 -lt 3 ‘]’
+ echo $ ‘\ 345 \ 260 \ 217 \ 344 \ 272 \ 2163’
Less than 3
Logical judgment expression: if [$ a -gt $ b]; if [$ a -lt 5]; if [$ b -eq 10]. -gt (>); -lt (<); -ge (> =); -le (<=); -eq (==); -ne (! =) Note that there are spaces everywhere. Can also be written as if ((a> b)), but this is not standard
[[email protected] shell] # cat if1.sh
#! / bin / bash
a = 5
b = 1
if ((a> b))
then
echo ok
fi
[[email protected] shell] # sh -x if1.sh
a = 5
b = 1
((a> b))
echo ok
ok
You can use && || to combine multiple conditions
[[email protected] shell] # cat if1.sh
#! / bin / bash
a = 5
if [$ a -gt 3] && [$ a -lt 10]
then
echo ok
fi
[[email protected] shell] # sh -x if1.sh
+ a = 5
+ ‘[‘ 5 -gt 3 ‘]’
+ ‘[‘ 5 -lt 10 ‘]’
+ echo ok
ok
[[email protected] shell] # vim if1.sh
[[email protected] shell] # cat if1.sh
#! / bin / bash
a = 3
if [$ a -gt 3] && [$ a -lt 10]
then
echo ok
elif [$ a -gt 3] || [$ a -lt 10]
then
echo "a is greater than 3 or less than 10"
else
echo no
fi
[[email protected] shell] # sh -x if1.sh
+ a = 3
+ ‘[‘ 3 -gt 3 ‘]’
+ ‘[‘ 3 -gt 3 ‘]’
+ ‘[‘ 3 -lt 10 ‘]’
+ echo $ ‘a \ 345 \ 244 \ 247 \ 344 \ 272 \ 2163 \ 346 \ 210 \ 226 \ 350 \ 200 \ 205 \ 345 \ 260 \ 217 \ 344 \ 272 \ 21610’
a is greater than 3 or less than 10
if judges file and directory attributes
[-f file] Determine whether it is a normal file and exists
[-d file] Determine if it is a directory and exists
[-e file] Determine whether a file or directory exists
[-r file] determines whether the file is readable
[-w file] Determine if the file is writable
[-x file] Determine if the file is executable
[[email protected] shell] # cat file1.sh
#! / bin / bash
f = / tmp / akuilinux
if [-f $ f]
then
echo ok
else
touch $ f
fi
[[email protected] shell] # sh -x file1.sh
+ f = / tmp / akuilinux
+ ‘[’ -F / tmp / akuilinux ‘]’
+ touch / tmp / akuilinux
[[email protected] shell] # sh -x file1.sh
+ f = / tmp / akuilinux
+ ‘[’ -F / tmp / akuilinux ‘]’
+ echo ok
ok
It can also be used in a script like this, file1.sh and file2.sh have the same meaning.! Means reverse
[[email protected] shell] # cat file1.sh
#! / bin / bash
f = / tmp / akuilinux
if [-f $ f]
then
rm -f $ f
fi
[[email protected] shell] # cat file2.sh
#! / bin / bash
f = / tmp / akuilinux
[-f $ f] && rm -f $ f
[[email protected] shell] # cat file1.sh
#! / bin / bash
f = / tmp / akuilinux
if [! -f $ f]
then
touch $ f
fi
[[email protected] shell] # cat file2.sh
#! / bin / bash
f = / tmp / akuilinux
[-f $ f] || touch $ f
Some special uses of if judgment
if [-z "$ a"] ?? This shows what happens when the value of variable a is empty
if [-n "$ a"] means when the value of variable a is not empty
When judging whether a variable is empty, [] needs to be enclosed in double quotes. -Z and -n can only judge whether the variable is empty, not whether the file is empty.
[[email protected] shell] # cat if4.sh
#! / bin / bash
#First determine if the file exists, exit if there is no, so as to avoid bugs
if [! -f / tmp / lalal]
then
echo "/ tmp / lalal not exist."
exit
fi
n = `wc -l / tmp / lalal`
#Exit if the value is empty
if [-z "$ n"]
then
echo error
exit
#Print if there is a value
elif [-n "$ n"]
then
echo $ n
fi
[[email protected] shell] # cat / tmp / lalal
dadhaj
dahdajhj
127676
adadahj
[[email protected] shell] # sh if4.sh
4 / tmp / lalal
if grep -wq ‘user1’ 1.txt; then means what happens if 1.txt contains the line ‘user1’
-w means match a word
-q does not print what grep matches
case judgment in shell
format
case variable name in
value1)
command
;;
value2)
command
;;
*)
command
;;
esac
In case programs, you can use | in conditions to indicate or mean, such as
2 | 3)
command
;;
case
#! / bin / bash
read -p "Please input a number:" n
if [-z "$ n"]
then
echo "Please input a number."
exit 1
fi
n1 = `echo $ n | sed‘ s / [0-9] // g’`
if [-n "$ n1"]
then
echo "Please input a number."
exit 1
fi
if [$ n -lt 60] && [$ n -ge 0]
then
tag = 1
elif [$ n -ge 60] && [$ n -lt 80]
then
#! / bin / bash
#readInteract with the user and assign the value entered by the user to n
read -p "Please input a number:" n
#If n is empty, the user has not entered
if [-z "$ n"]
then
echo "Please input a number."
exit 1
fi
#Determine if the input is a number, prompt and exit if it is not a number
n1 = `echo $ n | sed‘ s / [0-9] // g’`
if [-n "$ n1"]
then
echo "Please input a number."
exit 1
fi
#If it is a number, if it is less than 60, mark it, if it is greater than 60 and less than 80, mark 2 and so on
if [$ n -lt 60] && [$ n -ge 0]
then
#Determine if the input is a number, prompt and exit if it is not a number
n1 = `echo $ n | sed‘ s / [0-9] // g’`
if [-n "$ n1"]
then
echo "Please input a number."
exit 1
fi
#If it is a number, if it is less than 60, mark it, if it is greater than 60 and less than 80, mark 2 and so on
if [$ n -lt 60] && [$ n -ge 0]
then
tag = 1
elif [$ n -ge 60] && [$ n -lt 80]
then
tag = 2
elif [$ n -ge 80] && [$ n -lt 90]
then
tag = 3
elif [$ n -ge 90] && [$ n -le 100]
then
tag = 4
else
tag = 0
fi
#Use case to judge the mark and give the result
case $ tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number range is 0-100."
;;
esac
Logical judgment in the shell, if to judge file and directory attributes, some special uses of the if judgment
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.