bash test

admin Posted in bash
0

conditon测试类型对照表

运算符 描述 示例
文件比较运算符
-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]
-d filename 如果 filename为目录,则为真 [ -d /tmp/mydir ]
-f filename 如果 filename为常规文件,则为真 [ -f /usr/bin/grep ]
-L filename 如果 filename为符号链接,则为真 [ -L /usr/bin/grep ]
-r filename 如果 filename可读,则为真 [ -r /var/log/syslog ]
-w filename 如果 filename可写,则为真 [ -w /var/mytmp.txt ]
-x filename 如果 filename可执行,则为真 [ -L /usr/bin/grep ]
filename1-nt filename2 如果 filename1filename2新,则为真 [ /tmp/install/etc/services -nt /etc/services ]
filename1-ot filename2 如果 filename1filename2旧,则为真 [ /boot/bzImage -ot arch/i386/boot/bzImage ]
字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法)
-z string 如果 string长度为零,则为真 [ -z "$myvar" ]
-n string 如果 string长度非零,则为真 [ -n "$myvar" ]
string1= string2 如果 string1string2相同,则为真 [ "$myvar" = "one two three" ]
string1!= string2 如果 string1string2不同,则为真 [ "$myvar" != "one two three" ]
算术比较运算符
num1-eq num2 等于 [ 3 -eq $mynum ]
num1-ne num2 不等于 [ 3 -ne $mynum ]
num1-lt num2 小于 [ 3 -lt $mynum ]
num1-le num2 小于或等于 [ 3 -le $mynum ]
num1-gt num2 大于 [ 3 -gt $mynum ]
num1-ge num2 大于或等于 [ 3 -ge $mynum ]

wget的-e选项

admin Posted in bash
0

wget可谓居家旅行,杀人越货的必备之物了~其体积小巧,功能强大.所以,同时,其选项也比较繁多,man也是长长的,但是却有写个小技巧没写出来.
man里有这样的描述:

Wget can follow links in HTML and XHTML pages and create local versions of remote web sites, fully recreating the directory structure of the original site. This is sometimes referred to as “recursive downloading.” While doing that, Wget respects the Robot Exclusion Standard (/robots.txt). Wget can be instructed to convert the links in downloaded HTML files to the local files for offline viewing.

于是乎,如果你想mirror一整个站点,但是人家的 /robots.txt 却是:

User-agent: *
Disallow: /

你就要开始郁闷了,呵呵.
而且,我翻遍了man也找不到解决办法的,总不能为这点事去hack源码吧…
其实有这么个选项:

-e command
–execute command
Execute command as if it were a part of .wgetrc. A command thus invoked will be executed after the commands in .wgetrc, thus taking precedence over them. If you need to specify more than one wgetrc command, use multiple instances of -e.

用这个,就可以忽略 robots.txt 哦,具体是 -erobots=off 嘿嘿.

scp代理

admin Posted in bash
0

我写的ssh快速登陆脚本可以方便地通过跳板登陆到目标服务器,也就是 本机->hostA->hostB.
但是当有个本地文件像上传给hostB的时候,一般还是得先scp到hostA,再登陆hostA,scp到hostB,是不是必须这么麻烦呢?答案显然是不是的,不然我也不会在写这篇日志了,:)
下面就看看我的方法:
编辑 ~/.ssh/config 文件,加入以下几行

Host hostB
User usernameB
ProxyCommand ssh usernameA@hostA nc %h %p

然后,只要在本机输入 scp filename hostB:/remote/path(这里不要写usernameB了) 就可以直接将文件复制到目标地点了,哈哈.
当然如果你没有设置公钥对的话,过程中会让你输两次密码,一次是hostA的密码,一次是hostB的密码.
还有有个前提条件,就是hostA中必须安装有 nc(netcat).