似乎无法

编程入门 行业动态 更新时间:2024-10-27 06:24:24
本文介绍了似乎无法-c选项字符串后使用bash -c选项与参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于bash手册页说,对于-c选项:

Man page for bash says, regarding -c option:

-c字符串结果  如果-c选项是present,那么命令将从读  串。如果有参数之后  串,它们被分配给  位置参数,从  $ 0

-c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

所以,有了这样的描述,我想这样的事情应该工作:

So given that description, I would think something like this ought to work:

bash -c "echo arg 0: $0, arg 1: $1" arg1

但输出的只是显示下,所以它看起来像-c字符串没有被分配到位置参数后的参数。

but the output just shows the following so it looks like the arguments after the -c string are not being assigned to the positional parameters.

arg 0: -bash, arg 1:

我运行一个相当古老的bash(在Fedora 4):

I am running a fairly ancient bash (on Fedora 4):

[根@ dd42干线]#的bash --version结果  GNU bash的版本3.00.16(1)-release(I386-红帽Linux的GNU的)结果  版权所有(C)2004自由软件基金会。

[root@dd42 trunk]# bash --version GNU bash, version 3.00.16(1)-release (i386-redhat-linux-gnu) Copyright (C) 2004 Free Software Foundation, Inc.

什么我真的想在这里做的是执行位shell脚本与参数。我觉得-c看起来非常有前途的,所以上面的问题。我想知道使用eval,但我不认为我可以通过args设置为后面的eval东西。我接受其他的建议为好。

What I am really trying to do here is to execute a bit of shell script with arguments. I thought -c looked very promising, hence the issue above. I wondered about using eval but I don't think I can pass args to the stuff that follows eval. I'm open to other suggestions as well.

推荐答案

您需要使用单引号prevent插在你执行shell发生的事情。

You need to use single quotes to prevent interpolation happening in your calling shell.

$ bash -c 'echo arg 0: $0, arg 1: $1' arg1 arg2 arg 0: arg1, arg 1: arg2

或逃生的变量在双引号字符串。使用哪种可能取决于你想要把你的code的片段是什么。

Or escape the variables in your double-quoted string. Which to use might depend on exactly what you want to put in your snippet of code.

更多推荐

似乎无法

本文发布于:2023-11-10 06:41:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574640.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!