1. A simple shell script:
#!/bin/bash #This is a shell sample echo "Our first sample" echo #This insert an empty line in output echo "We are currently in the following dir" /bin/pwd echo echo "This directory contains the following files that includes .sh keyword" /bin/ls | grep .sh
2. Structure of shell
1) #! (Shebang) point out the shell we are using (/bin/bash or /bin/ksh)
2) # represents comments --> It is recommanded to explain the function of this script at the begining of the script.
3) linux commands and flow control
3. Steps to create a shell script:
1) Create a script file
2) Add x permission to this file
3) Execute this shell script using "./script.sh" or "sh script.sh"
4. Another sample shell script
#!/bin/sh #Auto mail system info /bin/date +%F >> /tmp/sysinfo echo "disk info:" >> /tmp/sysinfo /bin/df -h >> /tmp/sysinfo echo >> /tmp/sysinfo echo "online users:" >> /tmp/sysinfo /usr/bin/who | /bin/grep -v root >> /tmp/sysinfo echo >> /tmp/sysinfo echo "memory info:" >> /tmp/sysinfo /usr/bin/free -m >> /tmp/sysinfo echo >> /tmp/sysinfo #write to root /usr/bin/write root < /tmp/sysinfo && /bin/rm /tmp/sysinfo #crontab -e #0 9 * * 1-5 script
5. How to create variable in shell
1) Two kinds of variable in shell: temp var and persist var
Temp vars are vars that defined inside a shell program and it is only effective inside this program.
Temp vars include user defined vars and position vars
Persist vars are environment variables, they would be effective both inside and outside shell program.
#!/bin/bash #A sample script for displaying environment variables echo "Our system path" echo $PATH echo "Our system lang" echo $LANG #Empty result represents english echo "Our system shell" echo $SHELL echo "Our system command line prompt" echo $PS1
2) Use uppercase alphabet to name a variable by convention.
Variable name can only starts with alphabets and cannot start with digits or other character.
When we assign a value to a variable, there should be no space at the both side of "="
#!/bin/bash #This is a demo for usage of variables NUM=1 #There should be no spaces at both side of "=" echo $NUM #When we use a variable, we should add "$" prefix TIME=`date` #When we need to assign the result of a command to a var, we should use "`" echo TIME A=2 echo "The value of A is $A" B=3 echo "The value of B is $B" B=$A echo "The value of B is $B"
3) The difference between "" and '' when applied for variable usage
#!/bin/bash #A demo shows the difference between "" and '' A=1 echo "A=$A" echo 'A=$A" #Output #A=1 #A=$A
4) Delete variables
#!/bin/bash #A demo for delete variable #Show all the variables that has been defined echo "Defined variables are as below:" set A=1 echo "A=$A" unset A echo "A=$A" #Output #Defined variables are as below: #.... #A=1 #A=
5) Define variables that contains multiple words that seperated by spaces or tabs
#!/bin/bash #A demo for defining variable contains multiple words NAME="DAVY JONES" echo $NAME COMPANY="CITI CORP" echo $COMPANY INFO="NAME: $NAME, COMPANY: $COMPANY" echo $INFO
6. Introduction to position variables and special variables
1) When using shell to compile user command, the first part of the command is regarded as command name, and the other parts are regarded as params.
These params are regarded as position params, they are marked by their position in the command.
ls -l file1 file2 file3 $0 means the name of the command "ls -l" $n means the params of the command, n=1~9
#!/bin/bash #A demo for position variables and special variables #Script name variables.sh echo "Command name is: $0" echo "First position params is: $1" echo "Second position params is: $2" echo "Third position params is: $3" #Input #sh variables.sh aaa bbb ccc #Output #Command name is: variables.sh #First position params is: aaa #Second position params is: bbb #Third position params is: ccc #Input #sh variables.sh aaa #Output #Command name is: variables.sh #First position params is: aaa #Second position params is: #Third position params is: #Input #sh variables.sh aaa bbb ccc ddd #Output #Command name is: variables.sh #First position params is: aaa #Second position params is: bbb #Third position params is: ccc
2) Special variables
1) $* represents all the params in command line
2) $# represents the param number in command line
3) $$ represents the PID of current program
4) $! represents the PID of previous backend command
5) $? represents the return value of last executed command.
0 represents previous command successfully executed
non-0 represents previous command failed execution
#!/bin/bash #A demo for special variables ls -ltr | grep.sh echo "Last command execution status: $?" aaa -aa echo "Last command execution status: $?" #Output: #..... #Last command execution status: 0 variable6.sh: line6: aaa: command not found #Last command execution status: 127
#!/bin/bash #A demo for usage of special variables #Usage: sh variable7.sh file01 file02 echo '$# is:' $# echo '$* is:' $* echo '$? is:' $? echo '$$ is:' $$ echo '$0 is:' $0 #Output: #$# is: 2 #$* is: file01 file02 #$? is: 0 #$$ is: 5580 #$0 is: variable7.sh
7. How to read user input
#!/bin/bash #A demo for reading user input read first second third echo "The first param is $first" echo "The second param is $second" echo "The third param is $third" #Sample Input #sh read.sh aaa bbb ccc #Sample Output #The first param is aaa #The second param is bbb #The last param is ccc #Sample Input #sh read.sh aaa bbb #Sample Output #The first param is aaa #The second param is bbb #The third param is #Sample Input #sh read.sh aaa bbb ccc ddd #Sample Output #The first param is aaa #The second param is bbb #The third param is ccc ddd
1) A technique frequently used for debugging shell: sh -x ***.sh
8. expr
1) expr is used for arithmatic calculation for int variable
2) There should be spaces at the both end of operator
3) Think of a scenario that we want to batch add user, we use a specific prefix, and use 1, 2, 3 ... as suffix.
#!/bin/bash #A demo for usage of expr var1 = 3 var2 = 10 var3 = 13 expr 3 + 5 expr $var1 - 5 expr $var1 / $var2 expr $var3 \* $var1 #Sample Output #8 #-2 #0 #39
[root@localhost ***]# expr 3 + 5 8 [root@localhost ***]# expr 100 / 3 33 [root@localhost ***]# expr 3+5 3+5 [root@localhost ***]# expr 3 * 5 expr: syntax error [root@localhost ***]# expr 3 \* 5 15
#!/bin/bash #A demo for advanced usage of expr var1=1 var2=2 var3=3 var4=4 expr `expr $var1 + $var2` / $var3 var4=`expr $var2 / $var1` echo "Value of var4 is $var4" #Sample Output #1 #Value of var4 is 2