Debian系统中执行shell报错unexpected operator
文章发布较早,内容可能过时,阅读注意甄别。
新的 Jenkins 测试环境通过 docker 搭建,官方镜像的基础镜像使用的是 Debian 系统,在执行到一些 shell 步骤的时候,总是会报如下错误:
/var/jenkins_home/workspace/ops/test-eryajf-blog@tmp/durable-8207aae1/script.sh: 5: [: ALL: unexpected operator
1
这是由于 Debian 系统以及 Ubuntu 系统在最近新系统中,默认的 sh 软链到的是 dash,而非 bash。
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 28 08:00 /bin/sh -> dash
1
2
2
通过执行如下命令,可以将 sh 软链到 bash 上。
$ dpkg-reconfigure dash
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.)
debconf: falling back to frontend: Readline
Configuring dash
----------------
The system shell is the default command interpreter for shell scripts.
Using dash as the system shell will improve the system's overall performance. It does not alter the shell presented to interactive users.
Use dash as the default system shell (/bin/sh)? [yes/no] no 需在此处输入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'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
更改成功之后,再看一下当前 sh 的软链:
ls -l /bin/sh
lrwxrwxrwx 1 root root 4 May 21 22:35 /bin/sh -> bash
1
2
2
然后再运行脚本就不会报刚刚那个错了。
上次更新: 2024/11/19, 23:11:42