执行shell脚本中declare: not found
2011-03-25 16:45
615 查看
在学习 Shell 脚本的时候,有两个例子在运行时出现异常。比如,我在 test-dash.sh 脚
本输入如下内容:
echo -e "Trekshot. /a /n"
declare -i number=7
echo $number
在 Ubuntu 系统下使用 sh test-bash.sh 运行将出现如下提示信息:
-e Trekshot.
test-dash.sh: 2: declare: not found:
有两个问题:
a. echo 的参数 '-e' 怎么也打印出来了?
b. 一个简单的整数声明语句为何提示 "declare: not found"?
1.
出现此问题的原因并非代码本身有误,而是 Ubuntu 系统的问题。我们在编写脚本时,第一
行经常要写这么一行内容:#!/bin/bash. 这是对该脚本所使用的 shell 进行声明,因为
Linux 上的 shell 并非只有一种,而各个 shell 的语法是由差别的。之所以把 echo 命令
的参数误打出来,就是因为 sh 命令没有理解该语法,即当前使用的并不是 bash.下面来证
明:
获取 /bin 中相关文件的属性:
-rwxr-xr-x 1 root root 725136 2008-05-13 02:48 bash
-rwxr-xr-x 1 root root 87924 2008-06-21 00:07 dash
lrwxrwxrwx 1 root root 4 2010-03-25 14:29 sh -> dash
这表明在执行 sh test-dash.sh 的时候,我们使用的是一个叫 "dash" 的命令(注意 sh
实为链接文件),而并不是脚本第一行中声明的 bash. 这就是上面现象的原因。 其实,如
果使用 bash test-dash.sh 命令执行的话,上面两个错误根本不会出现。
2.
dash(Debian Almquist SHell) Ubuntu 自 6.10 后,将先前的 bashshell 更换成了
dash (有待考证). 在设置 dash 的说明中有下面文字:
The default /bin/sh shell on Debian and Debian-based systems is bash.
However, since the default shell is required to be POSIX-compliant, any
shell that conforms to POSIX, such as dash, can serve as /bin/sh. You may
wish to do this because dash is faster and smaller than bash.
大致意思是说默认的 shell 是 bash shell, 但只要是能兼容 POSIX 的 shell 都可以,而
dash 比 bash 速度更快、更小巧,因此 Ubuntu 安装了 dash.
3.
怎么能把 dash 去掉而使用默认的 bash?使用如下命令:
sudo dpkg-reconfigure dash
此命令是对已安装的包进行重新配置,在菜单(dash-bash-sh.png)中选择是否将 sh 链接
到 dash (Install dash as /bin/sh?) 选择否即可。
附图一:Ubuntu 下设置 dash 界面
本输入如下内容:
echo -e "Trekshot. /a /n"
declare -i number=7
echo $number
在 Ubuntu 系统下使用 sh test-bash.sh 运行将出现如下提示信息:
-e Trekshot.
test-dash.sh: 2: declare: not found:
有两个问题:
a. echo 的参数 '-e' 怎么也打印出来了?
b. 一个简单的整数声明语句为何提示 "declare: not found"?
1.
出现此问题的原因并非代码本身有误,而是 Ubuntu 系统的问题。我们在编写脚本时,第一
行经常要写这么一行内容:#!/bin/bash. 这是对该脚本所使用的 shell 进行声明,因为
Linux 上的 shell 并非只有一种,而各个 shell 的语法是由差别的。之所以把 echo 命令
的参数误打出来,就是因为 sh 命令没有理解该语法,即当前使用的并不是 bash.下面来证
明:
获取 /bin 中相关文件的属性:
-rwxr-xr-x 1 root root 725136 2008-05-13 02:48 bash
-rwxr-xr-x 1 root root 87924 2008-06-21 00:07 dash
lrwxrwxrwx 1 root root 4 2010-03-25 14:29 sh -> dash
这表明在执行 sh test-dash.sh 的时候,我们使用的是一个叫 "dash" 的命令(注意 sh
实为链接文件),而并不是脚本第一行中声明的 bash. 这就是上面现象的原因。 其实,如
果使用 bash test-dash.sh 命令执行的话,上面两个错误根本不会出现。
2.
dash(Debian Almquist SHell) Ubuntu 自 6.10 后,将先前的 bashshell 更换成了
dash (有待考证). 在设置 dash 的说明中有下面文字:
The default /bin/sh shell on Debian and Debian-based systems is bash.
However, since the default shell is required to be POSIX-compliant, any
shell that conforms to POSIX, such as dash, can serve as /bin/sh. You may
wish to do this because dash is faster and smaller than bash.
大致意思是说默认的 shell 是 bash shell, 但只要是能兼容 POSIX 的 shell 都可以,而
dash 比 bash 速度更快、更小巧,因此 Ubuntu 安装了 dash.
3.
怎么能把 dash 去掉而使用默认的 bash?使用如下命令:
sudo dpkg-reconfigure dash
此命令是对已安装的包进行重新配置,在菜单(dash-bash-sh.png)中选择是否将 sh 链接
到 dash (Install dash as /bin/sh?) 选择否即可。
附图一:Ubuntu 下设置 dash 界面
![](http://images.cnblogs.com/cnblogs_com/trekshot/dash-bash-sh.png)
相关文章推荐
- 执行shell脚本中declare: not found
- Shell脚本执行时出现declare: not found的解决方法
- 嵌入式 Shell脚本执行时出现declare: not found的解决方法
- shell脚本执行错误 $'\r':command not found
- 计划任务里执行包含连接oracle命令的shell脚本报错:SP2-0667: Message file sp1<lang>.msb not found
- ubuntu终端执行shell脚本报command not found解决方法
- sublime text 3编写shell脚本执行报$'\r': command not found
- Mac执行shell脚本提示command not found的一种解决方案
- 日常潜水-20160930-Mac执行shell脚本报错_command not found
- shell脚本执行错误 $'\r':command not found
- 用shell脚本切分task_list,并分别执行的脚本
- Linux中执行shell脚本的4种方法总结
- Shell脚本执行软件安装
- Shell脚本实现每个工作日定时执行指定程序(学习记录)
- Linux执行SHELL脚本的几种方法
- shell脚本不暂停进程,暂停几秒执行下一条shell命令
- Linux执行Shell脚本问题
- 执行shell脚本报错
- 在adb shell下执行sqlite3访问数据库出现/system/bin/sh: sqlite3: not found
- 为什么直接执行shell脚本可以,但crontab下执行错误