在bash函数参数preserving报价 [英] Preserving quotes in bash function parameters

查看:151
本文介绍了在bash函数参数preserving报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的就是拿,作为输入功能,一条可能包含引号(单人或双人)和回音该行正是因为它是为功能提供。例如:

What I'd like to do is take, as an input to a function, a line that may include quotes (single or double) and echo that line exactly as it was provided to the function. For instance:

function doit {
   printf "%s " ${@} 
   eval "${@}"
   printf " # [%3d]\n" ${?}
}

其中,给定下列输入

Which, given the following input

doit VAR=42
doit echo 'single quote $VAR'
doit echo "double quote $VAR"

收益率如下:

VAR=42  # [  0]
echo single quote $VAR  # [  0]
echo double quote 42  # [  0]

所以变量扩展的语义pserved如我期望$ P $,但因为它是提供的功能我不能让该行的确切格式。我想是有 DOIT回声'单引号$ VAR结果回声'单引号$ VAR

我敢肯定,这与bash的加工参数它们传递给函数之前做的;我只是在寻找办法解决(如果可能)。

I'm sure this has to do with bash processing the arguments before they are passed to the function; I'm just looking for a way around that (if possible).

所以,我本来打算是阴影脚本的执行,同时提供可以用来作为诊断工具,包括每一步的退出状态执行的翻版。

So what I had intended was to shadow the execution of a script while providing an exact replica of the execution that could be used as a diagnostic tool including exit status of each step.

虽然我的可以的做类似

while read line ; do 
   doit ${line}
done < ${INPUT}

这方法在面对控制结构(即如果,而等)失败。我想过使用设置-x 但是这有它的局限性,以及:变成 键,退出状态是不可见的失败的命令。

That approach fails in the face of control structures (i.e. if, while, etc). I thought about using set -x but that has it's limitations as well: " becomes ' and exit status is not visible for commands that fail.

推荐答案

发生这种情况的原因是bash的跨$ P $点的参数,如你想。该报价根本不存在时,它调用函数了,所以这是不可能的。它曾在DOS因为方案可以跨preT命令行本身,而不是它可以帮助你!

The reason this happens is because bash interprets the arguments, as you thought. The quotes simply aren't there any more when it calls the function, so this isn't possible. It worked in DOS because programs could interpret the command line themselves, not that it helps you!

这篇关于在bash函数参数preserving报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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