庆典:执行字符串命令 [英] bash: Execute a string as a command

查看:83
本文介绍了庆典:执行字符串命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅我的previous问题上装配一个特定的字符串<一个href=\"https://stackoverflow.com/questions/30868964/bash-how-can-i-assemble-the-string-filename-output-0-csv\">here.

我获得了一个回答这个问题,但不幸的信息实际上并没有帮助我完成我试图实现的。

使用由该职位的信息,我已经能够收集下列组字符串: gnuplot的-e文件名='output_N.csv'plot.p其中N是由字符串重新整数presentation取代。

下面的循环将解释:(其实,有可能做这个循环,你可能要指出的一个更好的方式 - 希望下面的code会不会不高兴的人太多了......)

  1#!/斌/庆典
  2 N = 0
  3为在输出f作_ *。CSV
  4做
  5 FILE =\\文件名='_输出$ N的.csv'\\
  6 SCRIPT ='plot.p'
  7 COMMAND =gnuplot的-e $文件$ SCRIPT
  8 $ COMMAND
  9 N = $(($ N + 1))
 10 DONE

不幸的是这并没有工作... gnuplot在运行,但提供了以下错误信息:

 文件名='output_0.csv'
^
行0:无效的命令
文件名='output_1.csv'
^
行0:无效的命令
文件名='output_2.csv'
^
行0:无效的命令
文件名='output_3.csv'
^
行0:无效的命令...

所以,正如我以前说过,我在bash没有专家。我的猜测是,东西是不是跨$ P $正确PTED - 要么东西被间preTED因为它不应该或它是不是被PTED作为一个字符串,它应该跨$ P $字符串? (只是一个猜测?)

我怎样才能解决这个问题呢?

第几(相关)我gnuplot的脚本行如下:

(注意,这里使用的变量文件名这是进入作为命令行参数。请参阅本<一个href=\"https://stackoverflow.com/questions/12328603/how-to-pass-command-line-argument-to-gnuplot\">link.)

  30配合F(X)使用的文件名1:4:9通过B,C,E
31
32名积1使用每N_STEPS:4:9 yerrorbars标题为RK45数据,F(x)的标题为Landau模型


解决方案

容易修复 - 我做了带引号的错误。 (

从本质上讲,唯一的原因引号中的文本,需要文件名='_输出$ N的.csv是这样,这串由bash的跨preTED正确,执行命令前!因此,我们确实是正确的,当程序运行时的命令 gnuplot的-e文件名='output_0.csv'plot.p输入到终端直接,但预先组装字符串时引号不是必需的。 (这是一个有点难以解释,但希望它是在你的头脑中2之间的区别清楚。)

所以上面的code的修正版本是:

  1#!/斌/庆典
  2 N = 0
  3为在输出f作_ *。CSV
  4做
  5 FILE =文件名='_输出$ N的.csv'
  6 SCRIPT ='plot.p
  7 COMMAND =gnuplot的-e $文件$ SCRIPT
  8 $ COMMAND
  9 N = $(($ N + 1))
 10 DONE

,现在是纠正和工作。注意去除逃脱双引号的。

See my previous question on assembling a specific string here.

I was given an answer to that question, but unfortunately the information didn't actually help me accomplish what I was trying to achieve.

Using the info from that post, I have been able to assemble the following set of strings: gnuplot -e "filename='output_N.csv'" 'plot.p' where N is replaced by the string representation of an integer.

The following loop will explain: (Actually, there is probably a better way of doing this loop, which you may want to point out - hopefully the following code won't upset too many people...)

  1 #!/bin/bash
  2 n=0
  3 for f in output_*.csv
  4 do
  5     FILE="\"filename='output_"$n".csv'\""
  6     SCRIPT="'plot.p'"
  7     COMMAND="gnuplot -e $FILE $SCRIPT"
  8     $COMMAND
  9     n=$(($n+1))
 10 done

Unfortunately this didn't work... gnuplot does run, but gives the following error message:

"filename='output_0.csv'"
^
line 0: invalid command


"filename='output_1.csv'"
^
line 0: invalid command


"filename='output_2.csv'"
^
line 0: invalid command


"filename='output_3.csv'"
^
line 0: invalid command

...

So, as I said before, I'm no expert in bash. My guess is that something isn't being interpreted correctly - either something is being interpreted as a string where it shouldn't or it is not being interpreted as a string where it should? (Just a guess?)

How can I fix this problem?

The first few (relevant) line of my gnuplot script are the following:

(Note the use of the variable filename which was entered as a command line argument. See this link.)

30 fit f(x) filename using 1:4:9 via b,c,e
31 
32 plot filename every N_STEPS using 1:4:9 with yerrorbars title "RK45 Data", f(x) title "Landau Model"

解决方案

Easy fix - I made a mistake with the quotation marks. ("")

Essentially, the only reason why the quotation marks " and " are required around the text filename='output_"$n".csv' is so that this string is interpreted correctly by bash, before executing the command! So indeed it is correct that the program runs when the command gnuplot -e "filename='output_0.csv'" 'plot.p' is entered into the terminal directly, but the quotation marks are NOT required when assembling the string beforehand. (This is a bit difficult to explain, but hopefully it is clear in your mind the difference between the 2.)

So the corrected version of the above code is:

  1 #!/bin/bash
  2 n=0
  3 for f in output_*.csv
  4 do
  5     FILE="filename='output_"$n".csv'"
  6     SCRIPT='plot.p'
  7     COMMAND="gnuplot -e $FILE $SCRIPT"
  8     $COMMAND
  9     n=$(($n+1))
 10 done

That is now corrected and working. Note the removal of the escaped double quotes.

这篇关于庆典:执行字符串命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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