Writing shell scripts
1. Make good use of judgment conditions Example
test -e /jason && echo "exist" || echo "not exist"
2. Another judgment symbol []
Each component in brackets should be separated by spaces, and the constants in brackets should be set with double or single quotes. For example
格式: [ "&HOME" == "$MAIL" ]
例2: read -p "please input (Y/N): " yn
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "ok,continue" && exit 0
[ "$yn" == "N" -o "$yn" == "n" ] && echo "oh,interrupt" && exit 0
echo "sorry,I dont know what is your choise" && exit 0
3. Default variables for shell scripts
The format is as follows:
/path/to/srcriptsname opt1 opt2 opt3 ........
$0 $1 $2 $3
example:
#!bin/bash
echo "the script name is $0"
[ -n "$1" ] && echo "the 1st parameter is $1" || exit 0
[ -n "$2" ] && echo "the 1st parameter is $2" || exit 0
[ -n "$3" ] && echo "the 1st parameter is $3" || exit 0
result:
jason@jason:~/scripts$ sh sh04.sh afa beta thta
the script name is sh04.sh
the 1st parameter is afa
the 1st parameter is beta
the 1st parameter is thta