http://blog.chinaunix.net/uid-26827001-id-3154024.html
First, I saw a lot of #! / Bin / bash that start with this. This is a shell program that tells the system to interpret this script file.
# This is a comment line, when the line is valid
Script programs are generally represented by xx.sh files, so you can see the properties of the files in the same way.
After a script is written, if you want to execute it, you must first assign + x the execution permission to it
For example: chmod + x xx.sh, then execute with the command ./filename
Here are some points of knowledge:
1. echo: Used for display (without parameters, if there are multiple lines, it will output new lines) For example: echo "hello shell"
If the parameter -n is added, if there are multiple lines, the output will not be wrapped.
Click (here) to collapse or open
#! / bin / bash
# filename is while.sh
count = 0
while (($ count <5))
do
echo -n "count =" $ count
let count = count + 1
done
By the way, Linux provides 2 commands: one is an internal command, and the other is an external command. Interpreting and executing internal commands is much faster than external commands. At the same time, when interpreting internal commands, the shell does not need to create child processes, and external commands need to create child processes, which increases the system overhead. To see if it is an internal command, you can use the following command: type command For example: type echo
2. $ xx refers to the variable: as in the above code, the third line defines the variable count, the fourth line, and the sixth line $ count refers to the variable.
3. Position variable: $ 0 file name, $ 1 first parameter $ 2 second parameter
Click (here) to collapse or open
#! / bin / bash
#filename is first.sh
#have 3 parameter to first.sh
#command is: ./first.sh 1 2 3
# $ #: Number of parameters passed to the script
# $ *: Show all parameters, separated by spaces
# $ 0: The command itself, the name of the file to be executed
# $ 1: first parameter
# $ 2: second parameter
# $ 3: The third parameter is the same as the latter ~ wait
echo "number of vars:" $ #
echo "var of vars:" $ 0
echo "first var:" $ 1
echo "second var: $ 2
echo "third var: $ 3
4. let: Used to perform arithmetic operations and numerical expression tests.
The alternative representation of the let command is: ((arithmetic expression)). The advantage of this is that if the expression contains special characters, the shell can be notified for special processing: for example, let "val = a | b" If you do not enclose it, the shell will treat the "|" in the command line let val = a | b as a pipe character, and treat the left and right sides as different commands, so it cannot be executed correctly.
The following code executes the result: 1 3 7 10
Click (here) to collapse or open
#! / bin / bash
#filename is let.sh
i = 1
echo "i =" $ i
let i = i + 2
echo "i =" $ i
let "i = i + 4"
echo "i =" $ i
((i + = 3))
echo "i =" $ i
5. Function call: Declare function function name () {~~~~}, call function, just write function name name.
Click (here) to collapse or open
#! / bin / bash
# filename is second.sh
# Function declaration and call
function fun1 () {
local a = "hello world"
echo "Function call succeeded" $ a
}
echo "before function call"
fun1
echo "End of function call"
6. Types of variables in the shell: local and global variables
A local variable is a variable declared locally in a function: its scope is from the beginning of the function call to the end of the function.
Global variables Variables declared under a shell script whose scope is within the entire life cycle of the shell. The shell ends and the period of the global variable ends.
Click (here) to collapse or open
#! / bin / bash
#filename is localvar.sh
var1 = "hello shell"
function fun () {
#Define local variables
local var2 = "hello local variable"
echo "Use a local variable in the function var2 =" $ var2
echo "call global variable 1 var1 =" $ var1
}
fun
echo "Call a local variable outside the function var2 =" $ var2
echo "call global variable 2 var1 =" $ var1
Supplement: environment variables, position variables, standard variables, special variables, etc.
Environment variables: used for all user processes, can be seen as environment variables under windows, defined with export: such as
export LOGNAME = "fjsm20", you can view system environment variables with env or export
Position variable: It is the parameter when calling the function, for example: ./first.sh 11 22 33 As the third knowledge point above
Standard variables: scalars of some standard environments created by the bash environment. He will automatically parse them. Basically, EXINIT, HOME, IFS, LOGNAME, MAIL, MAILCHECK, etc. are defined in / etc / profile (refer to online information)
Special variables: $ # number of parameters, $ * lists all parameters, $! Shows the process ID of the script acquired, $$ shows the process id as the script, $? The status of the last command exit, 0 means normal;
Shell script let, echo variables