Simple example shell program
Example 1.
#!/bin/bash#This is to show what a shell script looks likeecho "Our first example"echo # This inserts an empty line in output.echo "We are currently in the following directory."/bin/pwdechoecho "This directory contains the following files"/bin/ls -l .
Example 2.
#!/bin/bash# Auto mail for system info/bin/date +%F >> /tmp/sysinfoecho >> /tmp/sysinfoecho "Disk info:" >> /tmp/sysinfo/bin/df -h >> /tmp/sysinfoecho >> /tmp/sysinfoecho "Online users:" >> /tmp/sysinfo/usr/bin/who | /bin/grep -v root >> /tmp/sysinfoecho >> /tmp/sysinfoecho "Memory info:" >> /tmp/sysinfo/usr/bin/free -m >> /tmp/sysinfoecho >> /tmp/sysinfo# Write to root /usr/bin/write root < /tmp/sysinfo && /bin/rm -f /tmp/sysinfo# crontab -e# 0 9 * * 1-5 ./sysinfo.sh
Shell Structure
1 ,#! Specifies the shell for executing the script
2. # comment the line to describe the role and owner of the script.
3. command and control structure
Procedure for creating a shell program
Step 1: create a file that contains commands and control structures.
Step 2: Modify the permission of this file so that it can execute: chmod U + x
Step 3: Execute./example or sh example or. sysinfo. Sh
Shell variable
Variable: a method used by Shell to transmit data. It represents the symbolic name of each value.
Shell has two types of variables: temporary variables and permanent variables.
Temporary variables are defined within the shell program. The scope of use is limited to the program that defines them and are invisible to other programs. Including User-Defined variables and location variables.
A permanent variable is an environment variable and its value does not disappear with the execution of the shell script.
1. User-Defined variables
A user-defined variable starts with a letter or underscore and is a string of letters, numbers, or underscores (_). It is case sensitive. Variable name length is not limited. When using the variable value, you must add the prefix "$" before the variable name ".
2. Set and use variables
Set variables: Typically, variables are named with uppercase letters. The variable name must start with an English letter and cannot contain numbers.
Variable Value assignment: there is no space on both sides of "=.
Assign a value when defining, for example, num = 1
Assign the execution result of a command to a variable, for example, time = 'date'; time = $ (date + % F)
Assign the value of a variable to another variable, for example, B = 120; A = $ B
Use the echo command to view the variable value. Example: Echo $
3. variables that contain multiple words:
Name = Mike Ron # An error occurred while running, which should be changed:
Name = "Mike Ron" or $ name = 'Mike Ron'
4. Differences between single quotes and double quotes
For example, define date =$ (date + % F)
Time = "time is $ date"
Echo $ time
Time = 'Time is $ date'
Echo $ time
The content between single quotes is invariably specified to the variable.
5. Set to view all variables defined in the system
Unset: deletes the defined variable.
6. Location Variables
Shell interprets the first part of the command line as the command name and the other part as the parameter when executing the USER command. The parameter determined by the location on the command line is called the location parameter.
For example:
Ls-l file1 file2 file3
$0 name of the program LS-l
$ N the nth parameter value of this program, n = 1 ~ 9
7. Special Variables
$ * All parameters of this program
$ # Number of parameters of this program
$ PID of the program
$! PID used to execute the previous background command
$? The Return Value of the previous command.
Example-autobak. Sh
#!/bin/bash# backup files by dateDATE=$(/bin/date +%Y%m%d)/bin/tar -cf /backup/$1.$DATE.tar $1 > /dev/null 2>> /backup/$1.bak.log/bin/gzip /backup/$1.$DATE.tarif [ $? -eq 0 ] then echo "$1 $DATE backup successfully" >> /backup/$1.bak.logelse echo "ERROR: failure $1 $DATE backup" >> /bakup/$1.bak.logfi# crontable -e# 0 3 * * 2,5 script
Example-special. Sh
#!/bin/bash# A test script for special varecho ‘$# is‘ $#echo ‘$* is‘ $*echo ‘$0 is‘ $0echo ‘$? is‘ $?echo ‘$$ is‘ $$echo ‘$2 is‘ $2
Shell commands
1. Read Command [interaction mode]
Read Username
Example-read. Sh
#!/bin/bash# A test script for readread first second thirdecho "Your first parameter is $first"echo "Your second parameter is $second"echo "Your third parameter is $third"
# Sh-x command: single-step debugging display and execution
2. expr command to calculate Integer Variables
For example, there must be a space between expr 3 + 5 # "+"
Expr $ var1-5
Expr $ var1/$ var2
Expr $ var3 \ * 10 # "*" requires an escape character "\"
Complex operations:
Expr 'expr 5 + 7'/$ var4
Assign the calculation result to the variable:
Var4 = 'expr $ var1/$ var2'
Example-expr. Sh
#!/bin/bash# A test for expra=10b=20c=30val1=$(expr $a + $b + $c)echo "The value of val1 is $val1"val2=$(expr $c / $b)echo "The value of val2 is $val2"val3=`expr $a \* $b`echo "The value of val3 is $val3"val4=`expr $a + $c / $b`echo ‘The value of $a + $c / $b is ‘ $val4
Getting started with shell programming (version 2) (I)