I have a bash script running in dom0.
It needs to run one of its functions in a qube.
How is the best way to pass that function to the qube and execute it?
There is not only one solution, this is how I do it:
code example
[user@dom0 ~]$ cat x.sh
#!/usr/bin/bash
foo ()
{
echo bar
}
qvm-run --pass-io --no-gui --user root some_qube "
$(declare -f foo)
foo"
[user@dom0 ~]$ ./x.sh
bar
note that with this method, you can’t use external variable in foo ()
.
edit:
well you can, but you need to declare them in the qube.
code example
[user@dom0 ~]$ cat x.sh
#!/usr/bin/bash
some_var=42
foo ()
{
echo bar
echo $some_var
}
qvm-run --pass-io --no-gui --user root some_qube "
some_var=$some_var
$(declare -f foo)
foo"
[user@dom0 ~]$ ./x.sh
bar
42
Nice solution, never even thought about passing code inside the qube. I use scripts inside it and pass variables as arguments from dom0
.
Side note: be aware, bash
is a one huge pitfall, it is best to use awesome shellcheck
and follow all its recommendations when writing bash scripts.
In this line currently everything works, but at the moment when somewhere above the value of ${somevar} will be modified to something like'42 some_dangerous_or_random_stuff
, the problems will occur. Especially this is the case when value is provided by user or third-party.
It is advised to quote everything (even 42 if you use it logically as a string, for consistency) and use braces everywhere (even args like ${0}, once again for consistency):
echo "${some_var}"
...
some_var=\"${some_var}\" # because it is inside double quotes already
Quoting everything would also allow MrMEEE to completely avoid the most infamous github commit/bug:
In this case the string was also hard-coded and expected to have no spaces, that’s why the MrMEEE skipped this recommendation with quotes. But bash never forgives.