Shell Script
dking~ sharing
1.1 Scripting Concepts
Put multiple commands into a single file to facilitate one-time execution of a program file
Unified Script Storage Directory:/server/scripts
Using Vim to edit scripts is recommended
To view the script execution process: Sh-x script
1.2 Script editing format:
#!/bin/bash
#author: DQF
#date: 2018/1/11
#ver: 1.0
#desc: Backup/etc to/backup
Script editing Examples:
#!/bin/bash
#author:d QF
#date: 2018/1/11
#ver: 1.0
#desc: Backup/etc to/backup
CD/
Tar zchf/backup/etc- date +%F-%a-%T
. tar.gz etc/
find/backup/-type f-mtime +7! -name "Etc-*-mon.tar.gz" | Xargs mv-t
/tmp
1.3 Variable Concept
A file in the system that can change changes that affect the operating environment at any time
- Variable classification:
Global variables, also known as environment variables, can be used globally
Local variables, also known as normal variables, are used by the current environment
- Assigning variables:
dqf=123 (normal variable)
Export dqf=123 (environment variable)
Permanent entry: Put in/etc/profile
Source dqf.sh execution script can also take effect
- Display variable: Echo $DQF
123
Echo ${DQF}123
$DQF ==${DQF}
- Files related to variables
~/.bash_profile ~/.BASHRC/ETC/PROFILE/ETC/BASHRC
- Directories related to variables:/etc/profile.d/
Example 1-1 puts the cowsay.sh into the/profile.d/directory/usr/bin/cowsay hello,world! Write script into/etc/profile.d/directory < hello,world! >\ ^__^ \ (oo)\_______ (__)\ )\/ ||----w | || ||
chmod +x/etc/profile.d/cowsay.sh gives execute permission, power on to self-run1.4 The role of the $
$0 indicates the name of the script
$1 indicates the first parameter
$2 indicates the second parameter
$ n Represents the nth parameter
$ # Indicates the number of scripts
$ ? Represents the return value of the previous command to determine whether the previous command performed the correct
1.5 [] Function (conditional expression)
[-d/root] is used to determine if there is a directory under the root/root D means directory
[-f/root/oldboy.txt] is used to determine whether the existence of oldboy.txt file F/root
$ ? Represents the return value of the previous command to determine whether the previous command performed the correct
0 indicates correct non 0 is incorrect, command execution is problematic
Example 1-2 determine if the/oldboy/oldboydir directory exists in the system if prompted dir exist
[-d/data]echo $?]
0
[-d/oldboy/oldboydir] && echo "dir exist"
Dir exist
[-f/tmp/ufo.txt] | | Touch/tmp/ufo.txt
1.6 Comparison Size
[A-eq b] equal = = means a=b
[A-ne b] not equal! = represents a≠b
[A-GT b] great than > representation a>b
[A-ge b] Great equal >= represents a≥b
[A-lt b] less than < = A<b
[A-le b] Less equal <= represents a≤b
1.7 If statement
If statement format-1:
If [Set Condition];then
Execute command
Fi (end sign)
If statement format-2:
If [Set Condition];then
Execute command
Else
Perform other
Fi (end sign)
1.8 For Loop
For loop format:
For name in conditional command
Do
Execute command
Done
1.9 Script Case Demo
1.9.1 using awk to make a simple arithmetic device
Scripted script with read/awk/if statements
vim/server/scripts/bc.sh
Read-t5-p "input a B:" A B
If [$b-eq 0];then
echo "B is not allow 0"
Exit
Fi
Awk-va= $a-vb= $b ' begin{print a+b,a-b,ab,a/b} '
Editing scripts with $n/awk/if statements
vim/server/scripts/bc.sh
If [$#-ne 2];then
echo "Usages:you can only write words"
Exit
Fi
Awk-va=$1-vb=$2 ' Begin{print a+b "\ n" a-B "\ n" a \ "\ n" \ A} '
1.9.2 using the For statement to turn off useless services
vim/server/scripts/chk.sh
off=chkconfig |awk ‘!/crond|network|sshd|rsyslog|sysstat/{print $1}‘
For DQF in $off
Do
Chkconfig $DQF off
Done
1.9.3 Variable Assignment method
Method 1-Assign a normal variable--restart invalid normal variable cannot be run in script
A=xecho $a env View System variables
X
Method 2-export Assignment environment variable--Restart invalid environment variable can be run in script
Export A=xecho $a
X
Method 3-read Interactive Assignment variable--restart invalidation
Read-t5-p "input a B:" A B-T5 indicates automatic exit after 5 seconds
Input a b:2 3-p means print quiz on screen
Echo $a $b-s means hidden input process
2 3 Write/etc/profile in order to configure permanent variables
Getting Started with Shell scripting