1. Linux Scripting Basics
1.1 Basic Introduction to grammar
1.1.1 Opening
The program must start with the following line (must be in the first line of the file):
#!/bin/sh
The symbol #! is used to tell the system that the parameter behind it is the program used to execute the file. In this example we use/BIN/SH to execute the program.
When you edit a script, you must also make it executable if you want to execute the script.
To make the script executable:
Compile chmod +x filename so you can use./filename to run
1.1.2 Notes
In shell programming, a sentence that begins with # represents a comment until the end of the line. We sincerely recommend that you use annotations in your programs.
If you use annotations, you can understand the script in a short amount of time, even if you haven't used it for a long time.
and working principle.
1.1.3 Variable
In other programming languages, you must use variables. In shell programming, all variables are made up of strings, and you don't need to
Be declared. To assign a value to a variable, you can write:
#!/bin/sh
#对变量赋值:
A= "Hello World"
# now print the contents of variable a:
echo "A is:"
Echo $a
Sometimes variable names are easily confused with other words, such as:
num=2
echo "The $numnd"
This does not print out "This is the 2nd" and simply prints "This is the" because the shell will go to search for the value of the variable numnd,
But there is no value for this variable. You can use curly braces to tell the shell that we're printing num variables:
num=2
echo "This is the ${num}nd"
This will print: This is the 2nd
1.1.4 Environment variables
Variables processed by the EXPORT keyword are called environment variables. We do not discuss environment variables, as it is usually only in the case of login
Environment variables are used in the script.
1.1.5 shell command and Process Control
There are three types of commands that can be used in shell scripts:
1) Unix command:
Although you can use any Unix command in a shell script, there are some relatively common commands. These commands are usually used to
For file and text manipulation.
Common command syntax and functions
echo "Some text": Print text content on the screen
LS: File list
Wc–l filewc-w filewc-c File: Count the number of words in the file count the number of characters in the file
CP sourcefile destfile: File copy
MV oldname newname: Renaming files or moving files
RM file: Deleting files
grep ' pattern ' file: Search for strings within a file such as: grep ' searchstring ' file.txt
Cut-b colnum File: Specifies the range of files to display and outputs them to a standard output device such as: output
5th to 9th characters per line cut-b5-9 file.txt never be confused with a cat command,
This is two completely different commands.
Cat file.txt: Output file contents to standard output device (screen)
File somefile: Get the files type
Read Var: Prompts the user for input and assigns the input to the variable
Sort file.txt: Sorting rows in a file.txt file
Uniq: Delete columns that appear in a text file such as: sort File.txt | Uniq
Expr: Perform mathematical operations Example:add 2 and 3expr 2 "+" 3
Find: Search for files like: Search for Find by file name. -name Filename-print
Tee: Output data to standard output devices (screens) and files such as: Somecommand | Tee outfile
basename file: Returns a file name that does not contain a path such as: Basename/bin/tux will return Tux
DirName file: Returns the path of the files such as: Dirname/bin/tux will return/bin
Head file: Print text file at the beginning of a few lines
Tail File: Print text file at the end of a few lines
Sed:sed is a basic find-and-replace program. You can read text from standard input, such as a command pipeline, and
The result is output to standard output (screen). the command is searched using a regular expression (see Reference).
Do not confuse with wildcard characters in the shell. For example: Replace the Linuxfocus with
Linuxfocus:cat Text.file | Sed ' s/linuxfocus/linuxfocus/' > Newtext.file
The Awk:awk is used to extract fields from a text file. By default, the field separator is a space, and you can use-f to specify additional delimiters.
Cat File.txt | Awk-f, ' {print $ ', ' $ i} ' here we use, as field separators, while printing
First and third fields. If the contents of the file are as follows: Adam Bor, Indiakerry Miller, USA
The command output results are: Adam Bor, Indiakerry Miller, USA
Source: http://blog.chinaunix.net/uid-25266990-id-101002.html
Full learning of Linux shell scripts (i)