Linux shell Scripting--Using structured commands (ii)

Source: Internet
Author: User

In a structured command, the most basic type is the If-then statement if Commandthen Commandfibash the Shell's if statement executes the IF line definition of the command, and if the exit status code of the command is 0, then the command in the then section executes. If the exit status code for the command defined by the if line is other, then the command in the then section will not be executed, and the If-then statement cannot test condition code that is independent of the exit status code of the command 2-1
[Email protected]:/data# cat demo1 #!/bin/bashif datethen        echo "Hello World" fi[email protected]:/data#./demo1 Fri De C  2 05:26:27 CST 2016hello World

  

If you deliberately place a command that cannot be executed, then the then code 2-2 is not entered
[Email protected]:/data# cat Demo2 #!/bin/bashif aaathen        echo "Hello World" Fi[email protected]:/data#./demo2./ Demo2:line 2:aaa:command not found

  

If-then-else Code 2-3
[Email protected]:/data# cat Demo3 #!/bin/bashif aaathen        echo "Hello World" Else        echo "Hello java" fi[email protected]:/data#./demo3./demo3:line 2:aaa:command not Foundhello Java

  

Nested if,else is optional, if all conditions are not met, then enter ElseIf Command1thencommand set 1elif command2thencommand set 2elif Command3thencommand set 3elsecommand Set 4FI code 2-4
[Email protected]:/data# cat Demo4 #!/bin/bashif aaathen        echo "Hello World" elif whothen        echo "Hello java" else
   echo "Hello Linux" Fi[email protected]ian:/data#./demo4/demo4:line 2:aaa:command not foundroot     pts/0        2016- 12-02 04:39 (122.91.222.126) Hello Java

  

The test command can determine 3 types of conditions (note: [Condition]condition must have spaces on both sides):
    • Numerical comparison
    • string comparison
    • File comparison
if test CONDITIONTHENCOMMANDSFI or if[condition]THENCOMMANDSFI
Number comparison
Comparison Describe
N1-eq N2 Check if N1 is equal to N2
N1-ge N2 Check if N1 is greater than or equal to N2
N1-GT N2 Check if N1 is greater than N2
N1-le N2 Check if N1 is less than or equal to N2
N1-lt N2 Check if N1 is less than N2
N1-ne N2 Check if N1 is not equal to N2
Code 2-5
[Email protected]:/data# cat Demo3 #!/bin/bashval1=10val2=20if [$val 1-gt $val 2]then        echo "The test value $val 1 is g Reater than $val 2 "elif [$val 1-lt $val 2]then        echo" The test value $val 1 is less than $val 2 "Else        echo" the test Value $val 1 is equal $val 2  "Fi[email protected]:/data#./demo3 The test value was less than 20

  

string comparison
Comparison Describe
STR1 = str2 Check if STR1 is the same as str2
Str1! = str2 Check if STR1 is different from str2
str1 > STR2 is str1 bigger than str2?
STR1 < STR2 STR1 is smaller than str2
-N str1 Check that the length of the str1 is not 0
-Z str2 Check if the length of the str1 is 0
Code 2-6
[Email protected]:/data# cat Demo4 #!/bin/bashtestuser= $USERbadUser =tomif [$USER = $testUser]then        echo "Hello $USER  "Fiif [$USER = $badUser]then        echo" Hello $USER "Else        echo" USER is $USER "Fi[email protected]:/data#./demo4 Hello Rootuser is root

  

String OrderIt becomes tedious to test whether a string is larger than another string:
    • Greater than less than symbols must be escaped, or the shell will treat them as a redirect symbol and string values as filenames
    • is greater than less than the order and sort commands are used differently
Code 2-7
[Email protected]:/data# cat demo5 #!/bin/bashval1= "Hello" val2= "Hello" if [$val 1 \> $val 2]then        echo "The result is $val 1 greater than $val 2 "elif [$val 1 \< $val 2]then        echo" The result is $val 1 less than $val 2 "fi[email protected] :/data#./demo5 The result is Hello greater than Hello[email protected]:/data# cat demo6 #!/bin/bashval1= "Hello" val2= "worl D "If [$val 1 \> $val 2]then        echo" The result is $val 1 greater than $val 2 "elif [$val 1 \< $val 2]then        echo" T He result is $val 1 less than $val 2 "Fi[email protected]:/data#./demo6 The result was hello less than world

  

Sort and test handle strings instead, sort by default ascending, sort with uppercase letters greater than lowercase, and test with lowercase letters greater than uppercase code 2-8

  

