Getting started with Shell programming (version 2) (medium) and getting started with shell programming
Variable test statement-test
Purpose: test whether the variables are equal, empty, and file type.
Format:
Test condition or [] # range: integer, String, file
1) integer test:
Test int1-eq int2 test whether the integer is equal
Test int1-ge int2 test whether int1> = int2
Test int1-gt int2 test whether int1> int2
Test int1-le int2 test int1 <= int2
Test int1-lt int2 test whether int1 is <int2
Test int1-ne int2 test whether the integer is not equal
2) string test:
Test str1 = str2 test whether the string is equal
Test str1! = Str2 test whether the string is not equal
Test str1 test whether the string is not empty
Test-n str1 test string is not empty
Test-z str1 test whether the string is null
3) file test:
Test-d file specifies whether the file is a directory
Test-f file specifies whether the file is a regular file
Test-x file specifies whether the file is executable
Test-r file specifies whether the file is readable.
Test-w file specifies whether the file is writable.
Test-a file specifies whether the file exists
Whether the size of the test-s file is not 0
Note: The test statement is generally not used separately. It is generally used as the test condition of the if statement, for example;
if test -d filethen....fi
Abbreviated form of test variables "[]"
Example-apachtest. sh
#!/bin/bash# A test shell script for test Apache is running or notweb=$(/usr/bin/pgrep httpd)echo "Now let's test the Apache..."echo#if [ "$web" != "" ]if [ -n "$web" ]then echo "Apache is running..."else echo "Apache is NOT running..." /etc/rc.d/init.d/httpd startfi
Process control statement
Flow Control statement: used to control the flow of shell programs
Exit statement: exit the program and return a return code. If the return code is 0, the program Exits normally. If the return code is not 0, the program exits abnormally.
Example: exit 0
I. if
If/then format
if test -d $1 then ... fi
Example-if_then.sh
#!/bin/bash# A test shell script for if/thenif [ -x /etc/rc.d/init.d/httpd ]then echo "Script: /etc/rc.d/init.d/httdp have x power!" /etc/rc.d/init.d/httpd restartfi
If/else format
If condition 1 then command 1 elif condition 2 then command 2 else command 3 fi
Union of multiple conditions:
-A: logical and. The result is true only when both conditions are true.
-O: logical or. If either of the two conditions is true, the result is true.
Example-if_else.sh
#!/bin/bash# A test shell script for if/elif/elseecho -n "Please input a filename: "read filenameif [ -d $filename ]then echo "$filename is a directory"elif [ -f $filename ]then echo "$filename is a commen file"elif [ -c $filename -o -b $filename ]then echo "$filename is a device file"else echo "$filename is a unkown file"fi
Example-if_elif_exit.sh
#!/bin/bash# A test shell script for if/elifif [ $# -ne 2 ] thenecho "Not enough parameters"exit 1fiif [ $1 -gt $2 ]then echo "$1 is great then $2"elif [ $1 -lt $2 ]then echo "$1 is little then $2"else echo "$1 is equal as $2"fi
2. for/in
For variable in name table do command list done
Example-for. sh
#!/bin/bash# A test shell script for "for"for DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturdaydo echo "The day is $DAY"done
Awk Command [segment extraction]
Awk-F domain separator 'COMMAND '[single quotation marks] # if you do not use-F to specify a delimiter, the default Delimiter is space.
1. Check users with UID 0 in the system.
Awk-F: '$3 = 0 {print $1}'/etc/passwd
# Awk-F: '{print $1}'/etc/passwd
-F: Specify the delimiter as follows:
$3 indicates the third digit of the separator
2. Check users with blank passwords in the system
Awk-F: 'length ($2) = 0 {print $1} '/etc/shadow
# Ps aux | grep-v root | awk '{print $2 }'
Example-awk. sh
#!/bin/bash# A test script for desplay users infomation/bin/echo -n "Please input a username: "read username/bin/grep $username /etc/passwd > /dev/null 2> /dev/nullif [ $? -eq 0 ]then /bin/echo "username is: $username"else /bin/echo "user: $username is not exits." exit 1fi/bin/echo# list /etc/passwd infouserinfo=`/bin/grep ^$username:x /etc/passwd`uid=`echo $userinfo | awk -F: '{print $3}'`gid=`echo $userinfo | awk -F: '{print $4'}`dir=`echo $userinfo | awk -F: '{print $6}'`shell=`echo $userinfo | awk -F: '{print $7}'`# get /etc/group infogroupinfo=`/bin/grep x:$gid /etc/group`gname=`/bin/echo $groupinfo | awk -F: '{print $1}'`/bin/echo "user id is: $uid"/bin/echo "default group is: $gname"/bin/echo "home directory is: $dir"/bin/echo "shell is: $shell"/bin/echo "group member info:"# get group membersgroups=`/usr/bin/groups $username`/bin/echo $groups/bin/echo# get online infoonline=`/usr/bin/who | grep $username`if [ -z "$online" ]then echo "$username is not online"else echo "$username is online..."fi
Instance-killuser. sh
# Idea: shutting down all processes of a user, including shell, is equivalent to kicking the user out of the system #! /Bin/bash # A shell srjit to kill a user in Linuxusername = $1 killpid = '/bin/ps aux | grep $ username | awk' {print $2}'' PID in $ killpiddo/bin/kill-9 $ PID 2>/dev/nulldone
Which of the following statements about $2 in shell programming is true?
This is generally the case:
<ShellScript> <option...>
Example:
Shell. sh abc bcd
Then $1 is abc $2, and bcd is the second option (parameter) of the command line output)
Shell programming $ @ $ *
Both are the parameters followed by running shellscript, and are connected into a string for calling. That is, $ @ =$ * = $1 + $2 + $3 ......
For example, the following is a simple shellscript named 01.sh.
======================================
#! /Bin/bash
# Tim: For "zhidao.baidu.com" to understand the parameter "$ @" "$ *"
# Name: 01.sh
Echo $ *
Echo $ @
======================================
After the permission is changed, run:./01.sh parameter 1 parameter 2 parameter 3
The running result is:
Parameter 1 parameter 2 parameter 3
Parameter 1 parameter 2 parameter 3
---------------------------------
====================
Question: Another special parameter is $ #. This is used to count the number of all parameters when shellscript is executed.
For example, add echo $ # in the above script as follows:
======================================
#! /Bin/bash
# Tim: For "zhidao.baidu.com" to understand the parameter "$ @" "$ *"
# Name: 01.sh
Echo $ *
Echo $ @
======================================
Continue to run as you have just run. The results are as follows:
Parameter 1 parameter 2 parameter 3
Parameter 1 parameter 2 parameter 3
3
-----------------
The "3" in the last line is the result of echo $. That is, how many parameters are counted.