Reputation: 1486
I can run a local bash script on a remote server like so...
ssh user@server "/bin/bash -s" < script.sh
But I need to take a 'config' file on my local server, containing a list of variables and their values (.env file perhaps?), and substitute those in the script so that they are present when it is executed on the server. So say my script contains:
#!/bin/bash
echo "image directory: ${PWD}/${IMAGE_DIR}"; # use pwd of server but image_dir specified on local machine
echo "model name: ${MODEL_NAME}";
echo "model dimensions: ${IMAGE_DIMENSIONS}";
# ...do some work...
echo "goodbye from $HOSTNAME";
And my 'config' file contains:
IMAGE_DIR=/path/to/images
MODEL_NAME=outputModel.h5
IMAGE_DIMENSIONS=(256 256 3)
Are there simple linux commands that will inject those variable values into my script before it is executed on the remote server? The envsubst program looks like it could help, but I don't know how to make envsubst 'see' the variables from my 'config' file?
I want to use simple Linux commands, where possible, as I eventually want to create a Docker image which can run these commands in a cron job. So for each 'config' file, I inject the config variables into the script and call it on a remote server.
Upvotes: 0
Views: 89
Reputation: 392
awk '
NR==FNR{
pos = index($0, "=");
val = substr($0, pos+1);
arr[$1]=val;
next;
}
{
for(key in arr){
gsub("\\${?"key"}?", arr[key], $0);
}
print
}
' FS='=' config FS='[ \t\n]+' script.sh
I wrote small awk script. I didn't test it but it should work for you.
Upvotes: 2