Shell script if condition summary

Original link: https://geniuspeng.github.io/2018/03/12/shell-if/

In shell programming, if...then is a common control flow command, and the conditional judgment of if generally uses the built-in command test. In fact, for the convenience of writing, most of them use [], [] as synonyms for test. Here are some summary records of if conditions ~

binary comparison operators, comparing variables or comparing numbers. Note the difference between numbers and strings.

The basic syntax of if:

if [ command ];then
statements that meet the conditions to execute
elif [ command ];then statements that meet the conditions to execute else
statements that meet the conditions to execute fi


File/folder (directory) judgment

[ -b FILE ] True if FILE exists and is a block special file.
[ -c FILE ] True if FILE exists and is a word special file.
[ -d DIR ] True if FILE exists and is a directory.
[ -e FILE ] True if FILE exists.
[ -f FILE ] True if FILE exists and is a regular file.
[ -g FILE ] True if FILE exists and the SGID has been set.
[ -k FILE ] True if FILE exists and the sticky bit has been set.
[ -p FILE ] True if FILE exists and is a named pipe (F if O).
[ -r FILE ] True if FILE exists and is readable.
[ -s FILE ] True if FILE exists and size is not 0.
[ -t FD ] True if file descriptor FD is open and points to a terminal.
[ -u FILE ] True if FILE exists and SUID (set user ID) is set.
[ -w FILE ] True if FILE exists and is writable.
[ -x FILE ] True if FILE exists and is executable.
[ -O FILE ] True if FILE exists and is a valid user ID.
[ -G FILE ] True if FILE exists and belongs to a valid user group.
[ -L FILE ] True if FILE exists and is a symbolic link.
[ -N FILE ] True if FILE exists and has been mod if ied since it was last read.
[ -S FILE ] True if FILE exists and is a socket.
[ FILE1 -nt FILE2 ] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ] True if FILE1 is older than FILE2, or if FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 point to the same device and node number.

String judgment

[ -z STRING ] True if the length of STRING is zero, that is, to judge whether it is empty, and empty is true;
[ -n STRING ] If the length of STRING is non-zero, it is true, that is, to judge whether it is not empty, not empty that is true;
[ STRING1 = STRING2 ] true if the two strings are the same;
[ STRING1 != STRING2 ] true if the strings are not identical;
[ STRING1 ] true if the strings are not empty, same as -n similar

Numerical judgment

INT1 -eq INT2 INT1 and INT2 are equal to true, =
INT1 -ne INT2 INT1 and INT2 are not equal to true, <>
INT1 -gt INT2 INT1 is greater than INT1 is true, >
INT1 -ge INT2 INT1 is greater than or equal to INT2 is True, >=
INT1 -lt INT2 True if INT1 is less than INT2, <
INT1 -le INT2 True if INT1 is less than or equal to INT2, <=

complex logical judgment

-a and
-o or
! not

exp1: 如果a>b且a
if (( a > b )) && (( a < c ))
或者
if [[ $a > $b ]] && [[ $a < $c ]]
或者
if [ $a -gt $b -a $a -lt $c ]

exp2: if a>b or a
if (( a > b )) || (( a < c ))
or
if [[ $a > $b ]] || [[ $a < $c ]]
or
if [ $a -gt $b -o $a -lt $c ]

"||" and "&&" can be used in SHELL, that is, the first one is written as if [ a>b && a

Welfare at the end of the sentence:

Benefit 1: A collection of 10G resources such as front-end, Java, product manager, WeChat applet, Python, etc. will be released :

Welfare 2: A full set of detailed video tutorials for WeChat mini-program introduction and actual combat.


【How to receive】

Pay attention to [Programming Micro Journal] WeChat public account:

Reply to [Mini Program Demo] One-click to receive 130 Wechat Mini Program source code demo resources.

Reply [Receive resources] One-click to receive front-end, Java, product manager, WeChat applet, Python and other resource collections 10G resources to be released.


Guess you like

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