Linux shell scripts and linuxshell scripts
[This article is my own learning notes. You are welcome to repost it, but please note the Source: http://blog.csdn.net/jesson20121020]
This section describes the basics and operators of Shell scripts. Let's take a look at the input and output of Shell scripts.
Shell Input and Output commands:
Echo
Read
Cat
MPs queue
Tee
Exec
Shell Input and Output concepts:
File redirection
Standard input, output, and error
Merge standard output and standard errors
Use file descriptor
Next, let's look at the usage of each command:
Input and Output command usage: 1. echo
This command can be used to display text lines or variables, or input strings to files.
Usage:
Echo [option] string
--E parse escape characters
--N: Do not use line breaks. By default, line breaks are used in linux.
-Escape Character (\ c (carriage return without line feed), \ f (static), \ t (tab), \ n (carriage return with line feed ))
Example:
Echotest. sh
#!/bin/bash#echotestecho -e "we are\f testing echo\tcommand\n\n"echo -n "we are testing echo command"echo "OK"echoecho "we are testing echo command\n\n"echo "output this string to file">echo_output.txt
Grant executable permissions and execute the script as follows:
jesson@jesson-HP:~/develop/workspace/shell_workspace$ chmod a+rx echotest.sh jesson@jesson-HP:~/develop/workspace/shell_workspace$ ./echotest.sh we are testing echocommandwe are testing echo commandOKwe are testing echo command\n\n
2. read
The read command can read information from a line of text on the keyboard or file and assign it to a variable.
Usage:
Read var1 var2...
-If only one variable is specified, read assigns all input to the variable until the first filename terminator or carriage return occurs. If multiple variables are provided, they are assigned different variables in order. Shell uses space as the separator between variables.
Example:
Readtest. sh
#!/bin/bash#readtestecho -n "First Name:"read firstnameecho -n "Last Name:"read lastnameecho -e "Your first name is:$firstname"echo -e "Your last name is:$lastname"echo -n "province and city:"read province cityecho -e "Your address provice:${province}"echo -e "Your address city:${city}"
Grant permissions to execute the script:
jesson@jesson-HP:~/develop/workspace/shell_workspace$ chmod a+rx readtest.sh jesson@jesson-HP:~/develop/workspace/shell_workspace$ ./readtest.sh First Name:SteveLast Name:JobsYour first name is:SteveYour last name is:Jobsprovince and city:California San FranciscoYour address provice:CaliforniaYour address city:San Francisco
3. cat
Cat is a simple and common command that can be used to display the file content, create a file, and display control characters.
Usage:
--V: display control characters
Example:
Jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat helloworld. sh #! /Bin/sh # This is a simple shell script that prints "hello world" echo "hello world! "Jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat helloworld. sh searchfile. sh #! /Bin/sh # This is a simple shell script that prints "hello world" echo "hello world! "#! /Bin/bash # searchfilefind.-name $1-printjesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat helloworld. sh searchfile. sh> shelltest. bakjesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat shelltest. bak #! /Bin/sh # This is a simple shell script that prints "hello world" echo "hello world! "#! /Bin/bash # searchfilefind.-name $1-print
Note that the cat command displays all input content. If you need to display the content by page, you can use the pipeline | and more or less commands.
4. MPS queue |
The output of one command can be passed to another command as input through the pipeline.
Usage:
Format: command1 | command2
Example:
Jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ ls-l total usage 32-rw-rw-r -- 1 jesson 25 Aug 17 20:16 cattest. sh-rw-r -- 1 jesson 27 January 22 19:50 echo_output.txt-rwxrwxr-x 1 jesson 209 January 22 19:50 echotest. sh-rwxrw-r -- 1 jesson 94 January 15 21:20 helloworld. sh-rwxrw-r -- 1 jesson 637 January 17 17:07 parm. sh-rwxrwxr-x 1 jesson 299 January 22 20:10 readtest. sh-rwxr-xr-x 1 jesson 47 August January 17 20:18 searchfile. sh-rw-r -- 1 jesson 141 January 22 shelltest. bakjesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ ls-l | grep parm. sh-rwxrw-r -- 1 jesson 637 January 17 17:07 parm. shjesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ df-kdf: "/root /. gvfs ": insufficient permissions File System 1 K-block used available % mount point/dev/sda9 29525076 12915484 15109792 47%/udev 2031604 4 2031600 1%/devtmpfs 816612 860 815752 1%/runnone 5120 0 5120 0% /run/locknone 2041528 792 2040736 1%/run/shm/dev/sda10 49135432 28230216 18409268 61%/home/dev/sda1 73400952 43260784 30140168 59%/media/ipv6a15000514ec/dev/sda5 138424040 123913684 14510 356 90%/media/00010A370001FCE2/dev/sda6 56504040 44425044 12078996 79%/media/000E2D72000188C0/dev/sda7 138134868 32924076 105210792 24%/media/0008eeff1_4113jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ df-k | awk '{print $1} 'df: "/root /. gvfs ": File System with insufficient permissions/dev/sda9udevtmpfsnonenone/dev/sda10/dev/sda1/dev/sda5/dev/sda6/dev/sda7jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ df-k | awk '{print $1}' | grep-v "File System" df: "/root /. gvfs ": insufficient permissions/dev/sda9udevtmpfsnonenone/dev/sda10/dev/sda1/dev/sda5/dev/sda6/dev/sda7jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ df-k | awk '{print $1}' | grep-v "File System" | grep devdf: "/root /. gvfs ": insufficient permissions/dev/sda9udev/dev/sda10/dev/sda1/dev/sda5/dev/sda6/dev/sda7
5. tee
The tee command delivers a copy of the output to the standard output, and the other copy is copied to the corresponding file.
Usage:
Tee-a files
-If you want to save the output to a file at the same time, this command is the most suitable.-a indicates the meaning of append.
-It is generally used after an MPS queue.
Example:
Jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ ll total usage 40drwxrwxr-x 2 jesson 4096 January 22 20:18. /drwx ------ 17 jesson 4096 January 21 17:53 .. /-rw-r -- 1 jesson 25 Aug 17 20:16 cattest. sh-rw-r -- 1 jesson 27 January 22 19:50 echo_output.txt-rwxrwxr-x 1 jesson 209 January 22 19:50 echotest. sh *-rwxrw-r -- 1 jesson 94 January 15 21:20 helloworld. sh *-rwxrw-r -- 1 jesson 637 January 17 17:07 Parm. sh *-rwxrwxr-x 1 jesson 299 January 22 20:10 readtest. sh *-rwxr-xr-x 1 jesson 47 January 17 15:21 searchfile. sh *-rw-r -- 1 jesson 141 January 22 20:18 shelltest. bakjesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ who | tee who. outjesson tty7 2015-01-17 12: 34 jesson pts/0 2015-01-21 (: 0) jesson pts/2 2015-01-21 07 (: 0) jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ ll total usage 44drwxrwxr-x 2 jesson 4096 January 22 20:33. /drwx ------ 17 jesson 4096 January 21 17:53 .. /-rw-r -- 1 jesson 25 Aug 17 20:16 cattest. sh-rw-r -- 1 jesson 27 January 22 19:50 echo_output.txt-rwxrwxr-x 1 jesson 209 January 22 19:50 echotest. sh *-rwxrw-r -- 1 jesson 94 January 15 21:20 helloworld. sh *-rwxrw-r -- 1 jesson 637 January 17 17:07 Parm. sh *-rwxrwxr-x 1 jesson 299 January 22 20:10 readtest. sh *-rwxr-xr-x 1 jesson 47 January 17 15:21 searchfile. sh *-rw-r -- 1 jesson 141 January 22 20:18 shelltest. bak-rw-r -- 1 jesson 127 January 22 20:33 who. outjesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat who. out jesson tty7 2015-01-17 12: 34 jesson pts/0 2015-01-21 (: 0) jesson pts/2 2015-01-21 07 (: 0) jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ who | tee-a who. out jesson tty7 2015-01-17 12: 34 jesson pts/0 2015-01-21 (: 0) jesson pts/2 2015-01-21 07 (: 0) jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat who. out jesson tty7 2015-01-17 12: 34 jesson pts/0 2015-01-21 (: 0) jesson pts/2 2015-01-21 (: 0) jesson tty7 12: 34 jesson pts/0 2015-01-21 (: 0) jesson pts/2 2015-01-21 (: 0) jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ who | tee who. out jesson tty7 2015-01-17 12: 34 jesson pts/0 2015-01-21 (: 0) jesson pts/2 2015-01-21 07 (: 0) jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat who. out jesson tty7 2015-01-17 12: 34 jesson pts/0 2015-01-21 (: 0) jesson pts/2 (: 0)
5. exec
The exec command can be used to replace the current shell. In other words, the shell is not started. Using this command in any existing environment will be cleared and a shell will be restarted.
Usage:
Exec command
-The command is usually a shell script.
When operating the file descriptor (or only at this time), it will not overwrite your current shell.
Basic concepts of input and output: 1. Standard input, output, and error
When executing commands in shell, each process is associated with three open files and uses file descriptors to reference these files. Because file descriptors are not easy to remember, shell also provides the corresponding file name.
File |
File descriptor |
Input File-standard input |
0 (screen or file by default) |
Output file-standard output |
1 (the default is the keyboard, or the output of files or other commands) |
Error output file-standard error |
2 (screen or file by default) |
2. File redirection
Change the Input Source and output location of the program.
Usage:
Command> filename |
Redirects the standard output to a new file. |
Command> filename |
Redirects the standard output to a file (append) |
Command 1> filename |
Redirects the standard output to a file. |
Command> filename 2> & 1 |
Redirects standard output and standard errors to a file. |
Command 2> filename |
Redirects a standard error to a file. |
Command 2> filename |
Redirects a standard error to a file (append) |
Command> filename 2> & 1 |
Redirects the standard output along with the standard error to a file (append) |
Command <filename> filename 2 |
The Command uses the filename file as the standard input and the filename2 file as the standard output. |
Command <filename |
The Command uses the filename file as the standard input. |
Command <delimiter |
Read from standard input until delimiter demarcation is encountered |
Command <& m |
Use file descriptor m as standard input |
Command> & m |
Redirects the standard output to the file descriptor m. |
Command <&- |
Disable Standard Input |
Example of standard input: Command <delimiter
jesson@jesson-HP:~/develop/workspace/shell_workspace$ cat >>term.txt <<EOF> Hello,I am learning shell> and I am using a $TERM termianl> bye...> EOFjesson@jesson-HP:~/develop/workspace/shell_workspace$ cat term.txt Hello,I am learning shelland I am using a xterm termianlbye...
Example of standard error output:
Jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ ls-l myfilels: unable to access myfile: the file or directory does not exist. jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ ls-l myfile 2> ls_error.outjesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat ls_error.out ls: unable to access myfile: there is no such file or directory. jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ ls-l myfiile 2>/dev/nulljesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat/dev/null jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $
3. Combined use of standard output and standard errors
Jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat account_new.txt account_old.txt 1> account. out 2> acount. errjesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat account_new.txt jessoncherryrootjesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat account. out maid @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat account_old.txtcat: account_old.txt: the file or directory does not exist. jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat acount. err cat: account_old.txt: no such file or directory
4. Merge standard output and standard errors
When merging standard output and standard errors, remember that shell analyzes the corresponding commands from left to right.
Jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ grep "jesson" account. out> grep. out 2> & 1jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat grep. out maid @ jesson-HP :~ /Develop/workspace/shell_workspace $ grep "jesson" account1.out> grep. out 2> & 1jesson @ jesson-HP :~ /Develop/workspace/shell_workspace $ cat grep. out grep: account1.out: no such file or directory
5. file descriptor
3-9 file descriptor (0--12 is occupied by the system, and 3-9 is available for us)
Combined Use of exec and file descriptor:
File_desc.sh
#!/bin/bash#file_desc.shexec 3<&0 0<name.txtread line1read line2exec 0<&3echo $line1echo $line2
Name.txt
jessoncherry
Run the script with executable permissions:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx file_desc.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./file_desc.sh jessoncherr
It can be seen that exec does not overwrite the current shell when operating the file descriptor.
The above are some basic commands and concepts of Shell input and output.