Script Programming of BASH Shell (1)

Source: Internet
Author: User

I. Super tools

Terminals, xterms, and shells shell are a full-featured programming environment. The background knowledge is as follows.
DEC launched a small PDP-11, known as mini) and a low price of 10000 $), caused a huge response in the university. For ease of transplantation, in 1969, Ken Thompson began to write the first line of code that became opposite to Unix and MULTICS. Later, Dennis Ritchie designed a new programming language for the new operating system --
C language. Although Unix is less efficient than the original operating system, it has three outstanding advantages:
1. can be transplanted to any other machine
2. The C language greatly simplifies programming.
3. free.
Terminal terminals)
Unix is an operating system that can run on many machines, but how do people use these machines? They connect to these machines through a dumb terminal, that is, a machine consisting of a keyboard, a display, and enough electronics) to connect to a central computer. On these terminals, you can press the character teletypy ),
This is the string 'tty 'indicating the terminal device file and the name of the 'getty' command. To avoid confusion in the keyboard layout, people have created a capability file containing all the features of different terminals. This is 'termcap'. For more information, see '/etc/termcap '. Most linux terminals use 'vt100' or 'linux 'as the terminal type.
Xterms generated the X Window System in the early 1980s S. XFree86 was created in the early 1990s S. One of the major advantages of X Window is that it can run multiple virtual terminals. Even in X Window, there is such an application-'xtermin '. You will find that 'xterminal' and 'virtual Terminal' are the same in many cases. Other terminal simulators, such as rxvt, konsole, aterm, eterm, and wterm. A terminal simulator, also known as a virtual terminal, is connected to the system through a pseudo (pseudo) tty device-pty, and uses its own display standard-xterm. As a result, different terminal simulators may have small differences in some buttons or programs, depending on the extent to which the simulator complies with the 'xtermin' display standard.
Shells shell is an interface program between users and linux or, more accurately, between you and linux kernel. Each command you enter at the prompt is explained by the shell and then transmitted to the Linux kernel. To run a program on a terminal, shell is required. Shell is a part of the operating system, used to deal with users, and can be used to coordinate various commands. The first real Unix shell-'sh', also known as the 'Bourne shell', was born in 1975 by Steve Bourne. Soon, other shells emerged, such as the 'K' and 'zsh' based on the original 'Bourne shell'. The latter is often used as the standard shell in the exclusive Unixes system; there are also shell derived from the C language, such as 'csh' or 'tcsh '. In linux, the labeled shell is 'bash ', that is, the GNU Bourne-Again Shell. This shell is very powerful, and the compressed man page has 50 KB.
Shell startup
First, we recommend that you do not use the 'root' account to run shell in common applications. If you are a newbie, pay special attention to this. As a common user, you cannot damage the system either intentionally or unintentionally. However, if you are 'root', it is different. If you press a few letters, it may lead to disastrous consequences.
When you log on to the system or open an xterm window, you first see the prompt (prompt ). The standard prompt in Red Hat linux includes your user name, the host name you have logged on to, if not set, it is 'localhost'), the current directory (working directory), and the prompt symbol:
[Tom @ belbo tom] $
I log on to the host named 'describe' with the username 'Tom ', which is currently in my home directory --'/home/tom.
'Root' prompt:
[Root @ belbo root] #
Except for different user names, the prompt symbol is changed from '$' '#'. According to the tradition of the Bourne shell, the normal user's prompt ends with '$', while the super user uses '#'. Each part of the prompt can be customized. You will learn more later.
To run the command, you only need to enter the command at the prompt and press the key. Shell will see in its path for details) Search for this command, find it, run it, and output the corresponding results in the terminal, if any), after the command ends, and then a new prompt:
[Tom @ belbo tom] $ whoami
Tom
[Tom @ belbo tom] $
When you press ENTER, it doesn't matter where the cursor (cursor) is, because the shell always reads the whole line.
Basic commands include: 'LS' list directory, list directory content), 'cp' copy, copy), 'mv 'move/rename, move/rename ), 'cd' change directory, change directory), these commands can be followed by a bunch of Optional options, man page has a detailed introduction to man ls, man mv, etc ).
The following describes terminology ).
The command may contain some Optional options) and the arguments parameter): mv-I file dir
'-I' is an optional option for the command 'mv ', while 'file' and 'dir' are parameters. All options are described in detail on the man page of the command. In this example, man mv is used. The parameter is provided by you. Optional determines how the command works, while parameters are used to determine the purpose of the command.
Ii. Automatic completion/command line history/edit command line/available Shell shortcuts

