Writing the first shell script
Like other languages, we use any kind of text editor, such as Nedit, Kedit, emacs
, vi, etc. to write our shell program. 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 make
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:
chmod +x filename
You can then execute your script by entering:./filename.
Shell variables
In other programming languages, you must use variables. In shell programming, all variables are made up of strings,
And you do not need to declare the variables.
>> Local Variables
Setting variables
Reference variable
Clear variables
View variables
>> Environment variables
Set Environment variables:
• Referencing environment variables:
• Common environment variables
–home
–path
–term
–uid
–pwd
–ps1
>> Special pre-defined variables
$#: Store the number of command line arguments in the shell program
$?: Stores the return value of the previous program execution in the shell (0 indicates the life
Make execution successful, not 0 problematic)
$ A: Store the Shell's own name
$[1-N]: Store [1-n] command line arguments
$*: Stores all parameters of the shell script (does not contain $)
$$: Process number (PID) for storing shell scripts
$!: Store the process number (PID) of the previous background execution command
Learn from a simple test.sh script:
Execute script:
#./test.sh aa bb number:2scname:./test02.shfirst:aasecond:bbargume:aa bb
By displaying the results you can see:
$# is the number of arguments passed to the script
The name of the script itself
$ $ is the first parameter passed to the shell script
$ $ is the second parameter passed to the shell script
[Email protected] is a list of all parameters passed to the script
Common command syntax
echo "Some text": Print text content on the screen
ls: File list
wc-l filewc-w filewc-c file&: calculating the number of words in a file calculation file
the number of characters
CP sourcefile destfile&: File copy
MV oldname newname: Renaming files or moving files
RM file&: Deleting files
grep ' pattern ' file&: Searching for strings within a file such as: grep ' searchstring '
file.txt
cut-b colnum file&: Specifies the range of file content to be displayed and outputs them to the standard output device
For example: Output the 5th to 9th character of each line cut-b5-9 file.txt do not confuse the cat command, which is two
a completely different order.
cat file.txt: Output file contents to standard output device (screen)
file Somefile&: To get Files of 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 by file name for find.-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 file 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. Text can be read from standard input (such as command pipeline)
and outputs the results to the standard output (screen). the command is searched using a regular expression (see Reference). No
to be confused with wildcard characters in the shell. For example: Replace the Linuxfocus with the Linuxfocus:cat
text.file | sed ' s/linuxfocus/linuxfocus/' > Newtext.file
Process Control and judgment
"if" expression if the condition is true then perform the following part:
elif ...; then
....
else
fi
usually uses "[]" to represent conditional tests. Note that the space here is important. The space to ensure the square brackets.
[-F "somefile"]: Determine if it is a file
[- X "/bin/ls"]: Determine if/bin/ls exists and has executable permissions
Span style= "font-size:15px;" > ["$a" = "$b"]: Determines whether $ A and $b are equal
Function
This is what a function looks like.
:
functionname ()
{
# inside the body argument given to the function
# The second ...
Body
}
Typical programming steps
Any good script should have help and input parameters.
How to Debug
The simplest debug command, of course, is to use the echo command.
You can use Echo to print any variable value in any place where you suspect it is wrong.
The shell also has a real debug mode. If there is an error in the script "Strangescript", you can
Sample to be debugged:
Sh-x Strangescript
This executes the script and displays the values of all variables.
The shell also has a pattern that does not need to execute a script just to check the syntax. This can be used:
Sh-n Your_script
This will return all syntax errors.
attached:
The meaning of- Z to-D in the shell in terms of the IF
[-A file] is true if file exists.
[-B file] True if file exists and is a block special file.
[-C file] True if file exists and is a word special.
[-D file] True if file exists and is a directory.
[-E File] True if file exists.
[-F file] True if file exists and is a normal file.
[-G file] True if file exists and Sgid has been set.
[-H file] True if file exists and is a symbolic connection.
[-K file] True if file exists and the sticky bit has been set.
[-P file] If file exists and is a name pipe (f if O) is true.
[-R File] True if file exists and is readable.
[-S file] True if file exists and the size is not 0.
[-T FD] true if the file descriptor FD is open and points to a terminal.
[-u file] True if file exists and suid (set user ID) is set.
[-W file] True if file exists and is writable.
[-X file] True if file exists and is executable.
[-o file] True if file exists and is a valid user ID.
[-G file] True if file exists and is a valid user group.
[-L file] True if file exists and is a symbolic connection.
[-N file] If file exists and has been mod if IED since it is last read is true.
[-S file] True if file exists and is a socket.
Getting Started with Shell scripting