ASarkar
ASarkar

Reputation: 519

Shell commands within if-block of gnuplot

I want to execute shell commands inside if-block of Gnuplot. I tried the following:

datatype = 'full'

if ( datatype eq 'full' ) {
    # Run shell command
    !echo 'full'
} else {
    # Run different shell command
    !echo 'not full'
}

However, this gives me the error "filename.plt" line xx: invalid command

FYI, I already know instead of !echo, I can use print to do the same thing. That's not the point. I want to use shell commands with the symbol ! within the if-block. Any help will be appreciated.

Upvotes: 3

Views: 132

Answers (1)

Ethan
Ethan

Reputation: 15093

@choroba has given the correct solution: use the system("command") function instead of !command.

As to why this is necessary, in brief:

(1) The ! operator is interpreted as indicating a shell command only if it is the first token on a gnuplot input line. Otherwise it is interpreted as a logical NOT.

(2) Gnuplot bracketed clauses { lots of commands } are treated internally as a single long input line. This is an implementation detail that has changed over time. The safest general guideline is that a bracketed clause should not contain any syntax that is documented as acting on or affecting a single line of input. This includes @ macro substitution, single-line if statements, and as you found, the ! operator.

(1+2) Therefor a ! inside a bracketed clause is not seen as being the first token, and is interpreted as a logical NOT operator instead.

Upvotes: 4

Related Questions