Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 364
ASarkar Avatar asked Jul 01 '26 21:07

ASarkar


1 Answers

@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.

like image 173
Ethan Avatar answered Jul 03 '26 10:07

Ethan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!