String size
[Email protected]:/data# cat Demo8 #!/bin/bashval1= "testing" val2= "" if [-n $val 1]then        echo "The string ' $val 1 ' is not Empty "Else        echo" The string ' $val 1 ' is empty "fiif [-N $val 2]then        echo" The string ' $val 2 ' is not empty "else
   echo "The string ' $val 2 ' is empty" fiif [-N $val 3]then        echo "The string ' $val 3 ' is not empty" else        echo "the St Ring ' $val 3 ' is empty "fi[email protected]:/data#./demo8 the string ' testing ' isn't emptythe string ' is not emptythe s Tring ' is not empty

In code 2-8:

If [-n $val 1] Determines whether the length of the VAL1 variable is 0, and it is exactly 0, then the then part is executed if [-Z $val 2] Determines whether the val2 variable length is 0, and it is exactly the length of 0, so the then part is executed if [-Z $val 3] to determine Val 3 variable length is 0, this variable is not defined in the shell script, so the string length is still not 0, so then part of the execution   
File comparison
Comparison Describe
-D File Check if file exists and is a directory
-E File Check if file exists
-F File Checks if file exists and is a file
-R File Check if file exists and is readable
-S file Check if file exists not empty
-W File Check if file exists and can be written
-X File Checks if file exists and executes
-O File Checks if file exists and is owned by the current user
-G file Checks if file exists and the default group is the same as the current user
File1-nt File2 Check if File1 is newer than file2
File1-ot File2 Check if File1 is older than file2
Check CatalogCode 2-8
[Email protected]:/data# cat Demo9 #/bin/bashif [-D $HOME]then        echo "Your HOME directory exists"        CD $HOME        ls -aelse        echo "There is a problem with your home directory" Fi[email protected]:/data#./demo9 Your home directory exist S ...  . Bash_history  . bashrc.  cache  . hivehistory  . Pip  . Profile  . pydistutils.cfg  . Rediscli_history  . scala_history.  selected_editor.  spark_history  .  vim  . Viminfo

  

Check if the file is emptyCode 2-9
[Email protected]:/data# cat demo1 #!/bin/bashfile= "text" Touch $fileif [-S $file]then        echo "The $file file exists an d have data in it "else        echoes" The $file exists and is empty "Fidate > $fileif [-S $file]then        echo" The $file fil e exists and have data in it "else        echoes" The $file exists and is empty "Fi[email protected]:/data#./demo1 the text exist S and is emptythe text file exists and have data in it

  

Use Boolean logic to combine tests:
    • [Condition1] && [Condition2]
    • [Condition1] | | [Condition2]
Code 2-10
[Email protected]:/data# cat Demo2 #!/bin/bashif [-D $HOME] && [-w $HOME/testing]then        echo "The file exist s and you can write it "else        echoes" I cannot write to the file "Fi[email protected]:/data#./demo2 I cannot write to the File

  

Use double parentheses to put an advanced mathematical expression into the comparison ((expression))
Symbol Describe
val++ Post-Increase
val-- Post-subtraction
++val First increase
--val First minus
! Logical negation
- Bit negation
** Power operation
<< Bit left shift
>> Bit right Shift
& Bit Boolean and
| Bit Boolean or
&& Logic and
|| Logical OR
Code 2-11
[Email protected]:/data# cat Demo3 #!/bin/bashval1=10if (($val 1 * * 2 >) then        ((val2= $val 1 * * 2)]        echo "The sq Uare of $val 1 is $val 2 "Fi[email protected]:/data#./demo3 the square of 100

  

use both brackets[[Expression]] provides another feature not provided by the test command--pattern matching code 2-12
[Email protected]:/data# cat Demo4 #!/bin/bashif [[$USER =r*]]then        echo "Hello $USER" Else        echo "Sorry. I don't know You "Fi[email protected]:/data#./demo4 Hello Root

  

The case command examines multiple values of a single variable column table format, the symbol ";;" Similar to the C language break, but also a bit different from the C language, C language allows no break, until the end of the match, and Shell script case matching if there is no ";;" This symbol will error case variable INPATTERN1 | PATTERN2) Command1;; PATTERN3) Command2;; *) default commands;; Code 2-13
[Email protected]:/data# cat Demo5 #!/bin/bashcase $USER inroot| TOM)        echo "Welcome $USER"        echo "please enjoy your visit";; testing)        echo "Special testing Account";; Jessica)        echo "Do not forget-log off when you ' re-done";; *)        echo "Sorry. You aren't allowed here ";; Esac[email protected]:/data#./demo5 Welcome Rootplease Enjoy your visit

  

Linux shell Scripting--Using structured commands (ii)

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.