ENGINEER1.4
Shell Script Basics
Understanding the shell environment
How bash shells are used
Interactive:
--manual intervention, high degree of intelligence
----explain execution by article, low efficiency
Non-interactive:
-Need to design in advance, the difficulty of intelligence is big;
--Batch execution, high efficiency;
-Easy to implement in the background and quietly;
What is Shell scripting : Designing executable statements in advance to complete a specific task's files
--Explanatory program
--Sequential, batch execution
Standardize the general composition of shell scripts:
#! Environmental statement
#注释文本
Executable code
--------------------------------------------------------------------------------------------------------------- -------------------
Example 1:
Writing the helloworld.sh greeting script
Write script code:
#vim/root/helloworld.sh
#! /bin/bash
echo "Hello World"
Add x Execute Permissions
#chmod +x/root/helloworld.sh
Run a script test
#/root/helloworld.sh
Example 2:
Writing System Information Report scripts
Write script code:
#vim/root/1.sh
#!/bin/bash
Cat/etc/redhat-release
Uname-r
Hostname
2. Add x Execute Permissions
# chmod +x/root/1.sh
3. run the script test:
#/root/1.sh
--------------------------------------------------------------------------------------------------------------- -----------------------
Simple Scripting Tips
Application of redirected output
: Collect only the correct 2>: Collect only error &>: Collect all
This example requires writing a script/root/out.sh with the following features:
Execute this script to show I Love study!!
The execution/root/out.sh 2> Err.log should not be displayed, but the contents of the view Err.log file are I Love study!!
Steps:
#vim/root/out.sh
#!/bin/bash
#echo "I love study!!" >&2
#chmod-X/root/out.sh
#/root/out.sh
#/root/out.sh 2>err.log
#cat Err.log
--------------------------------------------------------------------------------------------------------------- -------------------------
Definition and use of variables
Store a value that may change with an immutable name
Reference variable Value: $ variable name with {} to define easily confusing names
Types of variables:
vary depending on the purpose of the variable:
Environment variables: variable names are generally capitalized to set the user, System environment (PATH)
positional variables:bash built-in, command-line arguments provided when storing built-in scripts $n, n is ordinal $ ... ${10}
Predefined variables:bash built-in, special values that can be called directly, cannot be modified directly
$#: Number of position variables loaded $*: The value of all positional variables $?: state value after exit of program, 0 indicates normal, other exception
Custom variables: user-owned design, modification and use
Cases:
Requires writing a script/root/myhead with the following features:
This script can receive 2 positional parameters and can be executed in the following format
After this script executes, you can display "you have provided a total of $# parameters," and then the next line shows "the file's top $:", immediately following the next line to start outputting the first few lines of the corresponding file
Step one : write/root/myuseradd to add a user's script
#vim/root/myuseradd
#! /bin/bash
echo "provides a total of $ #个参数"
echo "Username is $ $, password is $"
Useradd $
echo "$" | passwd--stdin $
Two. Add Execute Permissions
#chmod-X/root/myuseradd.sh
--------------------------------------------------------------------------------------------------------------- -------------------------------
Common Test options:
File status detection-F,-D,-E,-R,-W,-X
Integer values compare-gt,-ge,-eq,-ne,-lt,-le
string comparison = =,! =
Take the counter 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
Cases:
This example requires the creation of a/root/foo.sh script on the virtual machine Server0, with the following objectives:
When running/root/foo.sh Redhat, the output is Fedora
When running/root/foo.sh fedora, the output is Redhat
When no parameters or parameters are not redhat or fedora, the error output produces the following information:/root/foo.sh Redhat|fedora
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
This article is from the Linux OPS blog, so be sure to keep this source http://13401400.blog.51cto.com/13391400/1979741
Linux operational ENGINEER1.4 (Shell Scripting Basics)