Problem with executing script using ssh

I'm writing a script which is meant to ssh into a remote machine, sed a file there, and write the output to a new file on the remote machine. Obviously the easy way to do this is to write and execute the script on the remote machine, but I'm trying to do this all via ssh since I like to keep and execute my scripts from a central location.

So here's my extremely ugly attempt so far:

#Do the following on a remote server:
#ssh in, then operates on some files in the remotedir directory
#sed from an "mg" file (such as host1.mg), replacing all instances of
#host1 and replacing it with a new string from a after.conf
#and then writing the output to the remotedir directory.
#Assume that the after.conf only has one value right now (host2)

remotebox="remote1"
adminssh="sudo -u admin ssh $remotebox"
before="host1"
remotedir="/dir/remotedir"

for after in `cat after.conf`
do
echo $target
$adminssh sed -e 's/$source/$target/g' $remotedir/$source.mg > $target.mg

done

========================================
This works up to a point for me. But as it stands now, it bombs out with the following error:

sed: -e expression #1, char 0: no previous regular expression

I'm assuming this is because I am mis-using sed somehow. Even if I replace the sed command with something like this:

$adminssh "cat $remotedir/$source.mg > $remotedir/$target.mg"

the script bombs out - it seems like the redirect is trying to force the output onto my local machine I think. I'm sure I'm doing several things wrong here. Can anyone help?

Nobody knows? :frowning:

Is this the completely wrong way to go about doing this?

Using variables in sed scripts is tricky, and it looks like you're caught on the first step.

Ever program in C? When this command runs:

sed -e 's/$source/$target/g' $remotedir/$source.mg

think about the values of argv.

What I imagine you WANT is for argv[2] to be something like:

s/\/old\/directory\/oldfile/\/new\/directory\/newfile/

What you HAVE is:

s/$source/$target/g

Because you're using single quotes, not double quotes, and sh isn't expanding those variables.

So the first step is to switch those single quotes to double quotes,

"s/$foo/$bar/g"

The next step is to make sure $foo and $bar don't contain any stray '/' characters that are going to confuse sed.

Best of luck!

The problem here is: 's/$source/$target/g'

---

for after in `cat after.conf`
do
echo $target
$adminssh sed -e 's/$source/$target/g' $remotedir/$source.mg > $target.mg

done

Also note that when you use commands with ssh, your command gets launched instead of a login shell.

So redirects like > don't work: those are part of the shell.

IIRC, you can do something like,

ssh remotehost '/bin/bash -l -c '\''echo hi > $HOME/foo'\'''

What command gets run on the remote host? It's

/bin/bash -l -c 'echo hi > $HOME/foo'

which causes bash to log in and run:

echo hi > /home/dir/foo

(the quoting around HOME is awkward here)

Extremely helpful - thank you so much. I'll give this a shot.

How does this board work anyway? Any way to thank people like blesses or points? Or do people help others out of the goodness of their own hearts? :slight_smile:

This is my first time using this board.