Linux Learning Shell Script

Source: Internet
Author: User

[This is my own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020]

Shell Script basic elements:
#!/bin/bash     ---First line #     ---comment variable flow control structure

Look at a simple example, learn any language, I think most of the cases are starting from the HelloWorld program, the shell is a language, we also start from HelloWorld.

Create a new file named Helloworld.sh, which is written in:

helloworld.sh

#!/bin/sh# This is a very simple print "Hello World" shell script echo "Hello world!"
to execute such a simple script, we first want to give the file permission to execute. as follows:

chmod u+x helloworld.sh./helloworld.sh Hello world!
As you can see, you can print "Hello World" to the screen with such a simple script.

Shell Features:

In general, shell scripts have the following characteristics:

1) aliases

2) command substitution

3) Background processing

4) Variables

5) Piping

6) redirect

7) Pattern matching

8) Special characters

  

Here are the following:

1) Aliases

You can view aliases for the current system through alias, such as my system alias:

Alias egrep= ' Egrep--color=auto ' Alias fgrep= ' Fgrep--color=auto ' Alias grep= ' grep--color=auto ' Alias l= ' LS-CF ' Alias la= ' Ls-a ' Alias ll= ' ls-alf ' Alias ls= ' ls--color=auto '
   You can also customize aliases, such as:

[Email protected]:~$ alias lh= ' LS-LH ' [email protected]:~$ lh Total usage 40kdrwxr-xr-x 2 Jesson jesson 4.0K December 09:47 desktopdr WX------4 Jesson Jesson 4.0K December  2 20:29 developdrwxr-xr-x 8 Jesson jesson 4.0K December 11:27 inodeclientdrwxr-xr-x 2 Jesson Jesson 4.0K December  6 19:30 public drwxr-xr-x 2 Jesson Jesson 4.0K December  6 19:30 template drwxr-xr-x 2 Jesson Jesson 4.0K 12 Month  6 19:30 video drwxr-xr-x 3 Jesson jesson 4.0K December 16 14:51 picture drwxr-xr-x 9 Jesson jesson 4.0K  January 15 20:22 Document drwxr-xr- X 6 Jesson Jesson 4.0K  January 15 20:45 Download Drwxr-xr-x 2 jesson jesson 4.0K December  6 19:30 Music
Since you can customize the alias, of course, you can also cancel the alias, in fact, it is very simple, directly enter the Unalias alias.

2) command substitution

Look directly at such a command

[Email protected]:~$ ls-l ' cat/etc/shells '-rwxr-xr-x 1 root root 920788  March  2013/bin/bash-rwxr-xr-x 1 root ro OT 100284  March  2012/bin/dashlrwxrwxrwx 1 root root      4 December  6 19:10/bin/rbash-bashlrwxrwxrwx 1 root Root      4 December  6 19:10/bin/sh Dash
As you can see, the result of the command is to list the shells that exist on the current system. In fact, the execution process is this, first execute the "command, the execution results are as follows:

[Email protected]:~$ cat/etc/shells #/etc/shells:valid Login Shells/bin/sh/bin/dash/bin/bash/bin/rbash
   The execution of this command prints out the contents of the file, and then executes each line of the ls-l output, so that the details of all the shells are listed in the row, and in fact the command is replaced with the "function".

3) Background processing

    A terminal can run multiple background programs at the same time.

Usage: nohup Command &

You can use jobs to view the current background program.

4) variables

    Variables are used to store information. such as system variables Shell,path, of course, also define the variables themselves.

5) Piping

A pipeline is an input that connects the output of one command to another command. Such as:

[Email protected]:~$ ls | Sortdesktopdevelopinodeclient Public template video picture document download music
It can be noted that this is the result of the sorted output.

6) redirect

   Redirects are associated with pipelines that can change the input source and output location of the program , such as

