shell常识总结

#!/bin/bash
cmd="ls -lt | grep ^d | awk 'NR==1 {print $9}'"
$cmd

这是一个获取文件夹名字的脚本,但是却提示:

ls: cannot access |: no such file or directory

ls: cannot access grep: no such file or directory

ls: cannot access ^d: no such file or directory

ls: cannot access awk: no such file or directory

...

解决:

cmd=$(ls -lt | grep ^d | awk 'NR==1 {print $9}')

也可以改成cmd=`ls -lt | grep ^d | awk 'NR==1 {print $9}'`

 

"find paths must precede expression"

使用命令 find -iname "result*" /media/magnum/test/, 提示上面错误,修改成:
find /media/magnum/test/ -iname "result*", 解决

 

获取远程命令的执行返回值问题

ssh xxxx@192.xx.xx.xx "test.sh; echo $?"

这样调用,显示的结果一直是0,无论你在test.sh的最后exit 1 或者2.

改成下面的这个形式可以正确获取返回值

ssh xxxx@192.xx.xx.xx "test.sh; "
echo $?

 

两种不同双引号的用法结果却不同

第一种:

format_type='"*.c"'
ret=`find -iname $format_type`
echo $ret

输出结果是空

另外一种:

format_type='*.c'
ret=`find -iname "$format_type"`
echo $ret

输出结果是:
GPIO.c IMU.c

posted @ 2015-10-19 16:30  Magnum Programm Life  阅读(292)  评论(0编辑  收藏  举报