Troubleshooting Copy To Dom0 Script

How would you parse the copy to dom0 command using variables?
Specifically ‘cat $src_path’ > file/path/here?

The script below takes appVm and file name inputs, and tries to parse them into the copy to dom0 command.

All parts to the parsing work, except when ‘cat $src_path’ >.

Any ideas what the proper syntax of this line should be?

qvm-run --pass-io $src_vm 'cat $src_path' > $d_path

Full script in context:

### copy to dom0
echo "Source file must be in appVm $src_file_fldr"
read -e -p "Enter the source appVm name: " src_vm
read -e -p "Enter the source file name: " src_file
echo 'Source path: '$src_path
echo 'Dom0 path: '$d_path
# qvm-run --pass-io $src_vm 'cat /home/user/Documents/' > $d_path
###^^^^ manually entering the file path works.
qvm-run --pass-io $src_vm 'cat $src_path' > $d_path
####^^^^ trying to use a variable doesn't.
echo 'Copied from '$src_vm' '$src_path' 'to' '$d_path

qvm-run --pass-io $src_vm "cat $src_path" > $d_path

Syntax is correct.


(Good… God… a hour trying to figure it out, and it was just “” vs ‘’). When to use “” vs ‘’?)

Hope these help:

1 Like

Probably tmi, but here’s one more:

I ran into this stuff when using sed to filter rsync stdout.
One of the responders in the link above built a table for himself, That may be a good idea; Build a table with examples that work for you. Building one for myself was a learning experience.

1 Like

For simple things where you need to include a variable, double quotes works.

However…bash quoting can get really complex really fast if your text or variables may contain quotes or other special characters in them (e.g. things like filenames or passwords).

I found the function in the first snippet somewhere on the web (here: shell - Escape double quotes in variable - Unix & Linux Stack Exchange ) and it was useful enough to keep:


# below function takes incoming text and 
#   1) replaces all ' with '"'"', then 
#   2) adds ' to the beginning and 
#   3) adds ' to the end 
# This creates a quoted string that can be passed by bash to other programs, 
# avoiding special character expansion (regardless of original content).

esc() {
        printf "%s" "$1" | sed -e "s/'/'\"'\"'/g" -e "1s/^/'/" -e "\$s/\$/'/" 

Example usage:

# using escaping to create this variable value, sans brackets [test 1/23 '" ]
value='test 1/23 '"'"'" '
echo ${value}  

escaped_value_with_single_quotes=$(esc "${value}")

# the value below may be safer to use later in the script if you are constructing 
# a commandline to another program.
echo ${escaped_value_with_single_quotes} 
1 Like