• crontab 里 % 引发的问题

    写个 crontab ,命令是类似这样的

    /path/to/script `date +%Y-%m-%d`

    直接运行很正常,但是在 crotnab 里就出错。

    /bin/sh: -c: line 1: unexpected EOF while looking for matching ``'
    /bin/sh: -c: line 2: syntax error: unexpected end of file

    google 了好一阵才找到答案。原来 crontab 里的 % 是有特殊意义的,在这里需要转义。man 5 crontab 可以看到,

    Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

    % 如果没有用 \ 转义,就会被替换成换行。所以之前的 crontab 就出错了。

    解决办法:可以在 % 前面都加个 \ ,对于这个例子,写成 date +\%Y-\%m-\%d。

     

    Tags: crontab Linux
  • 在 linux 的命令行输出进度条

    linux 有些个命令比如 wget 会显示一个进度条。终于知道了一个办法弄出这个效果。其实也很简单:
    <?php
    for ($i = 0; $i <= 50; $i++) {
      printf("progress: [%-50s] %d%%\r", str_repeat('#',$i), $i * 2);
      usleep(1000 * 100);
    }
    echo "\n";
    echo "Done.\n";

    每次用 \r 结尾就是了。\n 是 new line,\r 是 return 。也就是回到行首。

    bash 版:

    b=''
    for ((i=0;$i<=100;i+=2))
    do
      printf "progress: [%-50s] %d%%\r" $b $i
      sleep 0.1
      b+='#'
    done
    echo

    Tags: linux
  • 用脚本发送带附件的邮件

    有时候做监控或者别的什么的需要发带附件的邮件,mail 命令不能直接这么做,好在多数系统都自带了 mutt ,这个工具可以做到。

    echo "mail content" | mutt -s "subject" -a "/path/to/attachment" to@domain.com

    今天用到了,搜了好一会才发现这个办法。这里笔记一下。

    mutt 是一个很强大的字符界面的邮件终端,貌似好多牛人都用这个来收发邮件。

    另:
    经济危机果然影响深远,今年除夕和初五接财神的鞭炮烟花都少了好多好多。

     

日历

关于神仙

神仙

世人都晓神仙好啊~~