Shell Scripts
Shell is a command-line interpreter, its role is to interpreted user command, the user inputs a command, performing a Shell interpretation, this is called interactive (Interactive).
Shell also a way to execute a command called batch (Batch), a user in advance to write a Shell script (Script), of which there are many commands, so once these Shell command executed, one by one without having to type commands. Shell scripting and programming languages are very similar, there are variables, and flow control statements, including loops and branches. But Shell scripts are interpreted, not need to compile, Shell program to read and execute the commands from the script line by line, which is equivalent to a knock user commands in the script line by line to the next Shell prompt execution. As a programming language, although it is not part of the Linux kernel, but it calls most of the functionality of the system kernel to execute programs, create documents and in a parallel fashion coordinator to run each program.
shell script execution is an interpreted language, batch processing language, greatly save the cost of the work
The first line must shell script with #! At the beginning, it indicates that the script uses the back of the interpreter interpreted variables
Such as:! # / Bin / bash
1.1 Definitions variable
myName = "white"
String myName = "white"
Note that there is a space between the variable name and the equal sign, which may be familiar to you and all programming languages are not the same. Meanwhile, the name of the variable name must follow these rules:
The first character must be a letter (az, AZ).
excluding space, you can use an underscore (_).
You can not use punctuation.
Use variable: $ variable name in order to distinguish, we can use $ {variable name}
1.2 Delete variables
Use the unset command to remove variables. grammar:
unset variable_name
Can not be used again after the variable is deleted.
Example:
#!/bin/bash
myUrl="http://www.baidu.com"
unset myUrl
echo $myUrl
2.Shell string
String shell programming is the most common and useful type of data (except numbers and strings, but also other types of handy lacks), the string enclosed in single quotes, double quotes may be used, it may be practiced without quotes.
apostrophe
str = 'this is a string' is output as it
Single quoted strings limits:
Any characters in single quotes are output as a single quotation mark in a string variable is invalid;
Single quotes string single quotes (single quotes nor escape after use) can not occur.
Double quotes
your_name='qinjx'
str="Hello, I know your are \"$your_name\"!" 转移符
Double quotes advantages:
Double quotes can have variable
Double quotes can appear escape character
3. Operator
The following table lists common arithmetic operators, 10 is assumed as a variable, the variable b is 20:
Operators |
Explanation |
For example |
+ |
addition |
`Expr $ a + $ b` 30 results. , May be a numeric string |
- |
Subtraction |
`Expr $ a - $ b` result is -10. |
* |
multiplication |
`Expr $ a \ * $ b` result is 200. |
/ |
division |
`Expr $ b / $ a` 2 results. |
% |
Remainder |
`Expr $ b% $ a` result is 0. |
= |
Assignment |
$ a = b value will be assigned to the variable b a. |
== |
equal. For comparing the two figures, the same returns true. |
[$ A == $ b] returns false. |
!= |
not equal. Used to compare two numbers, the same is not true returns. |
[$ A! = $ B] returns true. |
-gt |
Equivalent to> |
[$ A -gt $ b] return false |
-lt |
Equivalent to less than |
[$ A -lt $ b] Returns true |
! |
non- |
[! True] returns false |
-O |
或 or |
[True -o false] Returns true |
-a |
And and |
[True -a false] returns false |
-give |
greater or equal to |
|
-the |
Less than or equal |
|
Note: The conditional expression to be placed between square brackets, and have a space, for example: [== $ A $ B] is wrong and must be written as [A == $ B $] .
Operator except that in this way may also be expr
#!bin/bash
a=10
b=20
echo $ ((($ a $ + b) * $ a))
#!/bin/bash
a=10
b=20
c=$(($a+$b))
echo $ c
((d=$a+$b))
echo $d
f = `expr $ a + $ b` (trans quotes)
echo $f
4.for cycle
Commands between do and done called loop
For example:
1. The first
#!bin/bash
for i in {1..10} 1到10
do
echo haha
done
2. The second
for file in `ls /etc`; do
echo $file
done
3. The third
#!bin/bash
for i in $(ls /test)
do
echo $i
done
4. The fourth
#!bin/bash
for((i=0;i<20;i++))
do
echo $i
done
5. Since growth
1. The first
for i in {1..10}
do
echo $i
let "i+=1"
Done
2. The second
#!/bin/bash
for i in {1..10}
do
echo $i
((i++))
done
6.while cycle
For example:
#!/bin/bash
sum=1
i=0
while(( i <= 2 ))
do
let "sum*=10"
let "i+=1"
done
echo "sum=$sum"
6.read keyboard input
scanner
receiving an input read command standard input (keypad), or enter another file descriptor. After obtaining input, read command data into a standard variable.
1. Read keyboard input
#! /bin/bash
Please read -p Enter the first number: first
Please read -p Enter the second number: second
echo $(($first+$second))
3. Read the contents of the documents
while read line
do
arr[$i]=$line
let i+=1
done < /test/a.txt
3. Read the contents of the remote login will be used between the EOF
while read line
do
echo $line
done << EOF
hehe
haha
EOF
7. array
Definition array list
Mode 1: arr = ()
Mode 2: arr = (value1 value2 value3) (with the value in this way)
Note: to add value to the array, the array length of the automatic growth
Get array length: $ {# arr [*]}
Iterate
#!/bin/bash
arr=(value value1 value2)
arr [0] = arr [1] = 2
arr[2]=3
arr[3]=4
for i in ${arr[*]}; do
echo $i
done
8. conditional statements
1.if else
#!/bin/bash
a=1
b=2
if [ $a -gt $b ]
then
echo "a is greater than b"
else
echo "a is less than b"
be
2.if elif
#!/bin/bash
if [ 1 -gt 2 ];then
echo "1>2"
elif [ 2 -gt 1 ];then
echo "2>1"
else
echo "What the hell"
be
If [-f fileName] to determine whether the document file
If [-d fileName] determine whether the folder dir
9. Note:
Single line: #
Multi-line: For example:
#!/bin/bash
:<<!
echo "hehe"
echo "aaaa"
!
echo "haha"
#!/bin/bash
: '(: And' a space between)
echo "hehe"
echo "aaaa"
'
echo "haha"
10.$0,$1,$#
$ 0 for the name of the file itself, you enter what is on display
$ 1 for the first parameter
$ # Represents the number of parameters
$? Return value of a command on
If the last command executed successfully: return ture; false otherwise
0 for success
1 for failure