1.生成另外一个脚本的脚本
#! /bin/sh
OUTFILE=generated.sh
(
cat <<'EOF'
echo "This is a generated shell script."
echo "Generated file will be named: $OUTFILE"
a=7
b=3
let " c = $a * $b "
echo " c= $c "
exit 0
EOF
) > $OUTFILE
if [ -f "$OUTFILE" ]
then
chmod 755 $OUTFILE
else
echo "Problem in creating file: /"$OUTFILE/""
fi
exit 0
2.here document与函数
#! /bin/sh
GetPersonalData(){
read firstname
read lastname
read address
read city
read state
read zipcode
}
GetPersonalData << RECORD001
Bozo
Bozeman
2726 Nondescript Dr.
Baltimore
MD
21226
RECORD001
echo
echo "$firstname $lastname"
echo $address
echo "$city,$state,$zipcode"
echo
exit 0
3."匿名"的here document
#! /bin/sh
: <<TST
${HOSTNAME}${USER}${MAIL?}
TST
exit 0
4.注释掉一段代码块
#! /bin/sh
: << COMMENTBLOCK
echo "This line will not echo."
This is a comment line missing the "#" prefix.
This is another comment line missing the "#" prefix.
&*@!!++=
The above line will cause no error message,
because the Base intetpreter will ignore it.
COMMENTBLOCK
echo "Exit value of above /"COMMENTBLOCK/" is $?."
:<<DEBUGXXX
for file in *
do
cat "$file"
done
DEBUGXXX
exit 0
5.分析一个邮箱
#! /bin/sh
E_MISSING_ARG=67
if [ -z "$1" ]
then
echo "Usage: $0 mailbox-file"
exit $E_MISSING_ARG
fi
mbox_grep(){
declare -i body=0 match=0
declare -a date sender
declare mail header value
echo "mail: $mail"
while IFS= read -r mail
do
if [[ $mail =~ "^From " ]]
then
(( body = 0 ))
(( match = 0 ))
unset date
elif (( body ))
then
(( match ))
elif [[ $mail ]]; then
IFS=: read -r header value <<< "$mail"
case "$header" in
[Ff][Rr][Oo][Mm] ) [[ $value =~ "$2" ]] && (( match++ )) ;;
[Dd][Aa][Tt][Ee] ) read -r -a date <<< "$value" ;;
[Rr][Ee][Cc][Ee][Ii][Vv][Ee][Dd] ) read -r -a sender <<< "$value" ;;
esac
else
(( body++ ))
(( match++ ))
echo "MESSAGE ${date:+of: ${date[*]} }"
echo "IP address of sender: ${sender[1]}"
fi
done< "$1"
}
mbox_grep "$1"
exit $?