Unix and later linux) were born under the command line. Therefore, the command line in Unix has many very practical functions.
Auto-completion
How can I use 'cd' to change the directory and change directory) to quickly jump from your current home directory to '/usr/src/redhat?
Cd/usrr
This is called 'automatic command line completion', which is indispensable in common applications. Let's take a closer look at this example:
Cd/u
Expanded to cd/usr. The following cd/usr extension is cd/usr/src /.
If you have only typed cd/us, the matching 'CD/u */s * 'under'/usr' will be listed for you to choose from: '/usr/sbin','/usr/shares', and '/usr/src '.
Therefore, keys can be easily used to find matching files or subdirectories based on the first few letters. For example, ls/usr/bin/zip will list all files or subdirectories starting with 'zip' under '/usr/bin. Of course, there are more powerful commands to complete such tasks, but this method does work. In addition, it is especially convenient to encounter long file names. If you want to install an RPM package named 'boomshakalakwhizbang-4.6.4.5-i586.rpm ', enter rpm-I boom. If no other files in the directory can match, then the shell will automatically help complete the configuration.
Cd/usrl
Expand to cd/usr/src/linux, and wait for it to continue. '/Usr/src' has two matched directories:'/usr/src/linux-[...] 'and'/usr/src/linux '. How do you tell shell the one you want? As long as there is a slash/, slash), you can select the one next.
If you are not sure whether it is '/usr/src/linux/documentation' or'/usr/src/linux/documentation '. As you know, linux is case sensitive. If you have carefully read the previous sections, you can use: cd/usrl/d.
Extended to '/usr/src/linux/drivers/', so it should be 'documentation' (uppercase 'D ').
This complement pair command is also valid:
[Tom @ belbo tom] $ gre
Grecord grefer grep
[Tom @ belbo tom] $ gre
Here, shell will list all known commands starting with the string 'G.
Command Line history
By pressing the up arrow key, you can traverse the commands recently entered in the console. You can use the downward arrow key to traverse the forward. If you use the SHIFT key, you can also traverse the previous output in the console. You can also edit the old command and run it again.
Then, shell enters the "reverse-I (ncremental)-search" Backward incremental search) mode. Enter the first letter of the command you are looking:
(Reverse-I-search) '':. Hitting 'I' may change to :( reverse-I-search) 'I': isdnctrl hangup ippp0
If you press the key again, the above command will be executed again. If you press the right or left arrow key, the above command will return to the normal command line, so that you can edit it as appropriate.
Edit command line
By using the cursor and function keys such as Home and End, you can browse and edit the command line. If you want to, you can also use the shortcut on the keyboard to complete the general Editing:
CTRL k: delete the part from the cursor to the end of the row
CTRL u: delete the part from the cursor to the beginning of the line
ALT d: delete the part from the cursor to the end of the current word
CTRL w: delete the part starting with the cursor to the current word
CTRL a: move the cursor to the beginning of the line
CTRL e: move the cursor to the end of the row
ALT a: move the cursor to the current word Header
ALT e: move the cursor to the end of the current word
CTRL y: insert the recently deleted word
! $: Repeat the last parameter of the previous command.
For example, you have created a directory by running the command mkdir peter/pan/documents/tinkerbell. Now you can run the command 'cd' to access this directory. You can use cd! $, Shell adds the parameter of the previous command 'mkdir' to the current 'cd.
After you have a better understanding of linux, you can see that these shortcuts are sometimes valid in other applications, for example, in the input box in the browser.
Available Shell shortcuts
Red Hat linux has many shortcuts, some of which are originally available in bash, while others are pre-configured for you and you will see how to set them later ). Since the home directory is the activity center of each user, many Unix have special shortcuts to this. '~ 'Is the abbreviated form of your home directory. Suppose you want to copy a file named 'sometext 'to the 'docs' subdirectory in another directory. Besides the input: cp sometext/home/myusername/docs
You can also use the abbreviation cp sometext ~ /Docs
Theoretically, this can also be applied to the command 'cd. No matter where the current path is, cd ~ Will return to your home directory. In fact, it can be simplified. You only need to type cd to return to the home directory.
Red Hat linux provides you with some preset shortcuts called 'aliases 'and aliases), such:
Ll: Execute 'LS-l-k' to list the directory content in long format, including some file attributes, and display the file size in KB rather than bytes)
Ls: Run 'LS-F -- color = auto' to list the directory content, add the file type identifier, and use the color)
Now you should have a better understanding of shell and some shortcuts. Let's take a look at what shell can do in addition to some simple commands.
Iii. Command arrangement/command task scheduling/command replacement

Command Arrangement
Now you will see some common commands. You may want to give all the commands in one line, and then you can focus on other places. No problem. shell allows you to place special arrange characters (queuing characters) between different commands ). Here we will introduce the two most common methods.
Please note that I add spaces on both sides of the characters to make it clearer. In practice, you do not have to do this. 'LS-a; du-hs 'and 'LS-a; du-hs' have the same effect.
Command1; command2: Execute command1 first, regardless of whether command1 has an error, and then execute command2.


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.