在服务器上跑一些命令
经常都会用到简单的shell也算是编程
用的比较多的是for语句
for i in a b c d; do
这样的
但当in后面的列表比较长的时候
往往就不知道怎么写了
今天偶尔翻到bash的faq(?貌似是这个)
发现如果后面是一系列有规律的数字的话可以这样处理:
for i in ((i = 0; i < 10000; i++ ))
这样就行
呵呵
再联想到bash下(())括起来的可以是运算
比如echo $((3+4))
输出是7
那这种写法也就不难理解了
怎样用shell脚本删除掉文件的最后一行
网上大家多有讨论
方法也多多
比如用sed
用tail
用head
等等等等
但貌似都需要先把输出定向到一个临时文件
然后再删除原有文件
最后再把临时文件mv成原文件
后来找到一种方法
利用vim和重定向
直接在shell里实现
代码是这样的:
vim xxxx.txt 2>/dev/null <
G
dd
!
END
如此而已
呵呵
作为系统管理员
tail这个命令应该是很熟悉的
经常会用到用”-f”的参数来监控log文件(看着log一屏屏的翻,比较有成就感:)
好像在实时log分析的程序里
大家的思路大多也是用tail -f某个log文件
然后再用管道传给程序处理
但这样有一个问题
就是当系统logrotate这个log文件的时候
系统会重建这个log文件
在这个时候
如果监控这个log文件用的是命令”tail -f“的话
就会接不到任何新的内容
一般的做法是在这个log文件logrotate之前把监控的脚本程序(包含tail -f)都kill掉
在logrotate之后呢
再重新起起来
但是这里也许有一种更好的解决方法:用参数”-F”代替”-f”
“-F“相当于”–retry -f”
用这个参数,就算原来的log文件被重建
tail还能重新打开新的log文件,继续接收log内容
我做过简单的测试
在一个shell里用一个脚本不停地往一个log文件里写东西
在另外一个shell里用tail -f来读
当第一个shell里停掉程序,删掉log文件,再重启程序(同时重建了log文件)的时候
另外那个shell里读这个log文件的tail再没有任何新的显示
但如果我们用tail -F来读
同样在头一个shell里那样处理后
这边出一个“
tail: ‘xxxxxx’ has been replaced; following end of new file
”
的提示后又接着显示log文件里的内容了
如果你的系统装有metamail这个rpm包的话
则命令metasend可以用来发带附件的邮件
如果没装这个软件包呢 那就装上:)
metasend -b -t $RECEIVE_MAIL -S 100000000 -s "$MYSQL_BACKUP_DATABASE_FILENAME `date +%y-%m-%d`.tar.gz" -f $BACKUP_PATH/$MYSQL_BACKUP_DATABASE_FILENAME-`date +%y-%m-%d`.tar.gz -m application/octet-stream -e base64
这个是别人写的
我只拿来用用
简单讲
就是把要当作附件发的文件uuencode一下
然后再把编过码之后的文件当作邮件正文发出去
一句话的命令就是
cat <attachfile> | uuencode <attachname> | mail -s <subject> <targetemailaddress>
比如我要把文件hello.gif当作附件发给haha@pei.com
邮件标题用hello
那么我会这样
cat hello.gif | uuencode hello.gif | mail -s "hello" haha@pei.com
注意:命令uuencode在rpm包sharutils里
编一个东西的时候
执行./configure的时候出的错
“configure: error: cannot run /bin/sh config/config.sub”
结果发现是系统没有装libtool的缘故
所以yum install libtool
然后再./configure则没有这个问题了
其实这跟Linux关系不大
倒是跟shell很是相关
其实还是跟crontab最相关:)
当时crontab的log出错信息是:
/bin/sh: -c: line 0: unexpected EOF while looking for matching “’
/bin/sh: -c: line 1: syntax error: unexpected end of file
下面这是从命令man 5 crontab中摘出的一段话:
The entire command portion of the line, up to a newline or %
character, will be executed by /bin/sh or by the shell specified in
the SHELL variable of the cronfile. Percent-signs (%) in the command,
unless escaped with backslash (\), will be changed into newline char-
acters, and all data after the first % will be sent to the command as
standard input.
意思就是说crontab将忽略"%"后面的字串
把其当作前面命令的输入部分
而且还有个crontab的例子
用来说明这个:
0 22 * * 1-5 mail -s "It’s 10pm" joe%Joe,%%Where are your kids?%
原文是英文的,在这里
最简单的办法就是
rm ./-filename
(这里"-filename"在当前目录下)
很多命令,尤其是那些使用"getopt(3)"做常规参数解析的
会接受"–"作为参数,而且这意味着“这是最后一个选项”
所以,你的rm的版本也许能够操作"rm — -filename"这种方式
还有一些没有用getopt()的版本的rm认为单个"-"跟上面提到的"–"一样的作用
所以在这种版本的rm,可以用命令"rm – -filename"
Sending email with attachments on UNIX systems(UNIX系统下发带附件的邮件)
以下的例子都使用了下面的这几个变量
TXTFILE=/tmp/textfile
ATTFILE=/tmp/binary_file
SUBJECT="Your attachment"
MAILTO=user@where.ever
一共可以有n种办法
下面列出几种
- uuencode
uuencode $ATTFILE $ATTFILE | \
mail -s "$SUBJECT" $MAILTO
(uuencode $FILE1 $FILE1; uuencode $FILE2\
$FILE2)| mail -s "$SUBJECT" $MAILTO
- simple shell commands
echo "From: $LOGNAME\n\
To: $MAILTO\nSubject: $SUBJECT\n\
Mime-Version: 1.0\n\
Content-Type: text/plain\n\
" > /tmp/file
cat $TXTFILE >> /tmp/file
/usr/lib/sendmail -t -oi < /tmp/file
- metamail
metasend -b -s "$SUBJECT"\
-f $TXTFILE -m text/plain -e none -n \
-f $ATTFILE -m application/octet-stream \
-e base64 -t $MAILTO
- mpack
mpack -s "$SUBJECT" -c \
application/octet-stream $ATTFILE $MAILTO
- mutt
mutt -a $ATTFILE -s "$SUBJECT" $MAILTO <\
$TXTFILE
- Elm
elm -s"$SUBJECT" -A $ATTFILE $MAILTO < $TXTFIL
- Pine – (to be investigated but it doesn’t look good; maybe the c-client?)
- uuenview
uuenview -m $MAILTO -b -a $ATTFILE < $TXTFILE
- nail
nail -s "$SUBJECT" -a $ATTFILE $MAILTO < $TXTFILE
- Z-Mail
For a single file:
cat $TXTFILE | zmail.small -subject "$SUBJECT"\
-attach application/octet-stream:${ATTFILE}\
$MAILTO Bart Schaefer offers the following\
for multiple attachments.\
(untested by myself)
zmail.small -rf /dev/null -e \
'mail -z -s "$SUBJECT" $MAILTO' \
-e 'compcmd attach-file $ATTACHFILE \
application/msword base64 \
"$DESCRIPTION"' \
-e! 'compcmd send'
最近评论