2018-4-18 17 weeks 1 lessons Shell logic judgment, File directory attribute judgment, if, case

Source: Internet
Author: User
Tags readable


20.5 logical Judgments in shell scripts









• format 1:if conditions; Then statement; Fi



Example: a=5



If [$a-gt 3]; then echo OK; Fi





[Email protected] shell]# sh If1.shok





• format 2:if conditions; Then statement; else statement; Fi



Example: a=5



If [$a-gt 3]; then echo OK; else Echo Nook; Fi





[[email protected] shell]# sh if1.sh
ok
[[email protected] shell]# sh if1.sh
not ok
[[email protected] shell]# sh -x if1.sh
+ a=2
+ '[' 2 -gt 3 ']'
+ echo not ok
not ok





• Format 3:if ...; Then ...; Elif ...; Then ...; else ...; Fi



The first condition, how it is, the second condition, how; Otherwise, other conditions





[[email protected] shell]# sh -x if1.sh
+ a=3
+ '[' 3 -gt 4 ']'
+ '[' 3 -lt 6 ']'
+ echo '<6 && >1'
<6 && >1







[[email protected] shell]# sh -x if1.sh
+ a=5
+ '[' 5 -lt 4 ']'
+ '[' 5 -gt 6 ']'
+ echo not ok
not ok





• Logical judgment expression:



If [$a-gt $b]; -GT (>), greater Than



If [$a-lt 5]; -lt (<); less than



If [$b-eq 10] etc-eq (= =);



-le (<=);-ge (>=); -ne (! =) Note that there are spaces everywhere



• If you want to use > <, you can use (())



[[Email protected] shell]# if (($a >1)) then echo Ok;fi



Ok



• can use && | | Combine multiple conditions



if [$a-gt 5] && [$a-lt 10]; Then and



if [$b-gt 5] | | [$b-lt 3]; Then or













20.6 File Directory property judgment






· [-F file] Determines if it is a normal file, and there is



· [-D file] Determines if it is a directory and exists



· [-E file] to determine whether files or directories exist



· [-R File] to determine if the document is readable



· [-W file] Determines whether the file is writable



· [-X file] Determines whether the file is executable



· [-F file] Determines if it is a normal file, and there is








[[email protected] ~] # sh -x file1.sh
+ f = / tmp / alex
+ '[' -f / tmp / alex ']' ## Exists and is a file
+ touch / tmp / alex ## Create if not present
[[email protected] ~] # sh -x file1.sh
+ f = / tmp / alex
+ '[’-F / tmp / alex'] '## Is it a file?
+ echo / tmp / alex exist ##
/ tmp / alex exist





· [-D file] Determines if it is a directory and exists





[[email protected] ~]# sh -x file2.sh
+ f=/tmp/alex
+ '[' -d /tmp/alex ']'
+ touch /tmp/alex
[[email protected] ~]# sh -x file2.sh
+ f=/tmp/alex
+ '[' -d /tmp/alex ']'
+ touch /tmp/alex





· [-E file] to determine whether files or directories exist





[[email protected] ~]# sh -x file2.sh
+ f=/tmp/alex
+ '[' -e /tmp/alex ']'
+ echo /tmp/alex exist
/tmp/alex exist





· [-R File] to determine if the document is readable





[Email protected] shell]# sh file2.sh/tmp/alex readable





· [-W file] Determines whether the file is writable





[Email protected] shell]# sh File2.sh/tmp/alex writeable





· [-X file] Determines whether the file is executable





[[email protected] ~]# sh file2.sh
[[email protected] ~]# ll /tmp/alex
-rw-r--r-- 1 root root 0 April  18 21:21 /tmp/alex
#!/bin/bash
f="/tmp/alex"
if [ -f $f ]
then
rm -f $f
fi


You can use the If judgment to write


#!/bin/bash
f="/tmp/alex"
[ -f $f ] && rm -f $f


#!/bin/bash
f="/tmp/alex"
if [ -f $f ]
then
rm -f $f
else
touch $f
fi


The original script is preferable to write back as


#!/bin/bash
f="/tmp/alex"
if [ ! -f $f ]
then
touch $f
fi


You can use the If judgment to write





#!/bin/bash
f="/tmp/alex"
[ -f $f ] || touch $f








20.7 If special usage





if [-Z "$a"] This indicates what happens when the value of variable A is empty (the variable is quoted, the file is not quoted)





can be optimized to




