为了学习《linux程序设计》,从网上把书的源代码下了下来,结果在运行CD数据库例子的SHELL脚本的时候出现了错误。

: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: invalid signal specification
: command not found
: command not found
'd_db: line 36: syntax error near unexpected token `{
'd_db: line 36: `get_return() {

左思右想脚本不可能有错阿,是作者自己写的应该没有问题,难道是centos不支持?

centos是和RHEL基本一样的,既然书的前言写支持redhat就应该不是操作系统发行版的问题。

使用sh -x来调试,输出如下:

: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: invalid signal specification
: command not found
: command not found
'd_db: line 36: syntax error near unexpected token `{
'd_db: line 36: `get_return() {
[justin@bogon app]$ 
[justin@bogon app]$ sh -x cd_db 
+ $'\r'
: command not found
+ $'\r'
: command not found
+ $'\r'
: command not found
+ $'\r'
: command not found
+ $'\r'
: command not found
+ $'\r'
: command not found
+ menu_choice=$'\r'
+ current_cd=$'\r'
+ title_file=$'title.cdb\r'
+ tracks_file=$'tracks.cdb\r'
+ temp_file=$'/tmp/cdb.25028\r'
+ trap 'rm -f $temp_file' $'EXIT\r'
: invalid signal specification
+ $'\r'
: command not found
+ $'\r'
: command not found
'd_db: line 36: syntax error near unexpected token `{
'd_db: line 36: `get_return() {
看到了错误出现在每行结尾的\r上,突然想起在鸟哥的书上看过DOS下编辑的文本与LINUX下的文本的换行符不同,于是利用了dos2unix命令把文件进行了转换,结果运行成功。

dos2unix [-kn] file [newfile]   #DOS格式转换为linux格式
unix2dos  [-kn] file [newfile]   #linux格式转换为DOS格式