0

I have a command stored in a text file command which looks like this:

command_name par1 par2 path/with/backslashes -option_1 3 -option_2 0.05 --option_3=* -p 'P001=A' -p 'P002=B' ...

The three dots signify there's a bunch of other parameters of one of the forms above.

It is a lengthy command.

If, on a terminal, I just:

cat command

select the shell output, paste it and hit enter, the invoked tool works properly and does what's intended to do.

If, on the other hand, on my bash shell I type:

$(cat command)

or

$(< command)

Then the tool returns an error, specifically that it cannot find a label:

tool_name: label "A'" not found

So, interestingly, it seems to take the closing quote in the -p 'P001=A' command option above as part of the label. This got me thinking that maybe there is some subtlety in the cat output that changes what is passed to the shell for execution?

If I type (this is bash):

. command

then again, everything works fine.

The tool can be configured as to output the actual running command, and here's another interesting thing:

The command out of the $(cat command) line is exactly that of the text file, i.e. the tool outputs something like this:

Commmand line for tool_name version 6.0
----------
command name par1 par2 path/with/backslashes -option_1 3 -option_2 0.05 --option_3=* -p 'P001=A' -p 'P002=B' ...
-----------

All quotes are kept.

On the other hand, if I copy-paste the output of cat command or I use . command in the bash shell, then the same line looks like this:

Commmand line for tool_name version 6.0
----------
command name par1 par2 path/with/backslashes -option_1 3 -option_2 0.05 --option_3=* -p P001=A -p P002=B ...
-----------

The quotes are gone! And the tool works properly.

Any idea about what could be behind this behaviour?


P.S In case you are wondering why am I doing all this, and not just use . command, it's because I intend to use this command - as I said it's a lengthy one so I prefer to keep it in a text file - with srun. I have a similar approach with other "lengthy commands" and since srun wants a command line to be fed to it, I use the following template:

srun -p my_queue -option_1 $(cat my_lengthy_cmd) &

This works absolutely fine for other commands/tools. It's only this specific one which is giving me issues.

muru
  • 72,889
  • If you change the file to contain echo command_name ..., does the output of $(< command) look correct? I'm thinking particularly about asterisks expanding into lists of files (or lists of no files). – glenn jackman Dec 15 '23 at 18:59
  • Along the same lines, if you type "set -f" before you do the $(cat command) and set +f after it, does it help? – icarus Dec 15 '23 at 19:28
  • 1
    Quotes from the expansion of $(…) are not special to the shell. – Kamil Maciorowski Dec 15 '23 at 19:59
  • How about bash < command or sh < command? Heck if srun is smart enough to read stdin, even srun -p my_queue -option_1 < my_lengthy_cmd – Jim L. Dec 15 '23 at 20:02
  • @glennjackman if I change my command file inserting an echo just before the command itself, the ouptut of $(< command) is a string where the -p = 'P001=A' appears, just like the output of cat. – Michele Ancis Dec 15 '23 at 21:00
  • @icarus I do set +f;$(cat command);set -f (that's my interpretation of what you're suggesting) - no luck, same error. – Michele Ancis Dec 15 '23 at 21:02
  • @JimL. srun does not appear clever enough to read stdin, so last proposal does not work. However, srun -p my_queue -option_1 bash < command does work!! – Michele Ancis Dec 15 '23 at 21:02
  • hm, a command substitution is only followed by word splitting and file name expansion, there is no quote removal, if you write $(echo 'hello' 'bello') and hit enter, you will get 'hello' 'bello', there is nothing magic about it, you can use bash -c "$(cat command)" – Schmaehgrunza Dec 16 '23 at 20:18
  • sorry meant $(echo "echo 'hello' 'bello'") – Schmaehgrunza Dec 16 '23 at 20:27
  • Sorry, you have the + and - the wrong way around. set -f; $(cat command)l set +f. The intention is to suppress the meaning of characters like * in the output of the cat. – icarus Dec 18 '23 at 22:26

0 Answers0