1218 mail ,here document: << , 函数

1218上午


here document: <<
实例1):
#!/bin/bash
cat >> FILE <<EOF
This is the first line,
the second line.
EOF

实例解释:创建一个文件FILE,把两个EOF中间的内容追加到新建的文件中去

实例2)
创建newscript.sh脚本
#!/bin/bash
if ! grep "^#!" $1 &> /dev/null;then
cat>>$1<<EOF
#!/bin/bash
#Author:zby
#Date & Time:`date + "%F %D"`
#Description:

EOF
fi
vim +5 $1



然后把newscript.sh放入 /bin 目录下,以后创建脚本用“#newscript.sh FILE”就可以了



#mail username给某用户发邮件
内容写完后,另起一行,以.号结束

mail 不加参数,是读邮件

通常你需要将一个文件的内容或一个命令的输出邮寄给你自己或其他用户。这可以通过直接在命令行下使用mail命令实现。

mail从标准输入读取要邮寄的内容。“ –s ” 可引出邮件主题,参数就是要邮寄到的邮件地址。

[kevinz @stationxx kevinz ] $ mail -s "xsel.c source code" [email protected] < xsel.c
[kevinz @stationxx kevinz ] $ find . -name "*.old" | mail -s "old filenames" [email protected]










1218下午


在脚本中应用函数

function:功能,函数


function name {

}



naem () {


}

写一个脚本,从键盘输入用户名,


函数实现代码重用


先声明定义,再使用




max.sh

#!/bin/bash
#description:

max(){
echo "please inpute two number:"

fi $1 -lt $2;then
echo " The max is $2 "

else
echo "The max is $1"


}

max

function usershell {

if cut -d: -f1 /etc/passwd |grep "^"
}







函数后加上
return 0

保证整个函数返回值正常

如果不加这一句,返回结果只是函数最后一个命令的返回值



. soure 同一个意思




cping (){
PINGNET=`echo $1 |sed 's/([0-9]\{0,3\}\.[0-9]{0,3\}\.[0-9]{0,3\}\).*/\1/g'`
let I=1

while [ $I -le 254 ];do
ping -c1 -W1 $PINGNET.$I &>/dev/null
[ $? -eq 0 ] && echo "$PINGNET.$I is online" || echo "$PINGNET.$I is offline "
let I++
done

}

bping () {
PINGNET=`echo $1 |sed 's/([0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/g'`
let J=0
while [ $J -le 255];do
cping $PINGNET.$J
done
}
read -p "inpute a ip:" IP
bping $IP


echo -e "\33[33m"

你可能感兴趣的:(函数,职场,<<,mail,休闲,here,document:)