[[email protected] shell] # sh -x file3.sh
++ wc -l / tmp / jojo
wc: / tmp / jojo: no such file or directory
+ n =
+ '[' -z '' ']'
+ echo not exist
not exist


Can be re-optimized to




[[email protected] shell] # sh -x file3.sh
++ wc -l / tmp / jojo
wc: / tmp / jojo: no such file or directory
+ n =
+ '[' -f ']'
+ echo 'not exist.'
not exist.
+ exit





if [-N "$a"] means that when the value of variable A is not empty


[[email protected] shell]# echo $b
[[email protected] shell]# if [  -n 01.sh ];then echo ok;fi
ok
[[email protected] shell]# if [  -n "$b" ];then echo "$b";else echo "b is null";fi
b is null





if Grep-q ' 123 ' 1.txt; Then what happens if the 1.txt contains a ' 123 ' row


[[email protected] shell] # if grep -w "user1" / etc / passwd; then echo "user1 exist"; fi
user1: x: 1011: 1011 :: / home / user1: / bin / bash
user1 exist
[[email protected] shell] # if grep -wq "user1" / etc / passwd; then echo "user1 exist"; fi
user1 exist ## grep -q silent output
[[email protected] shell] # if! grep -wq "user2" / etc / passwd; then useradd user2; fi





IF [!-e file]; Then what happens when the file doesn't exist?






if (($a <1)); Equivalent to if [$a-lt 1]; Then ...



· Symbols such as <,>,==,!=,>=,<= cannot be used in []













20.8/20.9 Case Judgment








• Format: Case variable name in



value1)



Command



;;



value2)



Command



;;



*)



Commond



;;



Esac






:: Indicates the end of a judgment, into the next judgment






• In a case program, you can use |, meaning, or means in a condition, such as



2|3)



Command



;;



· *) In addition to all of the above






Shell Script case


#! / bin / bash
read -p "Please input a number:" n ## Read input from standard input and assign value to variable n
if [-z "$ n"]
then
echo "Please input a number."
exit 1 ## 1 is the prompt when exiting
fi

n1 = `echo $ n | sed 's / [0-9] // g'` ## Replace the number from the variable n with empty. If n1 is not empty, it is not a pure number, then enter a number and drop out
if [-n "$ n1"]
then
echo "Please input a number."
exit 1
fi

if [$ n -lt 60] && [$ n -ge 0] ## Pure numbers continue to judge
then
tag = 1 ## Tag tag
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
case $ tag in
1)
echo "Fail"
;;
2)
echo "Pass"
;;
3 | 4)
echo "excellent"
;;
*)
echo "The number range is 0-100."
;;
esac





• Perform view results:





[[email protected] shell] # sh -x case.sh
+ read -p 'Please input a number:' n
Please input a number: 77
+ '[' -z 77 ']'
++ echo 77
++ sed 's / [0-9] // g'
+ n1 =
+ '[' -n '' ']'
+ '[' 77 -lt 60 ']'
+ '[' 77 -ge 60 ']'
+ '[' 77 -lt 80 ']'
+ tag = 2
+ case $ tag in
+ echo $ '\ 345 \ 217 \ 212 \ 346 \ 240 \ 274'
Pass
[[email protected] shell] # sh -x case.sh
+ read -p 'Please input a number:' n
Please input a number: 8sdafasdf ## Enter an impure number
+ '[' -z 8sdafasdf ']'
++ echo 8sdafasdf
++ sed 's / [0-9] // g'
+ n1 = sdafasdf
+ '[' -n sdafasdf ']'
+ echo 'Please input a number.'
Please input a number.
+ exit 1
[[email protected] shell] # sh -x case.sh
+ read -p 'Please input a number:' n
Please input a number: 123 ## Enter a number greater than 100
+ '[' -z 123 ']'
++ echo 123
++ sed 's / [0-9] // g'
+ n1 =
+ '[' -n '' ']'
+ '[' 123 -lt 60 ']'
+ '[' 123 -ge 60 ']'
+ '[' 123 -lt 80 ']'
+ '[' 123 -ge 80 ']'
+ '[' 123 -lt 90 ']'
+ '[' 123 -ge 90 ']'
+ '[' 123 -le 100 ']'
+ tag = 0
+ case $ tag in
+ echo 'The number range is 0-100.'
The number range is 0-100. 




2018-4-18 17 weeks 1 lessons Shell logic judgment, File directory attribute judgment, if, case


Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.