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:

#!/bin/sh
### copy to dom0
src_file_fldr='/home/user/Documents/'
d_file_fldr='/home/a1/Documents/'
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
src_path=$src_file_fldr$src_file
echo 'Source path: '$src_path
d_path=$d_file_fldr$src_file
echo 'Dom0 path: '$d_path
# qvm-run --pass-io $src_vm 'cat /home/user/Documents/t2.sh' > $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.

2 Likes

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

Hope these help:
https://tldp.org/LDP/abs/html/quotingvar.html
https://www.gnu.org/software/bash/manual/html_node/Quoting.html

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:

#!/bin/bash


# 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