0

Using bash string variables, I am trying to run the following command in a loop after assigning these variables:

c=$((c+1))
opt_string=\"opticsGroup${c}\"
input_files=\"${line}/*.tif\"

And when I echo these variables I get the desired effect:

echo $opt_string
"opticsGroup1"
echo $input_files
"Movies/01_Optic/*.tif"

But when I try to use the variables to execute a command, they get surrounded in single quotes. Is there anyway to escape this behavior? Or might this be a problem of the script I am trying to run in the loop?

relion_import --do_movies --optics_group_name '"opticsGroup1"' --optics_group_mtf ../../../../../Processing/Jacob/MTFs/mtf_k3_300kv_standard.star --angpix 1.06 --kV 300 --Cs 2.7 --Q0 0.1 --beamtilt_x 0 --beamtilt_y 0 --i '"Movies/01_Optic/*.tif"' --odir Import/job001/ --ofile movies.star --pipeline_control Import/job001/

The way I have the script executing is as the following:

relion_import  --do_movies --optics_group_name $opt_string --optics_group_mtf ../../../../../Processing/Jacob/MTFs/mtf_k3_300kv_standard.star --angpix 1.06 --kV 300 --Cs 2.7 --Q0 0.1 --beamtilt_x 0 --beamtilt_y 0 --i $input_files --odir Import/job$k/ --ofile movies.star --pipeline_control Import/job$k/

An example of a command that runs properly is:

relion_import  --do_movies  --optics_group_name "opticsGroup1" --optics_group_mtf mtf_k3_300kv_standard.star --angpix 1.06 --kV 300 --Cs 2.7 --Q0 0.1 --beamtilt_x 0 --beamtilt_y 0 --i "001_Optic/*tif" --odir Import/job001/ --ofile movies.star --continue  --pipeline_control Import/job001/
  • 1
    Sorry, but this comes from just misunderstanding how quoting works in shell. Research when to use quotes and what do they mean, how single quotes differ from double quotes, pass your script to http://shellcheck.net and read http://mywiki.wooledge.org/Quotes . – KamilCuk Apr 07 '21 at 16:12
  • I read the wiki page. It does not address the problem of the post. How to print a string with double quotes, only, that expands a string variable. – Jacob Anderson Apr 07 '21 at 16:15
  • `when I echo these variables` You do `echo $opt_string` or `echo "$opt_string"`? – KamilCuk Apr 07 '21 at 16:18
  • 1
    Yes, it does address the problem. Lliterally the first code snippet on the wiki says: `cp $file $destination # WRONG cp -- "$file" "$destination" # Right`. ie. `echo $input_files` is wrong, `echo "$input_files"` is right. – KamilCuk Apr 07 '21 at 16:20
  • 1
    I updated the post to include the echo commands. – Jacob Anderson Apr 07 '21 at 16:20
  • `relion_import ... $opt_string` is wrong -> `relion_import ... "$opt_string"` is good. Check your script with shellcheck.net - it will point and help you with such errors. – KamilCuk Apr 07 '21 at 16:22
  • I am still getting the identical bevahior with: ```relion_import --do_movies --optics_group_name "$opt_string"```. The command printed to screen is: ```relion_import --do_movies --optics_group_name '"opticsGroup1"'``` – Jacob Anderson Apr 07 '21 at 16:25
  • 1
    Because the assignment is wrong - you included _literal_ quotes. It's: `opt_string="opticsGroup${c}"`. The quotes are removed _before_ the command is executed. – KamilCuk Apr 07 '21 at 16:26
  • Now I do not get quotes at all: ```opt_string="opticsGroup${c}"``` then ```relion_import --do_movies --optics_group_name "$opt_string"```. This yields *no* double quotes. ```relion_import --do_movies --optics_group_name opticsGroup1```. But the syntax demands it, as I show in "An example of a command that runs properly". – Jacob Anderson Apr 07 '21 at 16:28
  • 1
    `but the syntax demands it` what syntax? – KamilCuk Apr 07 '21 at 16:28
  • It needs a string passed with double quotes as in the following: ```relion_import --do_movies --optics_group_name "opticsGroup1"``` – Jacob Anderson Apr 07 '21 at 16:30
  • Can you de-link this a solved problem? That also does not solve this bash problem. Perhaps I can cat out what I want to a text file, edit that in place, then run it as bash? – Jacob Anderson Apr 07 '21 at 16:32
  • If you ask the shell to execute `relion_import --do_movies --optics_group_name "opticsGroup1"`, it will treat that string exactly the same as any of `"relion_import" "--do_movies" '--optics_group_name' opticsGroup1` or `relion_im''port --do"_mov"ies --"op"ti'c's_group_name "opticsGroup1"` or `"relion_import" '--do'_mov""ies --"opti"cs_group_name opticsGroup1`. Quoting in the shell behaves in a way that is apparently non-intuitive to many. – William Pursell Apr 07 '21 at 16:43
  • Both of your examples are still just simply not what the syntax demands, as shown in the initial question. I found a work around. Thanks. – Jacob Anderson Apr 07 '21 at 23:57

0 Answers0