如何在较旧版本的Bash上测试我的Bash脚本? [英] How can I test my Bash script on older versions of Bash?

查看:107
本文介绍了如何在较旧版本的Bash上测试我的Bash脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Bash库,并希望确保我支持尽可能多的环境-包括旧版本的Bash.我的开发环境是Bash 4.3,但是我的一些用户可能正在运行旧版本,并且目前我无法确认或否认我的库适用于他们.特别是,我想与OSX(仍与Bash 3.2,AFAIK一起提供)兼容.

I'm working on a Bash library and want to ensure I'm supporting as many environments as possible - including old installations of Bash. My development environment is Bash 4.3, but some of my users may well be running much older versions and presently I have no way to confirm or deny that my library will work for them. In particular I'd like to be compatible with OSX (which still ships with Bash 3.2, AFAIK).

我知道Bash可以在POSIX兼容模式下运行;是否有类似的设置来禁用现代功能?还是以某种兼容模式运行Bash的方法?我正在寻找除实际查找和启动旧操作系统并在那里测试我的库以外的任何技术.

I know Bash can run in POSIX-compliant mode; is there a similar setting to disable modern functionality? Or a way to run Bash in some sort of compatibility mode? I'm looking for any technique short of actually finding and booting up old operating systems and testing my library there.

更新

例如,我避免使用关联数组因为它们是在Bash 4中引入的,但是如果不进行测试就很难确定我不会偶然使用其他Bash 4+功能.

For example, I've avoided using associative arrays since they were introduced in Bash 4, but it's hard to be sure without testing that I'm not accidentally using some other Bash 4+ feature.

推荐答案

最后回到这个问题,只需编译(不安装)

Finally coming back to this question, it's pretty easy to just compile (without installing) the bash version(s) you're interested in. Here's how I'm testing Bash 3.2.57:

$ mkdir ~/bash
$ cd ~/bash
$ wget http://ftp.gnu.org/gnu/bash/bash-3.2.57.tar.gz
$ tar xvzf bash-3.2.57.tar.gz
$ cd bash-3.2.57
$ ./configure
$ make
# if `make` fails due to yacc, run `sudo apt-get install byacc`
# No need to run `make install`
$ ./bash -version
GNU bash, version 3.2.57(1)-release (armv7l-unknown-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

现在,您可以运行bash 3.2.57二进制文件,而无需实际安装"它或修改您的正常环境.

Now you have a bash 3.2.57 binary you can run, without actually "installing" it or modifying your normal environment.

要对此版本运行shell脚本:

To run a shell script against this version:

$ ./bash your_script.sh

要输入干净的交互式提示:

$ env -i PATH="$PWD:$PATH" ./bash --noprofile --norc
bash-3.2$ bash -version
GNU bash, version 3.2.57(1)-release (armv7l-unknown-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
bash-3.2$ 

使用env -i而不是直接调用./bash会直接为您提供一个几乎为空的环境(从shell内部运行env来查看仍然设置的内容).更新PATH允许调用bash(例如bash -version)来调用本地bash shell,而不是系统范围的安装(但请注意,这会拉入您的整个PATH).添加--noprofile --norc可以避免加载.bashrc和关联的脚本.

Using env -i rather than just calling ./bash directly leaves you with a mostly-empty environment (run env from inside the shell to see what's still set). Updating the PATH allows calls to bash (e.g. bash -version) to invoke the local bash shell, not the system-wide installation (but note this pulls in your whole PATH). Adding --noprofile --norc avoids loading your .bashrc and associated scripts.

如果您不想进行任何PATH修改,只需在子Shell中执行一次export PATH="$PWD:$PATH"即可,而不是作为env命令的一部分.

If you don't want to pick up any PATH modifications, just execute export PATH="$PWD:$PATH" once inside the subshell instead of as part of the env command.

我有一个 Docker映像( repo ),如果这对人们参考很有帮助.我不一定建议直接使用该映像,但是欢迎您从Dockerfile/install脚本中进行复制.麻省理工学院许可.

I have a Docker image (repo) using these installation steps, if that's helpful for folks to reference. I wouldn't necessarily suggest using this image directly, but you're welcome to copy from the Dockerfile/install script. MIT licensed.

这篇关于如何在较旧版本的Bash上测试我的Bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