0

I am using a bash script to execute a program. The program must take the following argument. (The program is gnuplot.)

gnuplot -e "filename='output_0.csv'" 'plot.p'

I need to be able to assemble the following string: "filename='output_0.csv'"

My plan is to assemble the string STRING=filename='output_0.csv' and then do the following: gnuplot -r "$STRING" 'plot.p'. Note I left the words STRING without stackoverflow syntax style highlighting to emphasise the string I want to produce.

I'm not particularly proficient at bash, and so I have no idea how to do this.

I think that strings can be concatenated by using STRING="$STRING"stuff to append to string? I think that may be required?

As an extra layer of complication the value 0 is actually an integer which should increment by 1 each time the program is run. (Done by a for loop.) If I have n=1 in my program, how can I replace the 0 in the string by the "string value" or text version of the integer n?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • Actually, I'm not sure this will work! Will bash interpret the "" characters correctly when passing the argument `$STRING`? – FreelanceConsultant Jun 16 '15 at 13:27
  • Does gnuplot require the quotes (single or otherwise) in the argument string? If it does then what you have planned should work fine. If it doesn't then you want to not include them in the variable value. – Etan Reisner Jun 16 '15 at 13:42
  • It seems you are on the correct path. Also read about difference between quotes. http://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – RTLinuxSW Jun 16 '15 at 13:44
  • @RTLinuxSW Thanks, that is useful to know – FreelanceConsultant Jun 16 '15 at 14:15
  • Putting quotes inside variables can be tricky. See [this answer](http://stackoverflow.com/a/9712555/1072112) for some examples of the variations. – ghoti Jun 17 '15 at 21:26

2 Answers2

0

A safest way to append something to an existing string would be to include squiggly brackets and quotes:

STRING="something"
STRING="${STRING}else"

You can create the "dynamic" portion of your command line with something like this:

somevalue=0
STRING="filename='output_${somevalue}.csv'"

There are other tools like printf which can handle more complex formatting.

somevalue=1
fmt="filename='output_%s.csv'"

STRING="$(printf "$fmt" "$somevalue")"

Regarding your "extra layer of complication", I gather that this increment has to happen in such a way as to store the value somewhere outside the program, or you'd be able to use a for loop to handle things. You can use temporary files for this:

#!/usr/bin/env bash

# Specify our counter file
counter=/tmp/my_counter

# If it doesn't exist, "prime" it with zero
if [ ! -f "$counter" ]; then
  echo "0" > $counter
fi

# And if it STILL doesn't exist, fail.
if [ ! -f "$counter" ]; then
  echo "ERROR: can't create counter." >&2
fi

# Read the last value...
read value < "$counter"

# and set up our string, per your question.
STRING="$(printf "filename='output_%d.csv'" "${value}")"

# Last, run your command, and if it succeeds, update the stored counter.
gnuplot -e "$STRING" 'plot.p' && echo "$((value + 1))" > $counter

As always, there's more than one way to solve this problem. With luck, this will give you a head start on your reading of the bash man page and other StackOverflow questions which will help you learn what you need!

ghoti
  • 45,319
  • 8
  • 65
  • 104
0

An answer was posted, which I thought I had accepted already, but for some reason it has been deleted, possibly because it didn't quite answer the question.

I posted another similar question, and the answer to that helped me also answer this question. You can find said question and answer here: bash: Execute a string as a command

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225