The judgment statement in the Linux shell

Source: Internet
Author: User

Remember that if and the [] are whitespace

1. String judgment

STR1 = str2 When two strings have the same content, length is true
Str1! = str2 True when string str1 and str2 are not equal
-N str1 True when the length of the string is greater than 0 (string non-null)
-Z str1 True when the string length is 0 (empty string)
STR1 is True when string str1 is not empty

2, the number of the judgment

Int1-eq int2 Two numbers equal to True
Int1-ne Int2 Two number is true
INT1-GT Int2 int1 greater than Int2 is true
Int1-ge Int2 int1 greater than or equal to Int2 true
Int1-lt Int2 int1 Less than Int2 is true
Int1-le Int2 int1 less than or equal to Int2 true

3 Judgment of the document

-r file user readable as True
-W file user can write as true
-X file user can execute as true
-F file is true for regular files
-d file files are directory-True
-C File file is true for character special files
-B file files are true for block special files
-S file files non-0 o'clock True
-T file is true when the specified device is terminal (default = 1)

3. Complex logic judgment

-A and
-O or
! Non -

Here are some examples of usages:

#!/bin/sh
Mypath= "/var/log/httpd/"
Myfile= "/var/log/httpd/access.log"

#这里的-x parameter determines whether $mypath exists and has executable permissions
if [!-X "$myPath"]; Then
mkdir "$myPath"
Fi

#这里的-D parameter to determine if $mypath exists
if [!-D "$myPath"]; Then
mkdir "$myPath"
Fi

#这里的-F parameter to determine if $myfile exists
if [!-F "$myFile"]; Then
Touch "$myFile"
Fi

#其他参数还有-n,-n is to determine whether a variable has a value
if [!-n "$myVar"]; Then
echo "$myVar is empty"
Exit 0
Fi

#两个变量判断是否相等
If ["$var 1" = = "$var 2"]; Then
Echo ' $var 1 eq $var 2 '
Else
Echo ' $var 1 not EQ $var 2 '
Fi


If List Then
Do something here
Elif List Then
Do another thing here
Else
Do something else here
Fi

EX1:

#!/bin/sh

System= ' uname-s ' #获取操作系统类型, I'm local Linux

if [$SYSTEM = "Linux"]; Then #如果是linux的话打印linux字符串
echo "Linux"
elif [$SYSTEM = "FreeBSD"]; Then
echo "FreeBSD"
elif [$SYSTEM = "Solaris"]; Then
echo "Solaris"
Else
echo "What?"
Fi #ifend

Basically the same as other scripting languages. There's no big difference. But it's worth noting that. [] inside the condition to judge.

=========================================================================

#. $HOME/.bash_profile
CD $HOME
Sh. bash_profile
CD $HOME/jnjzapp/sos_dx/
Echo-e ' Start '
./jn_card_dx.sh
Wait
#sqlplus $DBSTR <<start_a
# @jn_card_dx. sql
#/
#commit;
#exit;
#START_A
CD $HOME/jnjzapp/sos_dx/data/
if [!-D bqmsg]; Then
mkdir bqmsg
Fi
#mkdir bqmsg
CD $HOME/jnjzapp/sos_dx/
Sqlplus $DBSTR <<start
@sos. sql;
Commit
Exit
START
Objdir= ' date-d ' Days ' +%y%m%d '
Echo $objdir
#cp jn_card_sos.txt $objdir. txt

The judgment statement in the Linux shell

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.