Shell notes, old boy shell notes

Source: Internet
Author: User

Shell notes, old boy shell notes

#! /Bin/bashvar2 = 'var2' echo $ var2echo $ {var2} var2 = 'var2 _ 1' # The variable can be defined repeatedly, but $ var3 = 'var3' readonly is not added before the variable name. var3 # declare var3 as a read-only variable # var3 = 'var3 _ 1' here, the var3 value cannot be changed again. var4 = 'var4' unset var4 # Delete the var4 variable # echo $ {var4} You cannot call var4 again here. var5 = 'var5' var6 = "This is $ {var5}" # concatenate the string var7 = "This is" $ var5 ", too "# concatenated string var8 = 'itvar8' echo $ {# var8} # output var8 length, that is, output 6var9 = 'Hello World! 'Echo $ {var9: 1: 5} # extract 5 characters from the string's 2nd characters var10 = 'Hello World! 'Echo 'expr index "$ var10" world' # Find the position of the character World in the string. arr1 = ('var1' 'var2' 'var3 ') # define the array method 1arr2 = (# define the array method 2' var1' var2' var3 ') var3 [1] = 'var2' # assign var_arr_1 =_1 {var2 [1]} By subscript # Read array element var_arr_all =$ {var2 [@]} # Read all element arr_lend_1 = $ {# var2 [@]} # obtain the array length. arr_lend_2 =$ {# var2 [*]} # obtain the array length.

Pass parameters to the script

#! /Bin/bashecho "Name of the executed File $0"; echo "the first parameter is $1"; echo "the second parameter is $2 ";

 

Run:

$ Chmod + x shell. sh $./shell. sh 1 2. The first parameter of shell. sh is 1. The second parameter is 2.

Special parameters:

Parameter processing description $ # number of parameters passed to the script $ * display all parameters passed to the script in a single string. For example, when "$ *" is included in... $ N "to output all parameters. $ ID of the current process running the script $! The ID of the last process running in the background is the same as $ *, but each parameter is enclosed in quotation marks. For example, when "$ @" is included in "... All parameters are output in the form of "$ n. $-Display the current options used by Shell, which have the same functions as the set command. $? Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error.

  

Arithmetic Operations:

Native bash does not support simple mathematical operations, but can be implemented using other commands, such as awk and expr.

#!/bin/basha=10b=20val=`expr $a + $b`echo "a + b : $val"

 

File test operator:

-R file: whether the file is readable

-W file: indicates whether the file can be written.

-X file: indicates whether the file is executable.

File = "/workplace/shell. sh "if [-r $ file] then echo" file readable "else echo" file unreadable "fiif [-w $ file] then echo" file writeable "else echo" file not readable write "fiif [-x $ file] then echo" file executable "else echo" file not executable "fi

 

Echo command:

#! /Bin/bashread name # The read command reads a row from the standard input, specify the value of each field in the input line to the shell variable echo $ nameecho "Hello World"> myfile #. The display result is directed to the file.

 

Process control:

If Syntax:

if condition1then    command1elif condition2 then     command2else    commandNfi

For Syntax:

for var in item1 item2 ... itemNdo    command1    command2    ...    commandNdone

While Syntax:

while conditiondo    commanddone

Example:

#!/bin/shint=1while(( $int<=5 ))do        echo $int        let "int++"done

Case Syntax:

Echo: numbers between 1 and 4: 'echo 'the number you entered is: 'read aNumcase $ aNum in 1) echo' you selected 1'; 2) echo 'you selected 2'; 3) echo' you selected 3'; 4) echo 'you selected 4 ';;*) echo 'you have not entered a number between 1 and 4 '; esac

  

Function

FunWithParam () {echo "the first parameter is $1! The second parameter of "echo" is $2! "Echo": the tenth parameter is $10! "Echo": the tenth parameter is ${10 }! "Echo" 11th parameters: ${11 }! The total number of "echo" parameters is $! "Echo" outputs all parameters as a string $ *! "} FunWithParam 1 2 3 4 5 6 7 8 9 34 73

 

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.