1.广播,将信息发给所有用户
#! /bin/sh
wall << zzzzzzzzzzz23End
asdfsdfsdaf
E-mail yousdf
asdfsadfasdf
asdfdsf
zzzzzzzzzzz23End
exit 0
2.虚拟一个2行的虚拟文件
#! /bin/sh
E_BADARGS=65
if [ -z "$1" ]
then
echo "Usage: `basename $0 ` filename"
exit $E_BADARGS
fi
TARGEFILE=$1
vi $TARGEFILE<<xx123
i
This is line 1 of the example line.
This is line 2 of the example line.
^[ -----------ctrl+v,ESC
ZZ
xx123
exit 0
3.cat多行数据
#! /bin/sh
cat<<End-of-message
--------------------------------
This is line of 1 of the message.
This is line of 2 of the message.
This is line of 3 of the message.
This is line of 4 of the message.
This is line of 5 of the message.
This is line of 6 of the message.
---------------------------------
End-of-message
exit 0
4.使用参数替换的here document
#! /bin/sh
CMNLINEPARAM=1
if [ $# -ge $CMNLINEPARAM ]
then
NAME=$1
else
NAME="John Doe"
fi
RESPONDENT="the author of this fine script"
cat<<Endofmessage
Hello,there,$NAME.
Greetings to you,$NAME,from $RESPONDENT.
Endofmessage
exit 0
5.上传一个文件对到一个目录
#! /bin/sh
E_ARGERROR=65
if [ -z "$1" ]
then
echo "Usage: `basename $0` Filename-to-upload"
exit $E_ARGERROR
fi
Filename=`basename $1`
Server="ibiblio.org"
Directory="/incoming/Linux"
Password="yourpassword"
ftp -n $Server << End-of-Session
user anonymous "$Password"
binary
bell
cd $Directory
put "$Filename.lsm"
put "$Filename.tar.gz"
bye
End-of-Session
exit 0
6.关闭参数替换
#! /bin/sh
NAME="John Doe"
RESPONDENT="the author of this fine script"
cat <<'EndofMessge'
Hello,there,$NAME.
Greeting to you, $NAME,from $RESPONDENT.
EndofMessage
exit 0
本文介绍了一系列实用的Shell脚本案例,包括广播消息给所有用户、创建虚拟文件、输出多行数据、使用参数进行文本替换、上传文件到指定目录以及在文本替换中关闭参数解析等功能。这些案例为读者提供了丰富的实践参考。
1955

被折叠的 条评论
为什么被折叠?



