Build basic script
When creating a shell script, you must specify the shell to be used in the first line of the file. Its format is:
#!/bin/bash
environment variable
Example:
#!/bin/bash #display user information from the system echo "User info for userid: $USER" echo UID: $UID echo HOME: $HOME
User variable
The user variable can be any text string composed of letters, numbers or underscores, with a length of no more than 20. User variables are case sensitive
Example:
#!/bin/bash #testing variales days=10 guest="Katie" echo "$guest checked in $days days ago" days=5 guest="Jessica" echo "$guest checked in $days days ago"
The dollar sign is required when referencing a variable, but not when assigning a value
Example:
#!/bin/bash #assigning a variable value to another variable value1=10 value2=$value1 echo The resulting value is $value2
Command replacement
Enclose the entire command with backquotes ` or $()
Example:
#!/bin/bash testing=$(date) #testing=`date` echo "The date and time are: " $testing
Example:
#!/bin/bash #copy the /usr/bin directory listing to a log file today=$(date +%y%m%d) ls /usr/bin -al > log.$today
Perform mathematical operations
expr command
Operator | describe |
---|---|
ARG1 | ARG2 | If ARG1 is neither null nor zero, return ARG1; Otherwise, return to ARG2 |
ARG1 & ARG2 | If no parameter is null or zero, return ARG1; Otherwise, return 0 |
ARG1 < ARG2 | If ARG1 is less than ARG2, return 1, otherwise return 0 |
ARG1 <= ARG2 | If ARG1 is less than or equal to ARG2, return 1, otherwise return 0 |
ARG1 = ARG2 | If ARG1 is equal to ARG2, return 1, otherwise return 0 |
ARG1 != ARG2 | If ARG1 is not equal to ARG2, return 1; Otherwise, return 0 |
ARG1 >= ARG2 | If ARG1 is greater than or equal to ARG2, return 1; Otherwise, return 0 |
ARG1 > ARG2 | If ARG1 is greater than ARG2, return 1; Otherwise, return 0 |
ARG1 + ARG2 | Returns the arithmetic sum of ARG1 and ARG2 |
ARG1 - ARG2 | Returns the arithmetic difference between ARG1 and ARG2 |
ARG1 * ARG2 | Returns the arithmetic product of ARG1 and ARG2 |
ARG1 / ARG2 | Returns the arithmetic quotient of ARG1 divided by ARG2 |
ARG1 % ARG2 | Returns the arithmetic remainder of ARG1 divided by ARG2 |
Example:
#!/bin/bash #An example of using the expr command var1=10 var2=20 var3=$(expr $var2 / $var1) echo The result is $var3
Use square brackets
In bash, when a mathematical operation result symbol is given to a variable, the mathematical expression can be enclosed by dollar sign and square brackets $[operation]
Example:
#!/bin/bash var1=100 var2=50 var3=45 var4=$[$var1*($var2-$var3)] echo The final result is $var4
bash shell mathematical operators only support integer operations
Example:
#!/bin/bash var1=100 var2=45 var3=$[$var1/$var2] echo The final result is $var3
Floating point solution
bc bash calculator
Example:
varriable=$(echo "options;expression" | bc)
scale is reserved for decimal places
Example:
#!/bin/bash var1=$(echo "scale=4;3.44/5"|bc) echo The answer is $var1
Example:
#!/bin/bash var1=100 var2=45 var3=$(echo "scale=4;$var1/$var2"|bc) echo The answer for this is $var3
Example:
#!/bin/bash var1=20 var2=3.14159 var3=$(echo "scale=4;$var1*$var1"|bc) var4=$(echo "scale=4;$var3*$var2"|bc) echo The final result is $var4
The above method can solve the shorter operation, and the longer operation can be solved by the following methods:
variable=$(bc<<EOF options statements expressions EOF )
The EOF text string identifies the start and end of inline redirection data
Example:
#!/bin/bash var1=10.46 var2=43.67 var3=33.2 var4=71 var5=$(bc<<EOF scale=4 a1=($var1*$var2) b1=($var3*$var4) a1+b1 EOF ) echo The final answer for this mess is $var5
Exit script
The exit status code of a successful command is 0. If there is an error at the end of a command, the exit status code is a positive number
LINUX exit status code
Status code | describe |
---|---|
0 | The command ended successfully |
1 | General unknown error |
2 | Unsuitable shell command |
126 | The command is not executable |
127 | No command found |
128 | Invalid exit parameter |
128+x | Serious error related to linux signal x |
130 | Command terminated by ctrl+c |
255 | Exit status code outside the normal range |
Returns the specified exit code
Example:
#!/bin/bash #testing the exit status var1=10 var2=30 var3=$[$var1+$var2] echo The answer is $var3 exit 5
exit returns the variables used in the parameters of the command
Example:
#!/bin/bash var1=10 var2=30 var3=$[$var1+$var2] exit $var3
The maximum return value can only be 255. If it exceeds the range of 0-255, the modular operation result will be obtained
Example:
#!/bin/bash var1=10 var2=30 var3=$[$var1*$var2] exit $var3