{ Summary }
I am trying to to execute multiple programs on multiple remote machines. Each program needs to be run in a custom Environment that is not known at login time. Currently I am using ssh to connect and run the program.
My issue is that I'm not sure how setup all the Environment Variables that I need on the remote machine. I was able to write all the environment variables/values (NAME=VALUE) that I needed to a file and place on a shared file server (prog.environment).
I thought the best way to approach this would be to write my own utility (custom.shell) that would interpret the environment file and then execute the desired command.
{ custom.shell }
Code:
#!/bin/tcsh
# determine absolute path of program
set PROG=`readlink -m $0`
set CUR_DIR = $PROG:h
set DONE = 0
# Find Project Root Directory
while ($DONE == 0)
if (-e $CUR_DIR/docs/license.txt ) then
set PROJ = $CUR_DIR
set DONE = 1
else if ($CUR_DIR == "") then
echo "Error: Not a project directory."
exit
else
set CUR_DIR = $CUR_DIR:h
endif
end
while ($#argv > 0)
set arg = $argv[1];
if ($arg == "--envfile" then
shift
if ($#argv >= 1) then
set ENV_FILE = $argv[1]
# Make sure the file exists
if (-e $ENV_FILE) then
# tmpchar is needed so that the foreach process a
# whole line at a time. Damn you csh!
set tmpchar = "~"
foreach TMPLINE (`cat $ENV_FILE | tr " " "$tmpchar"`)
set LINE = `echo $TMPLINE | tr "$tmpchar" " " | sed 's/=/ /'`
if ($LINE != "") then
set ENV_NAME = `echo $LINE | cut -d' ' -f1`
set ENV_VAL = `echo $LINE | cut -d' ' -f2-`
setenv $ENV_NAME $ENV_VAL
endif
end
else
echo "Error: $ENV_FILE does not exist."
exit
endif
else
echo "Error: Filename not provided"
endif
else if ($arg == "-c") then
shift
if ($#argv >= 1) then
set COMMAND="$ARG"
else
echo "Error: No command specified"
exit
endif
else
echo "Error: Unknown argument: $arg"
endif
shift
end
#Execute the Command
echo "DEBUG: $COMMAND"
$COMMAND
{ Invocation }
Code:
ssh myserv4 -n "/path/to/my/project/scripts/custom.shell --envfile /path/to/my/envfile/env.file -c sh -c 'set -a;DISPLAY=localhost:10.0;RELEASE=;/path/to/my/executable/bin/start.sh 0 1 222 -cpu 3'" &
{ Result }
My ssh session seems to be operating correctly. My custom.shell script seems to be getting called correctly. The custom.shell script seems to be producing the command that I want correctly with the exception that the single quotes are getting stripped out.
{ Output }
Code:
DEBUG: sh -c set -a;DISPLAY=localhost:10.0;RELEASE=;/path/to/my/executable/bin/start.sh 0 1 222 -cpu 3
{ Questions }
- How do I get my single quotes so that the sh command can interpret the whole -c command?
- Is there a better way to accomplish this? I feel like I'm riding a Go-Kart on a cross country trip. There has got to be a better way!