[email protected]:~/develop/worksapce/shell_workspace$ ls-l >homefile.txt[email protected]:~/ develop/worksapce/shell_workspace$ ll Total dosage 12drwxrwxr-x 2 Jesson jesson 4096  January 00:30./drwxrwxr-x 7 Jesson Jesson 4096  January 16 00:28.. /-rw-rw-r--1 Jesson jesson  573  January 00:30 Homefile.txt[email protected]:~/develop/worksapce/shell_  workspace$ cat homefile.txt Total dosage 40drwxr-xr-x  2 Jesson jesson 4096  January 23:44 desktopdrwxrwxr-x  4 Jesson Jesson 4096  January 00:41 developdrwxr-xr-x  8 Jesson jesson 4096  January 21:51 inodeclientdrwxr-xr-x  2 Jesson Jesson 4096  January 11 21:23 public drwxr-xr-x  2 Jesson jesson 4096  January 11 21:23 template drwxr-xr-x  2 Jes Son Jesson 4096  January 11 21:23 video drwxr-xr-x  3 Jesson jesson 4096  January 14 23:26 picture drwxr-xr-x  2 Jesson JE Sson 4096  January 11 22:46 document drwxr-xr-x  5 Jesson Jesson 4096  January 16 00:26 Download Drwxr-xr-x Jesson Jesson 4096&N Bsp January 12 00:48 ToneLe [email protected]:~/develop/worksapce/shell_workspace$ sort < homefile.txt > Homefile.txt.sort[email  protected]:~/develop/worksapce/shell_workspace$ ll Total dosage 16drwxrwxr-x 2 Jesson jesson 4096  January 16 00:31. Drwxrwxr-x 7 Jesson Jesson 4096  January 16 00:28..  /-rw-rw-r--1 Jesson jesson  573  January 00:30 homefile.txt-rw-rw-r--1 jesson jesson  573  January 16 00:31 homefile.txt.sort[email protected]:~/develop/worksapce/shell_workspace$ Cat Homefile.txt.sort Drwxrwxr-x    4 Jesson Jesson 4096  January 00:41 developdrwxr-xr-x 12 Jesson jesson 4096  January 00:48 Music drwxr-xr-x  2 Jesson Jesson 4096  January 11 21:23 public drwxr-xr-x  2 Jesson jesson 4096  January 11 21:23 Template drwxr-xr-x  2 JE Sson Jesson 4096  January 11 21:23 video drwxr-xr-x  2 Jesson jesson 4096  January 11 22:46 Document drwxr-xr-x  2 Jesson J Esson 4096  January One 23:44 desktopdrwxr-xr-x  3 Jesson jesson 4096  January 14 23:26 picture drwxr-xr-x  5 Jesson JE SsOn 4096  January 16 00:26 Download drwxr-xr-x  8 Jesson jesson 4096  January 21:51 inodeclient Total consumption 40 
It is easy to see that using redirection can easily specify the input source and output of the program, as in the above example
Sort < Homefile.txt > Homefile.txt.sort
The source of the sort is the Homefile.txt file, and the output is also a file, and this is homefile.txt.sort.

7) Pattern matching

If you display a file with a txt extension or a beginning with a, this ability is called pattern matching, and in pattern matching, regular expressions are used generally.

For example:

[Email protected]:~/develop/worksapce/shell_workspace$ ls-l *.txt-rw-rw-r--1 jesson jesson 573  January 00:30 homefile . txt

8) Special characters

Double quotation marks (""): Used to make the shell unable to recognize spaces, tabs, and most other special characters, so that "Ls-l helloworld.sh" represents a value.

Single quotation mark ('): Used to make the shell unable to recognize all special characters.

Anti-quote ('): Used to replace the command.

backslash (\): Used to make the shell unable to recognize the following special characters, so that the special characters after the loss of special meaning.

Semicolon (;): Allows multiple commands to be placed on a single line.

&: Execution in the background.

Parentheses (): Create a group of commands.

Curly braces {}: Create a command block.

Vertical bar (|): Pipe designator.

<: redirect specifier.

* ? []: Pattern match.

$: The beginning of the variable name.

#: Notes

Spaces, tabs, newline characters: as blanks.

Linux Learning Shell Script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.