The first shell script program. VI create a file test. SH and grant the execution permission
#!/bin/shecho 'hello word !'
Execution result: Hello word!
Shell script structure #! Start with/bin/sh, and next to the line feed is the program body. Of course, here we only use #! /Bin/sh is used as an example.
I generally follow this process to learn a new language.
1. assign values to variables and variables
2. Process Control
3. dataset operation: Set Array
4. Class and method call
I am familiar with the above things and have the most basic knowledge of this language.
I. Variables
All variables are strings and do not need to be declared.
first='OKOKOK'
Note that there is no space between the left and right sides of the equal sign.
Reference: $ {First} or $ {First}
# Assign the first value to secondsecond = $ first
first=2echo 'this is ${first}'
Execution result: This is 2
2. Process Control
1. If judgment
if...then.....elif....then.....else....fi
Format.
Different from Java, Java conditions are wrapped with (), and shell conditions are enclosed with [] or () square brackets. Note that there must be spaces before and after square brackets.
For example:
a='1'if [ ${a} -eq '1' ]then echo 'this is 1'elif [ ${a} -eq '2' ]then echo 'this is 2'else echo 'this is other'fi
Execution result: This is 1
The above program compares whether a variable is equal to a value.
The following is the if judgment of the composite condition.
a='3'if [[ ${a} -eq '1' ]] || [[ ${a} -eq '3' ]]then echo 'this is 1'elif [ ${a} -eq '2' ]then echo 'this is 2'else echo 'this is other'fi
Execution result: this is 1
In java, if (a. equals ("1") | a. equals ("3"), but not here.
This is also possible:
if [ ${a} -eq '1' -o ${a} -eq '3' ]
-O: Yes |, or
When it comes to process judgment, expressions and logical operators are required. The next article will write the corresponding information.