http://hi.baidu.com/d_life/blog/item/ac85b8ede02e6fdcb31cb1fd.html

  

用法:  cat -v startqftest.sh |tr -d "^M" > qf.sh

 

 

shell脚本在Windows中打开过,拷到Linux下执行老是出错。

bash$ ./startqftest.sh
bash: ./startqftest.sh: /bin/bash^M: bad interpreter: No such file or directory
bash$ bash startqftest.sh
: command not founde 6:
startqftest.sh: line 105: syntax error: unexpected end of file

在VI中打开也看到不异常,但有一点儿是肯定的,与^M 有关,以前与遇到过,看来这次一定要把这个问题根冶了,不然以后还会来烦人。

使用工具 cat 和 tr, 方法如下:

bash$ cat -v startqftest.sh |tr -d "^M" > qf.sh

其中 startqftest.sh 是存在问题的脚本, qf.sh是新生成的文件。

sed 's/^M//' test.sh > back.sh

^M是Ctrl+v Ctrl+m


解释一下, cat 可以查看许多不可见这符,
cat -t 显示TAB 为 ^I
cat -e 显示行尾为 $,
cat -v 显示不可见字符,用^M表示,所以^M就表示不认识的不可见字符。M 表示Mark,标记的意思。
tr - translate or delete characters

另外,cat 还可以用来调Makefile,
cat -t Makefile 查看命令中有非Tab开头的,
grep '^ ' Makefile 查找命令行中以空格开头的行

cat -e Makefile 查看行尾是否正确,防止\后面多一个空格,
grep '\\[ ]$' Makefile 找出以上那样的行

For example:
bash$ cat -n Makefile
1    foo:
2        for i in * ; do \
3            echo $$i >> tmpfile ; \
4        done
5        if [ -f Makefile ] ; then \
6            cat -t -e Makefile ; \
7        fi
8   
bash$
这个Makefile有两处错误,用肉眼是看不出来的。
一处是第2行,命令是以空格开空的,另一处是第6行,\ 换行符到面多了一个空格,这行导致错误。
bash$ make
Makefile:2: *** missing separator. Stop.
bash$
bash$ cat -t Makefile
foo:
for i in * ; do \
^I^Iecho $$i >> tmpfile ; \
^Idone
^Iif [ -f Makefile ] ; then \
^I^Icat -t -e Makefile ; \
^Ifi

bash$ grep '^ ' Makefile
for i in * ; do \
bash$
bash$ cat -e Makefile
foo:$
for i in * ; do \$
echo $$i >> tmpfile ; \$
done $
if [ -f Makefile ] ; then \$
cat -t -e Makefile ; \ $
fi$
$
bash$ grep '\\[ ]$' Makefile
cat -t -e Makefile ; \
bash$

把错误更正后,运行正确,效果如下:
bash$ vi Makefile
bash$
bash$
bash$ make
for i in * ; do \
echo $i >> tmpfile ; \
done
if [ -f Makefile ] ; then \
cat -t -e Makefile ; \
fi
foo:$
^Ifor i in * ; do \$
^I^Iecho $$i >> tmpfile ; \$
^Idone $
^Iif [ -f Makefile ] ; then \$
^I^Icat -t -e Makefile ; \$
^Ifi$
$
bash$

posted on 2011-01-11 14:12  hotty  阅读(3623)  评论(0)    收藏  举报