在 bash 脚本中,查看并使用整个命令行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13520601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:50:54  来源:igfitidea点击:

in bash script, see and use the exact entire command line

bashcommand-lineparameters

提问by user1070300

Is there any way to access the exact command line as typed?

有什么方法可以访问输入的确切命令行吗?

(After variable expansion is OK)

(变量展开后就OK了)

It's the only way I can think of to achieve the following:

这是我能想到的实现以下目标的唯一方法:

myscript.sh file1 file2 file3 -m"A multi word comment" [other parameters/options]

-- and inside the script we effectively call --

-- 在我们有效调用的脚本中 --

svn commit file1 file2 file3 -m"A multi word comment" [other parameters/options]

-- without having to duplicate all the parsing that svn does itself, just to make sense of the command line

-- 不必复制 svn 自己做的所有解析,只是为了理解命令行

(The problem I have is that in the example, $4 is going to be "-mA multi word comment" which is going to be a mess to sort out. I'd rather be able to shoot the whole command-line, quotes and all, straight to svn just exactly how svn will want to see it.)

(我遇到的问题是,在这个例子中,$4 将是“-mA 多字评论”,这将是一个混乱的整理。我宁愿能够拍摄整个命令行,引号和所有,直接到 svn 正是 svn 想要看到它的方式。)

If anyone knows a better way to write an svn "wrapper", I'd also like to hear it.

如果有人知道编写 svn“包装器”的更好方法,我也想听听。

回答by sampson-chen

Have you tried in your script:

您是否在脚本中尝试过:

svn commit "$@"

Explanation:

解释:

  • $@is a special variable that containing all the arguments to the shell, starting at $1.
  • $@是一个特殊变量,它包含 shell 的所有参数,从 $1 开始。

回答by Michael Krelin - hacker

Inside effectively call you can:

内部有效调用您可以:

svn commit "$@"

As for seeing exactly how it is typed — no. You can't tell a bfrom a b(note one and two spaces between parameters —?argh, I think even SO hides the second space ;)). But you don't need to.

至于确切地看到它是如何输入的 - 不。你不能a ba b(注意参数之间的一个和两个空格 -?啊,我认为即使 SO 隐藏了第二个空格 ;))。但你不需要。

回答by user1070300

if the script itself doesn't need the params, another way is to use alias, like this

如果脚本本身不需要参数,另一种方法是使用别名,就像这样

alias commit='code_check && svn ci'

(where code_check is the script or function to execute before the svn commit)

(其中 code_check 是要在 svn 提交之前执行的脚本或函数)

you can then call

然后你可以打电话

commit file1 file2 file3 -m"A multi word comment" [other parameters/options]

and the alias expansion turns the line into

并且别名扩展将行变成

code_check && svn ci file1 file2 file3 -m"A multi word comment" [other parameters/options]