Reputation: 145
I'm stumped by an attempt at a bash redirect attempt. I am trying to run a command that takes a redirect as a subcommand of screen
, with the redirect going to the subcommand rather than to screen
.
Here is a cleaned up version of the original command:
ssh -o "StrictHostKeyChecking no" <user>@<host> 'bash -s' < my_script.sh -- -s OPTION1 -o OPTION2
That works exactly as desired. But, my attempt at running it under screen
breaks:
screen -d -m ssh -o "StrictHostKeyChecking no" <user>@<host> 'bash -s' < my_script.sh -- -s OPTION1 -o OPTION2
I can see that now the redirect is going to screen
rather than to ssh
, but I can't figure out how to make it work the way I want it.
Upvotes: 0
Views: 398
Reputation: 295403
If you have some working code, export it as a function, and call that function from inside your subshell started by screen
. That way your code will run precisely as it would without screen involved.
#!/usr/bin/env bash
# ^^^^- IMPORTANT: 'export -f' requires the parent and child shells to both
# persist functions in the environment in the same way. If the child is
# bash, the parent must be bash too!
option1=$1
option2=$2
runCommand() {
[[ $user && $host ]] || { echo "ERROR: user and host not exported" >&2; return 1; }
option1=$1; option2=$2
printf -v cmd_str '%q ' -s "$option1" -o "$option2"
ssh -o "StrictHostKeyChecking no" "${user}@${host}" \
"bash -s -- $cmd_str" <my_script.sh
}
export -f runCommand
screen -d -m bash -c 'runCommand "$@"' _ "$option1" "$option2"
If your code uses variables you aren't showing us, be sure to export
them too, so the exported function can access them.
Upvotes: 2