1. What is a shell?
The shell is in the outer layer of the kernel, which is responsible for receiving commands entered by the user, interprets the commands as kernel can understand, then executes by kernel and returns the results back to the default output perimeter. The shell is a command interpreter and also a programming language. There are many types of shells, and in many Linux distributions, the standard shell is bash and the main object of our learning.
Basic structure of 2.shell program
The shell structure is largely set by the variables, built-in commands, shell syntax structure, function composition .
Usage Example Description: test.sh
1. #!/bin/bash
2. #说明使用/bin/bash as the interpreter for this script
3.
4. #定义一个函数
5. Function My_fun () {
6. Echo "Hello, $1,today is $"
7.}
8.
9. #定义连个变量
Ten. name=$1
today= ' Date '
12.
#函数调用.
My_fun "$name" "$today"
The above script will need to do some work to run, first give the execution permission
chmod +x test.sh
And then execute
./test.sh Willis
Output
Hello, Willis,today is Tue June 1 14:51:46 CST 2010
3. Parent shell and child shell
Before executing the script, the environment in which it is located is the parent shell. When the script is executed, the parent shell comes out with a new shell environment based on #!/bin/bash,fork, then executes in the child shell, executes after execution, and ends with the child shell, still returning to the parent shell, which does not affect the environment of the parent shell.
4.login Shell and Non-login shell
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/86/B3/wKioL1fH7lfQ9DepAABH1B_F1kc493.png "title=" Picture 1.png "alt=" Wkiol1fh7lfq9depaabh1b_f1kc493.png "/>
This picture is the process of the login shell, and when it is non-login the shell, only the part of the callout in the box is executed. By this diagram we can know, in the following cases, the process of execution.
4.1 Landing (login)
/etc/profile
~/.bash_profile
4.2 Logout (logout)
~/.bash_logout
5.Shell Interactive mode
5.1 Automatic Interaction Method One
Automatic interaction is the most important thing is the automatic input of interactive information, first Lenovo to file redirection, in shell programming has such a method of "command << delimiter (delimiter) is read from the standard input until the delimiter delimiter is encountered . "
The redirect operator Command << delimiter is a very useful order, and the shell will know that the input is finished by delimiter the delimiter until all the content before the next same delimiter is entered as input and the next delimiter is encountered. The most common delimiter delimiter is EOF, which can, of course, be self-defined for other characters.
Example: Disk auto-Partitioning script
#!/bin/bash
fdisk << EOF
N
P
1
( space)
+1g
W
Eof
MKFS.XFS/DEV/VDB1
Automatically modify user password scripts
#!/bin/bash
passwd << EOF
Willis
Willis
Eof
5.2 Automatic Interaction Method Two
another automatic input method that reminds you of interactive information: pipe, via echo + |
Example: Automatically modify user password scripts
#!/bin/bash
(echo "redhat"
Sleep 1
echo "redhat ") |passwd
Automatic partitioning
#!/bin/bash
Echo '
N
P
1
+1g
W ' | Fdisk $
Mkfs.xfs/dev/vdb1
5.3 Automatic Interaction Method Three
Expect is a dedicated tool to implement automatic interaction, expect syntax can refer to the relevant information, the code is as follows:
automatically switch to root user
#!/usr/bin/expect
Spawn Su Root
Expect "Password:"
Send "Willis \ r "
Expect EOF
Exit
The test passes, runs the script, and logs directly from the current user to the root user.
Shell Interactive Method Summary:
Method One (redirection) is simple, intuitive, and often practical, but has limited functionality in the field of auto-interaction.
Method Two (pipeline) is also very simple and intuitive, and sometimes even without sleep coordination can show a powerful automatic interactive power
Method Three (expect) is the most powerful in function, expect originally is to realize automatic interactive function, but the disadvantage is to install expect package, in the embedded environment difficult to install.
Three methods each have advantages and disadvantages, good application, can complete the Linux shell automatic interaction
This article from the "Technology life, Simple not simple" blog, please be sure to keep this source http://willis.blog.51cto.com/11907152/1845287
First knowledge of the shell