Вопрос

I'm using debug mode in a BASH script to trace the commands that have been executed by the script. So far this has all worked very well but now I need to run a command with some options contained in a string and that string contains quotes that seem to cause a problem in debug mode.

Here is a short example

#!/bin/bash
job_opts='-q long -M5000000 -R"select[mem>5000] rusage[mem=5000]"'
set -x
bsub $job_opts # .... more options for job here
set +x

This script generates submissions to lsf job queues using the bsub command but that shouldn't matter. On STDERR I see this trace:

+ bsub -q long -M5000000 '-R"select[mem>5000]' 'rusage[mem=5000]"' 
Job submission rejected.

The bsub command was supposed to look like this:

+ bsub -q long -M5000000 -R"select[mem>5000]' 'rusage[mem=5000]"

So my bsub command has failed and I can also see that this is because it didn't see the -R switch, presumably because of the additional single quotes around the whole switch.

I understand that BASH puts single quotes around quoted strings in debug mode but I didn't expect that this would affect the actual command that is being issued, which appears to have happened.

Am I doing somethng wrong here or is there any way of avoiding this extra quoting in BASH debug mode? It's just very convenint to log commands in this way, so would be a shame if I couldn't do it anymore just because of those pesky quotes.

Thanks for your help!

Это было полезно?

Решение

You cannot do that with plain variables, the shell will parse it to separate arguments. You can do eval, but I wouldn't recommand it:

eval bsub $job_opts

You can do it with an array: http://mywiki.wooledge.org/BashFAQ/050

job_opts=("-q" "long" "-M5000000" '-R"select[mem>5000] rusage[mem=5000]"')
bsub "${job_opts[@]}"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top