在bash字符串连接空间,并把周围的报价 [英] String Concatenation in bash with space and putting quote around

查看:132
本文介绍了在bash字符串连接空间,并把周围的报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的<一个很好的反应href=\"http://stackoverflow.com/questions/37426985/concatenating-remaining-arguments-beyond-the-first-n-in-bash\">last问题。当时的想法是要处理N多的命令行输入,保存前9个变量,使一个字符串10起。

I got a very good response in my last question. The idea was to process N number of inputs from command line, save first 9 to variables and make a string with 10 to onward.

我觉得这是最简单的解决方案。

I found this to be the easiest solution.

var1="$1"
var2="$2"
var3="$3"
var4="public"
var5="$5"
var6="''"
var7="$7"
var8="$8"
var9="$9"
var10="$(shift 9; IFS=""; echo "$*")"

echo snmptrap $var1 $var2 $var3  $var4 $var5 $var6 $var7 $var8 $var9 "$var10"

snmptrap $var1 $var2 $var3  $var4 $var5 $var6 $var7 $var8 $var9 "$var10"

输出看起来像这样...

The output looks like this...

./snmptas  -v 2c -c "" 9.48.85.57 "" 1.3.6.1.4.1.2.6.201.3 s s  ABC DDEF EFFF

snmptrap -v 2c -c public 9.48.85.57 '' 1.3.6.1.4.1.2.6.201.3 s s ABCDDEFEFFF

不过,我以这种形式要$ var10

But I wanted $var10 in this form

ABC DDEF EFFF。

"ABC DDEF EFFF".

这需要改变。它采取的空间了。

This needs to changed. It's taking the spaces off.

var10="$(shift 9; IFS=""; echo "$*")"

我怎样才能让var10 =ABC DDEF EFFF?

How can I make var10 = "ABC DDEF EFFF"?

感谢

推荐答案

一个子shell回声是不是真的适合那里。更好的是这样的:

A subshell with echo is not really appropriate there. Better this way:

shift 9
var10="$*"

如果你想周围的报价为价值的一部分,然后更改最后一行:

If you want quotes around that as part of the value, then change the last line to:

var10=\""$*"\"

无关到,我想知道,如果你的治疗 var6 做你的本意。我怀疑,这将是更接近你真正想要的结果:

Unrelated to that, I'm wondering if your treatment for var6 does what you intended. I suspect that this will be closer to what you really intended:

snmptrap $var1 $var2 $var3  $var4 $var5 '' $var7 $var8 $var9 "$var10"

或者这样:

var6=
snmptrap $var1 $var2 $var3  $var4 $var5 "$var6" $var7 $var8 $var9 "$var10"

但是,如果我的猜测是错误的,你原来的治疗如预期那么没关系,忽略了这些言论。

But if my guess is wrong and your original treatment is working as intended then never mind, ignore these remarks.

这篇关于在bash字符串连接空间,并把周围的报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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