Syntax knowledge for shell Scripts 2

Source: Internet
Author: User

Numerical calculation of ①shell

$[[space] $a [space]operator[space] $b [space]]

The calculation operator and its operands must be enclosed in square brackets [], followed by $ as a leader, and [space] means that spaces are dispensable here.

Eg: ' A=2;b=3;echo $[$a + $b] ' #屏幕输出 5

② interaction with the user when the script is run

' Read X '

When the program runs here, it pauses and prompts the user for a value, and when the user enters and returns, the program assigns the value entered by the user to x,x as any variable name.

Eg: ' Read x '

' Read-p ' Please enter a number: ' Y '

When the program runs here, the screen prints "Please enter a number:" and waits for user input, after the user enters and returns, the program assigns the value entered by the user to Y,y as any variable name

③ parameters of the script run

'./test.sh ferir 24 '

Scripts can be executed to add parameters to the script, the first parameter is Ferir, in the script is obtained, the second parameter is 24, in the script is obtained with the number of arguments, the parameters are infinite, in the script through the $+ parameter bits to get

Note: $ A script name

④if-elif-elif-else-fi

If-fi

' if ($a >); then echo $a; fi ' or ' if [$a-gt];then echo $a; Fi '

If-else-fi

' if ($a =), then echo $a; else echo 30;fi ' or ' if [$a-eq];then echo $a; else echo 30;fi '

If-elif-elif-else-fi

' if ($a >) && (($a =)); then echo $a; elif (($a > 20)) | | ($a =); then echo $[$a +1];else echo ' Sorry '; Fi '

Note: If you enclose the condition in square brackets [], there must be a space between the if and the [,] operand and [,]
> * * * *-gt = * *-EQ >= * * *-ge <= * *-le < * * * * * *-LT

If Judge document properties

' If [-defrwx filename] '- D exists && directory - e exists - f exists && normal file - r readable -w Writable- x executable

⑤case $a in value1) command;; value2) command;; VALUE3) command;; *) command; Esac

' Read-p ' please input a number: ' A;

Case $[$a%2] in

0)

echo "The number is even!";;

1)

echo "The number is odd!";;

*)

echo "This is impossible!";;

Esac '

Syntax knowledge for shell Scripts 2

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.