系统版本为:ubuntu18.04稳定版
1. 例子,输入一个for循环,实用(
括号
#!/bin/bash
for i in (1..6)
do
echo $i
done
2.显示报错信息,bash sh ./ 三种方法都不能运行,vi,vim编辑器都不行
$bash test2.sh
test2.sh: line 2: syntax error near unexpected token `('
test2.sh: line 2: `for i in (1..6)'
$sh test2.sh
test2.sh: 2: test2.sh: Syntax error: "(" unexpected
$./test2.sh
./test2.sh: line 2: syntax error near unexpected token `('
./test2.sh: line 2: `for i in (1..6)'
3. 列出所有shell版本的指向
$ls -l /bin/*sh
-rwxr-xr-x 1 root root 1113504 Apr 5 02:30 /bin/bash
-rwxr-xr-x 1 root root 121432 Jan 25 15:14 /bin/dash
lrwxrwxrwx 1 root root 4 Jun 3 22:05 /bin/rbash -> bash
lrwxrwxrwx 1 root root 4 Jun 3 22:05 /bin/sh -> dash
lrwxrwxrwx 1 root root 7 Jun 3 22:05 /bin/static-sh -> busybox
4.
$su root
Password:
root@tssys:/home/toucan# dpkg-reconfigure dash
# 选择no
输出显示
Removing 'diversion of /bin/sh to /bin/sh.distrib by dash'
Adding 'diversion of /bin/sh to /bin/sh.distrib by bash'
Removing 'diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by dash'
Adding 'diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by bash'
5. 再次查看shell版本,没有改变
root@tssys:/home/toucan# ll /bin/*sh
-rwxr-xr-x 1 root root 1113504 4月 5 02:30 /bin/bash*
-rwxr-xr-x 1 root root 121432 1月 25 15:14 /bin/dash*
lrwxrwxrwx 1 root root 4 6月 3 22:05 /bin/rbash -> bash*
lrwxrwxrwx 1 root root 4 6月 9 18:45 /bin/sh -> bash*
lrwxrwxrwx 1 root root 7 6月 3 22:05 /bin/static-sh -> busybox*
6. 重启电脑
7.测试脚本
原来不能运行的,现在都可以运行了
cat test6.sh
#!/bin/bash
A=(a b c d e)
echo ${A[@]}
no1=1
no2=2
result=$[no1 + no2]
echo $result
a b c d e
3
8. 再次选择Yes,看是否这是影响因素
root@tssys:/home/toucan# dpkg-reconfigure dash
# 选择Yes
Removing 'diversion of /bin/sh to /bin/sh.distrib by bash'
Adding 'diversion of /bin/sh to /bin/sh.distrib by dash'
Removing 'diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by bash'
Adding 'diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by dash'
显示结果:
刚刚还能执行的脚本,现在不能执行了
$sh test6.sh
test6.sh: 2: test6.sh: Syntax error: "(" unexpected
9.原因
因为Ubuntu/Debian为了加快开机速度,用dash代替了传统的bash,是dash在捣鬼,解决方法就是取消dash。
dpkg-reconfigure dash
# 选择no