出于兼容性原因,我正在移动一个bash脚本.是否有POSIX/Dash替代以下比较?
COMPARE_TO="^(lp:~?|https?://|svn://|svn\+ssh://|bzr://|bzr\+ssh://|git://|ssh://)"
if [[ $COMPARE =~ $COMPARE_TO ]]; then
echo "WE ARE COMPARED!"
fi
Run Code Online (Sandbox Code Playgroud) 我的公司开始增加我们的Linux支持,我们的一些shell脚本开始变得更加复杂.今天我们用shebang作为
#! /bin/sh
Run Code Online (Sandbox Code Playgroud)
但是我们遇到像Ubuntu这样的发行版的问题,例如sh指向破折号和一些更具异国情调的发行版.
我的问题是,例如,将shebang改为bash的风险是什么?也许更好的问题是,如果这些天可以有一个没有bash的发行版?
当我有文件a.txt,b.txt并且c.txt是保证该
cat *.txt > all_files.txt
Run Code Online (Sandbox Code Playgroud)
要么
cat ?.txt > all_files.txt
Run Code Online (Sandbox Code Playgroud)
将按字母顺序组合文件?
(在我的所有测试中,字母顺序都被保留了,但是我不确定,因为例如ls订单是未定义的并且不需要是字母 - 但它经常是,因为文件经常按字母顺序写入目录)
我的意思是我想使用unset它本身不是shell函数.如果我能做到这一点,我可以command通过跑步来确保它是纯粹的
#!/bin/sh
{ \unset -f unalias command [; \unalias unset command [ } 2>/dev/null;
# make zsh find *builtins* with `command` too:
[ -n "$ZSH_VERSION" ] && options[POSIX_BUILTINS]=on
Run Code Online (Sandbox Code Playgroud)
如果我使用的是Debian Almquist shell(破折号),我认为我可以依赖它\unset是纯粹的.至少我不能定义一个名为shell函数unset在dash.而在我bash或之中zsh我可以定义unset() { echo fake unset; },然后我无法取消该功能:\unset -f unset输出"假未设置".
与此相关,在bash脚本中,可以导出函数,export -f <function name>以便可以在bash脚本调用的脚本中使用它.但是,这在dash脚本中不起作用.我想知道,如果我不得不担心一个命令被定义为我写的脚本文件外的shell函数,如果我正在使用dash?其他POSIX兼容的shell怎么样?
在bash中,您可以使用(例如)为输出着色
echo -e "\e[34mblue text\e[0m"
Run Code Online (Sandbox Code Playgroud)
但这不适用于破折号。
有没有办法获得带有破折号的彩色输出?
我目前使用 dash 作为主 shell。
我尝试编写一个应该模仿等待的小函数,但带有一些文本。
这是一个最小的工作代码:
#!/bin/dash
wait() {
echo Waiting...
local pid="${1}"; shift
local delay=.250
while kill -0 "${pid}" 2>/dev/null; do
echo Still waiting...
sleep "${delay}"
done
echo Resuming
}
main() {
sleep 3 &
wait %1
}
main
Run Code Online (Sandbox Code Playgroud)
如果将其复制粘贴到破折号外壳中,您可以看到代码运行得很好。
无论如何,如果您尝试将其保存在文件中,则不会。
经过一些故障排除后,我发现删除2>/dev/null,您可以看到一条错误消息:kill: No such process,但使用command wait "${pid}"它只是等待它。
例如:
#!/bin/dash
wait() {
echo Waiting...
local pid="${1}"; shift
command wait "${pid}"
echo Resuming
}
main() {
sleep 3 &
wait %1
}
main …Run Code Online (Sandbox Code Playgroud) 有以下内容:
#!/usr/bin/env dash
seq -w 10 | while read -r num
do
echo $num: $((num + 1))
done
Run Code Online (Sandbox Code Playgroud)
版画
01: 2
02: 3
03: 4
04: 5
05: 6
06: 7
07: 8
sd: 3: sd: Illegal number: 08
Run Code Online (Sandbox Code Playgroud)
谁能解释一下上面的dashartihmetic有什么问题?
请注意,它是一个破折号(不是bash)脚本.
也标记了它,bash也是来自bash专家的更多关注.:)
我有一个Shell脚本,可以从cURL读取多行JSON字符串:
r=$(curl -H "X-Vault-Token: $VAULT_TOKEN" -fs $VAULT_ADDR/v1/$1)
Run Code Online (Sandbox Code Playgroud)
如果运行echo $r并执行脚本,/bin/sh我会发现JSON中的换行符被解释为换行符:
{"data": {"value": "foo
bar"}}
Run Code Online (Sandbox Code Playgroud)
但是当我使用/bin/bash换行符执行脚本时,它们是原义的:
{"data": {"value": "foo\nbar"}}
Run Code Online (Sandbox Code Playgroud)
我希望在使用/bin/sh和时,换行符是原义的/bin/bash。如何停止/bin/sh解释换行符?
file -h /bin/sh
/bin/sh: symbolic link to dash
Run Code Online (Sandbox Code Playgroud) bash的标准解决方案,请参阅:
https://askubuntu.com/questions/15853/how-can-a-script-check-if-its-being-run-as-root
这是:
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
在dash中不起作用,它正慢慢成为Linux下的标准shell.如何将上述内容移植到破折号?
我有这个示例代码:
find "$1" ! -regex "$regex" 2>/dev/null | while read line ; do
a="$line"
done
echo ("$a") # prints nothing because of subshell
Run Code Online (Sandbox Code Playgroud)
我需要:
$a外部(在全局范围内)可见的解决方法我怎样才能做到这一点?有什么简单的解决办法吗?
我需要在 shell 脚本 ( ) 中进行字符串操作/bin/dash:
#!/bin/sh
PORT="-p7777"
echo $PORT
echo ${PORT/p/P}
Run Code Online (Sandbox Code Playgroud)
最后一个回显失败并显示Bad substitution。当我将 shell 更改为 bash 时,它可以工作:
#!/bin/bash
PORT="-p7777"
echo $PORT
echo ${PORT/p/P}
Run Code Online (Sandbox Code Playgroud)
如何实现 中的字符串替换dash?
我有一个简单的debug_print shell函数,可用于编写较大的shell脚本。通常,我尝试仅编写兼容Bourne的脚本,并避免使用Bash-isms。在这种情况下,我的debug_print函数的Bash版本可以正常工作,但是我无法终生弄清楚为什么我的Bourne兼容版本失败了。具体来说,我的问题是在printf中使用%s格式修饰符。
我在下面包括了我的测试脚本。debug_print函数显示带有标签的标题栏,其字符串或文件参数的内容,最后显示与标题宽度相同的页脚栏。问题在于显示页脚栏,并以注释“显示页脚行...”开始。我认为以下两行未注释的行应该起作用,但行不通。
接下来,在“没有这些工作”注释下的几行注释行中,您可以看到我在试图找出问题时使用的各种测试/方差。这些行之后是有效的Bourne版本,最后是有效的Bash版本。
我已经仔细阅读了联机帮助页,并尝试了各种格式的字符串,引号和我能想到的变量用法。到目前为止,这些尝试都没有奏效。在“没有这些工作”注释下,“无工作”命令全部打印出整个字符串,而我认为应该打印整个字符串,而不是仅打印由字段宽度修饰符指定的字符数。
为了确定“伯恩兼容性”,我在系统上使用破折号作为/ bin / sh,这是Debian系统的典型特征。Dash是为了有效执行脚本而不是交互使用。根据该手册页,它努力实现严格的Bourne兼容性,因此我认为,如果我的脚本使用破折号,则它应该是Bourne合理的兼容对象,并且不包含bash行为。此外,我认识到此功能并不是调试打印的全部/全部,并且可能会有很多特殊情况和需要改进的地方。我欢迎所有建议,但是我对此printf问题特别感兴趣。鉴于这应该多么简单,看来我一定犯了一些愚蠢的错误,但我无法发现它。
这是我的debug_print测试脚本:
#!/bin/sh
#
# Purpose: Debug print function. Outputs delimiting lines and will display
# either the contents of a file or the contents of a variable.
#
# Usage: debug_print label input
# Where label is the title to print in the header bar, usually the name of
# the input variable. Input is either a file on disk to be printed, or,
# if the …Run Code Online (Sandbox Code Playgroud)