发现一个好网站:commandlinefu
从今天开始
每天从这个网站上选择一个我个人感觉有用的命令行实现
尽我自己的能力来简单解释一下
今天学习的是命令
sudo !!
这条命令sudo的意思大家都知道
关键是!!的含义,在这里是执行上一个命令
也就是history里的最后一个命令(用history命令可以看出)
我man bash看了一下
这里的!!指的是上一个命令,跟!-1是一个意思
同时,还有一些其他的!开头的表达的相关含义:
!n Refer to command line n.
!-n Refer to the current command line minus n.
!! Refer to the previous command. This is a synonym for ‘!-1’.
!string
Refer to the most recent command starting with string.
!?string[?]
Refer to the most recent command containing string. The trailing ? may be
omitted if string is followed immediately by a newline.
^string1^string2^
Quick substitution. Repeat the last command, replacing string1 with
string2. Equivalent to ‘‘!!:s/string1/string2/’’ (see Modifiers below).
!# The entire command line typed so far.
不过我们在工作中
经常用上箭头来呼出上一条命令(此功能为bash支持)
然后再回车执行
linux下
登录用户的操作都会被记到history文件里
(bash下貌似是~/.bash_history)
这样好像不太爽
尤其是想做点敏感操作的时候
于是
简单
登上去
unset HISTFILE
即可
在服务器上跑一些命令
经常都会用到简单的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?%
近期评论