Home  >  Q&A  >  body text

shell - About the use of $ variable in the string "" and '' in linux scripts

About the problem of variable $foo in "" quotes in Linux

Now we get an incoming parameter from the command line $1, assuming the value of $1 is 666
We want to use $1 in sed -i, assuming the command is sed -i " s/foo/$1/g” file.txt
Now we need to write an ant build.xml (can be understood as a script), and we need to implement the sed instruction:

<exec executable="/usr/bin/sed" dir="common/self_cleaning_builder">
    <arg line="#我需要的那条sed指令"/>
</exec>

我应该如何把这个sed指令放入arg标签下的line=“”中,才能使得我放进去的指令可以被正确读出且实现功能?引号太多了,怎么写都实现不了功能。

What I wrote is:

<exec executable="/usr/bin/sed" dir="/common">
    <arg line='-i -r "s/foo//g" file.txt'/>
</exec>

as well as

<property name="regex" value="s/input//g"/>
<exec executable="/usr/bin/sed" dir="/common">
    <arg line="-i -r $regex file.txt"/>
</exec>

But none of them can achieve the function I want.

After reading some posts about “” and ‘”, I still don’t know how to solve this problem. I hope you can give me some advice

女神的闺蜜爱上我女神的闺蜜爱上我2639 days ago805

reply all(2)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-07-03 11:43:33

    Just write a wrapper for sed. If you use sed directly like this, you will first get the parameters and
    think it is a file, $1 will not work

    reply
    0
  • PHP中文网

    PHP中文网2017-07-03 11:43:33

    $1 is an input parameter in the shell script, but not in the ant script. In addition, when you call ant from the command line, it is impossible to pass parameters by position. You must specify the parameter name:

    ant <your target> -Darg1=value1

    Use ${arg1} to access variables in ant script:

    <arg line="-i -r 's/foo/${arg1}/g' file.txt"/>

    reply
    0
  • Cancelreply