Here Document (二)

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 $? 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值