如何在csh中将字符串连接在一起?

发布于 2024-12-14 08:02:55 字数 645 浏览 2 评论 0原文

我想将字符串连接在一起以在 csh 脚本 file1.csh 中创建命令字符串。但是,csh 不断抱怨 commandString 变量错误,我真的不知道我做错了什么。这是部分代码。

set var1 = "Hat"
set var2 = 100
set embeddedString = 's/'$var1' =.*$/'$var1' = '$var2'/g'
set commandString = "sed -i ' "$embeddedString" ' productPrice.txt"
echo $commandString

我的目标是将commandString vairable 设置为以下内容

sed -i 's/Hat =.*$ /Hat = 100/g' productPrice.txt 

然后,该commandString 将被插入到另一个脚本文件file2.csh 中。 file2.csh 是实际的脚本文件,它执行帽子价格的替换命令。此外,var1和var2的值是从priceUpdateList.txt文件中读取的,因此它们不是固定值。换句话说,我不能简单地在 commandString 变量中键入 Hat 和 100。有谁知道如何正确使用引号在 csh 中生成命令字符串?

非常感谢你,

I would like to concatenate strings together to create a command string in a csh script,file1.csh. However, csh keeps complaining errors for commandString variable and I do not really know what I did wrong. Here are part of codes.

set var1 = "Hat"
set var2 = 100
set embeddedString = 's/'$var1' =.*$/'$var1' = '$var2'/g'
set commandString = "sed -i ' "$embeddedString" ' productPrice.txt"
echo $commandString

My goal is to set commandString vairable to be something as

sed -i 's/Hat =.*$ /Hat = 100/g' productPrice.txt 

Then, this commandString will be inserted into another script file,file2.csh. file2.csh is the actual script file which performs the substitution command for Hat's price. In addition, the values of var1 and var2 are read from a priceUpdateList.txt file so they are not fixed values. On other words, I can not simply type Hat and 100 in the commandString variable. Does anyone know how to use quotation correctly to generate the command string in csh ?

Thank you so very much,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

女皇必胜 2024-12-21 08:02:56

您必须引用(至少 1x)嵌入的单引号(即 cmdStr = "sed -i \' .... 要实际运行 $cmdStr,您将需要 < code>eval 对吗?

要在 csh 中使用 shell 调试(我建议查看发生了什么),请将脚本中的第一行更改为

#!/bin/csh -vx

这将显示执行时的每一行或代码块,然后是扩展了环境变量的同一代码块。

You'll have to quote (at least 1x) the embedded single-quotes (i.e. cmdStr = "sed -i \' .... To actually run the $cmdStr you're going to need eval right?

To use shell debugging in csh (which I would recommend to see what is happening), change the first line in your script to

#!/bin/csh -vx

This will show you each line or block of code as it is executed, and then the same block of code with the environment variables expanded.

悲喜皆因你 2024-12-21 08:02:56

怎么样:
设置embeddedString =“s,$ var1 =。*,$ var1 = $ var2,g”
set commandString =“sed -i '$embeddedString'productPrice.txt”

如果您愿意,您可以在嵌入字符串中使用 / 代替 , :-)

how about:
set embeddedString="s,$var1 =.*,$var1 = $var2,g"
set commandString="sed -i '$embeddedString' productPrice.txt"

you can use / instead of , in embeddedString if you want :-)

腻橙味 2024-12-21 08:02:56

抱歉,我忘了解释 Cassie 引用的 No Match
之间的区别

 echo $commandString

在此处输入代码echo "$commandString"

在于,第一个回显正确构建的命令的结果(当然不匹配,因为它需要读取一个不存在的文件`名为productPrice.txt) ,而第二个将命令嵌入双引号中,显示命令本身,这正是所要求的。

Sorry I forgot to explain the No Match quoted by Cassie
The difference between

 echo $commandString

andenter code hereecho "$commandString"

is that the first one echoes the result of the correctly built command (No match of course because it needs to read an absent file `named productPrice.txt , while the second, embedding the command in double quotes, shows the command itself, which is what was asked for.

哆兒滾 2024-12-21 08:02:56

非常简单:
您的目标是将 commandString 变量设置为如下所示

sed -i 's/Hat =.*$ /Hat = 100/g' productPrice.txt 

,并打印 commandString 以便与您的目标进行比较...

此外,您的所有引用都是无用的,除了那些分隔“最终”字符串。
但大多数情况下,请注意 .*$ 和 /$var1 之间缺少空格。足以欺骗解释器,它看到一个名为 $/ 的字符串,而字符串名称应该以字母开头。

set var1 = "Hat"
set var2 = 100
set embeddedString = "s/$var1 =.*$ /$var1 = $var2/g"
set commandString  = "sed -i $embeddedString  productPrice.txt"
echo  "$commandString"

Exceedingly simple:
Your goal is to set commandString variable to be something as

sed -i 's/Hat =.*$ /Hat = 100/g' productPrice.txt 

The below does it, and prints commandString for comparison with your goal...

Additionally all your quotes are useless save those which delimit your "final" strings.
But mostly, please note the missing space between .*$ and /$var1 . Enough to fool the interpreter which saw a string named $/ while string names ought to begin with a letter.

set var1 = "Hat"
set var2 = 100
set embeddedString = "s/$var1 =.*$ /$var1 = $var2/g"
set commandString  = "sed -i $embeddedString  productPrice.txt"
echo  "$commandString"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文