First, the shell script writing and testing
Goal:
This example requires two simple shell scripts, with the following task objectives:
1> wrote a greeting/root/helloworld.sh script that was executed and displayed a phrase "Hello world!! ”
2> Write a/root/sysinfo script that can output system information, then output the current version of the Red Hat system, the kernel version currently in use, and the hostname of the current system.
Scheme:
Standardize the general composition of shell scripts:
1> #! Environmental Statement (Sha-bang)
2> # Comment Text
3> Executable code
Steps:
Step one: Write the helloworld.sh greeting script
1) Writing script code
[Email protected] ~]# vim/root/helloworld.sh
#!/bin/bash
echo "Hello world!"
2) Add x Execute Permissions
[Email protected] ~]# chmod +x/root/helloworld.sh
3) Run the script test
[Email protected] ~]#/root/helloworld.sh
Hello World!!
Step two: Write the SysInfo System Information Report script
1) Writing script code
[Email protected] ~]# Vim/root/sysinfo
#!/bin/bash
Cat/etc/redhat-release
Uname-r
Hostname
2) Add x Execute Permissions
[Email protected] ~]# chmod +x/root/sysinfo
3) Run the script test
[Email protected] ~]#/root/sysinfo
Red Hat Enterprise Linux Server Release 7.0 (MAIPO)
3.10.0-123.el7.x86_64
Server0.example.com
Second, the application of redirected output
Goal:
This example requires writing a script/root/out.sh with the following features:
1> execute this script to show I Love study!!
2> execution/root/out.sh 2> Err.log should not be displayed, but view the contents of the Err.log file for I Love study!!
Scheme:
Type of screen output text:
1> standard Output (1): Command line performs normal display results
2> standard Error (2): Display results when the command line performs an error or an exception
To save the screen display information to a file:
1> cmd > file, cmd >> file
2> cmd 2> file, cmd 2>> file
3> cmd &> file, cmd 2> file 1>&2
The standard output of the command line can be programmed with standard errors using the 1>&2 or >&2 operation.
steps:
Step: Write out.sh output test script
1) Writing script code
[Email protected] ~]# vim/root/out.sh
#!/bin/bash
echo "I Love study!" >&2
2) Add x Execute Permissions
[Email protected] ~]# chmod +x/root/out.sh
3) Run the script test
[Email protected] ~]#/root/out.sh
I Love study!!
[Email protected] ~]#/root/out.sh 2> err.log
[email protected] ~]# cat Err.log
I Love study!!
Iii. use of special variables
Goal:
This example requires writing a script/root/myhead with the following features:
1) This script can receive 2 positional parameters and can be executed in the following format:
/root/myhead File path line number
2) After this script executes, you can display "you have provided a total of $# parameters", and then on the next line displays "The file's previous line:", immediately after the next line to start outputting the corresponding file of the first few lines of content.
Scheme:
Use positional variables to get the command-line arguments that are provided when the script is executed:
- represented as $n, N is ordinal
- $, $ 、.. .. ${10}, ${11} 、.. ..
Use the pre-defined variable $ #可以统计执行脚本时提供的位置变量个数.
Steps:
Step one: Write/root/myuseradd to add a user's script
1) Writing script code
[Email protected] ~]# Vim/root/myuseradd
#!/bin/bash
echo "provides a total of $# parameters"
echo "Username is $ $, password is $"
Useradd $
echo "$" | passwd--stdin $
2) Add x Execute Permissions
[Email protected] ~]# chmod +x/root/myuseradd.sh
Step two: Test the/root/myuseradd script
1) test Add user bob with password set to 1234567
[[email protected] ~]#/root/myuseradd Bob 1234567
2 parameters are provided altogether
The user name is Bob, and the password is 1234567.
Change the password for user Bob.
PASSWD: All the authentication tokens have been successfully updated.
[[email protected] ~]# ID BOB
uid=1002 (Bob) gid=1002 (Bob) group =1002 (Bob)
2) test Add user Jerry, password set to 1234567
[Email protected] ~]#/root/myuseradd Jerry 1234567
2 parameters are provided altogether
The user name is Jerry, and the password is 1234567.
Change the user's password for Jerry.
PASSWD: All the authentication tokens have been successfully updated.
[[email protected] ~]# ID Jerry
Uid=1003 (Jerry) gid=1003 (Jerry) group =1003 (Jerry)
Iv. Writing a judgment script
Goal:
This example requires the creation of a/root/foo.sh script on the virtual machine Server0, with the following objectives:
1> when running/root/foo.sh redhat, the output is Fedora
2> when running/root/foo.sh fedora, output is Redhat
3> when no parameters or parameters are not redhat or fedora, the error output produces the following information:/root/foo.sh Redhat|fedora
Scheme:
How to perform conditional tests in a shell script:
1> any one command line
2> Test An expression
3> [test expression]
Common test options for testing:
1> file status Detection-F,-D,-E,-R,-W,-X
2> integer values compare-gt,-ge,-eq,-ne,-lt,-le
3> string comparison = =,! =
4> take the reverse action!
Multi-Branch If selection structure:
If condition test action 1
Then
Command sequence 1 ....
elif Condition Test Action 2
Then
Command sequence 2 ....
Else
Command Sequence 3 ....
Fi
Steps:
Step One: Write foo.sh judgment script
1) Writing script code
[Email protected] ~]# vim/root/foo.sh
#!/bin/bash
If ["$" = "Redhat"]
Then
echo "Fedora"
elif ["$" = "Fedora"]
Then
echo "Redhat"
Else
echo "/root/foo.sh Redhat|fedora" >&2
Fi
2) Add x Execute Permissions
[email protected] ~]# chmod +x/root/foo.sh
Step two: Test foo.sh judgment Script
1) test conditions that provide the correct parameters
[Email protected] ~]#/root/foo.sh Redhat
Fedora
[Email protected] ~]#/root/foo.sh Fedora
Redhat
2) test to provide unexpected parameters
[Email protected] ~]#/root/foo.sh Ubuntu
/root/foo.sh Redhat|fedora
3) test conditions that do not provide parameters
[Email protected] ~]#/root/foo.sh
/root/foo.sh Redhat|fedora
Five, write a bulk add user script
Goal:
This example requires the creation of a/root/batchusers script on the virtual machine Server0, with the following objectives:
1> This script requires a user name list file as a parameter
2> If no arguments are provided, this script should give the hint Usage:/root/batchusers, Exit and return the corresponding value
3> If you provide a file that does not exist, this script should give the prompt Input file not found, exit and return the corresponding value
4> The new user's login shell is/bin/false, no need to set a password
5> list test file: http://classroom/pub/materials/userlist
Scheme:
Single Branch If selection structure:
If condition test action
Then
Command sequence ....
Fi
Exit status of the script: depends on the last command before exiting $? Value, or "Exit integer value" specified.
List-for-loop structure:
For variable name in value 1 value 2 value 3: ..
Do
Command sequence ($ variable name)
Done
Use command substitution to get command results: $ (Command line)
Steps:
Step one: Write batchusers bulk Add user script
1) Writing script code
[Email protected] ~]# vim/root/batchusers
#!/bin/bash
If [$#-eq 0]; Then
echo "Usage:/root/batchusers <userfile>"
Exit 1
Fi
if [!-F $]; Then
echo "Input File not Found"
Exit 2
Fi
For name in $ (cat $)
Do
Useradd-s/bin/false $name
Done
2) Add x Execute Permissions
[email protected] ~]# chmod +x/root/batchusers
Step Two: Test batchusers batch Add user script
1) Download the user list test file:
[Email protected] ~]# wget Http://classroom/pub/materials/userlist-O/root/userlist
.. ..
2016-11-27 17:23:32 (2.83 MB/s)-'/root/userlist ' saved [27/27]
[[email protected] ~]# cat/root/userlist//Check download file
Duanwu
Zhongqiu
Zhsan
Lisi
2) Implement bulk Add Users:
[Email protected] ~]#/root/batchusers/root/userlist
[[email protected] ~]# ID Duanwu
uid=1006 (Duanwu) gid=1006 (Duanwu) groups=1006 (DUANWU)
3) test other exception handling:
[[email protected] ~]#/root/batchusers//No list file provided
Usage:/root/batchusers <userfile>
[[email protected] ~]# echo $?
1
[[email protected] ~]#/root/batchusers/root/userlist.txt//supplied list file not found
Input File not found
[[email protected] ~]# echo $?
2
Engineer Technology (v): Shell script writing and testing, redirecting output application, using special variables, writing a judgment script, writing a batch add user script