Passing link parameters via sed - bash

I'm trying to insert an image link into a file for a wiki on a remote server.
ssh root#10.10.10.1 "sed -i -e '1i'[^http://10.10.10.2/image.jpg^]'\'" /var/www/wiki/page
Works but I need to add the resize parameter after the file name but it doesn't work, how do I account for the spaces?
ssh root#10.10.10.1 "sed -i -e '1i'[^http://10.10.10.2/image.jpg height480 width=640^]'\'" /var/www/wiki/page
sed: can't read height480: No such file or directory
sed: can't read width=640^]\: No such file or directory

I'm not sure why you're quoting only the '1i'. You should quote the entire sed expression, if you have spaces in it. Try this:
ssh root#10.10.10.1 "sed -i -e '1i[^http://10.10.10.2/image.jpg height480 width=640^]'" /var/www/wiki/page

Better use heredoc here to avoid crazy escaping and other issues like spaces in command:
ssh -t -t root#10.10.10.1 <<'EOF'
sed -i '1i[^http://10.10.10.2/image.jpg height480 width=640^]' /var/www/wiki/page
exit
EOF

Related

bash script using sed command on remote server with ssh and special characters

I am trying to run this sed command on a remote server through a bash script but I can not get the escaping correct.
ssh -t user#ip "sed -i -e '/<!--insert_new_code-->/i\ <div class="col-lg-4 col-md-6">\n <?php include "''explorers/'"${coin_symbol_lower}"'.php''";?>\n </div>\n\n' /home/some-data/some_dir/dir/dir/index.php"
I have the spaces in there to keep the html code looking clean. The sed command works fine when ran locally, just not over ssh.
Any help getting it working would be appreciated
The command passed to ssh is delimited by double quotes. Since there are also double quotes within the string being passed to sed, they will need to be escaped to prevent the shell from trying to take them:
ssh -t user#ip "sed -i -e '/<!--insert_new_code-->/i\ <div class=\"col-lg-4 col-md-6\">\n <?php include \"''explorers/'\"${coin_symbol_lower}\"'.php''\";?>\n </div>\n\n' /home/some-data/some_dir/dir/dir/index.php"
Try this syntax
ssh -t user#ip << \EOF
sed -i -e '/<!--insert_new_code-->/i\ <div class="col-lg-4 col-md-6">\n <?php include "''explorers/'"${coin_symbol_lower}"'.php''";?>\n </div>\n\n' /home/some-data/some_dir/dir/dir/index.php
EOF

Bash script placing quotes around command

I have a Bash script that will get an IP to use as part of an SSH tunnel, but running this script the SSH tunnel fails. When using set -x I can see it places the arguments to the SSH command in single quotes and manually running this line results in the same error.
The Script:
ssh -N -L 9000:${ip_array[$2]}:443 ssh-server
The first argument is used elsewhere in the script for something else which is why the second is used here. ssh-server is an alias in my SSH config to the server i am tunneling through.
The output I get is:
ssh -N -L '9000:"172.0.0.1":443' ssh-server
Could this be because the script to fetch the IP returns strings to the array?
you can try removing the double-quotes first :
ip=$(echo "${ip_array[$2]}" | sed "s/\"//g")
ssh -N -L 9000:${ip}:443 ssh-server
Or just use shell parameter expansion to remove the quotes:
ssh -N -L 9000:${ip_array[$2]//"/}:443 ssh-server
That lone double quote may mess up your editor's syntax highlighting.
Get rid of the quotes by piping it through the tr command:
ssh -N -L 9000:$( echo ${ip_array[$2]} | tr -d '"' ):443 ssh-server

Using SED in a ssh command on a remote node

I wrote a script to ssh to some nodes and run a sed command inside the node. The script looks like
NODES="compute-0-3"
for i in $NODES
do
echo $i
ssh $i 'sed -i \'s/172.16.48.70/172.20.54.10/g\' /etc/hosts;'
done
However, the error is
unexpected EOF while looking for matching `''
syntax error: unexpected end of file
It seems that the character ' is not treated as the begining of a sed command.
I suggest to replace
ssh $i 'sed -i \'s/172.16.48.70/172.20.54.10/g\' /etc/hosts;'
by
ssh "$i" 'sed -i "s/172.16.48.70/172.20.54.10/g" /etc/hosts'
If you absolutely want to use single quotes:
ssh "$i" 'sed -i '"'"'s/172.16.48.70/172.20.54.10/g'"'"' /etc/hosts'

Deleting word from line on remote server

I am trying to delete a word from bash profile on remote server but the command never come out.
ssh -X test_server 'sed -e \'s/unalias ls//g\' -i .bash_profile' </dev/null
Not sure what i am doing wrong, Kindly assist.
It's not possible to nest single quotes in shell, switching to double quotes should give you the desired result. E.g.:
$ ssh somehost sh <<< 'echo "Xunalias lsX" | sed -e "s/unalias ls//"'
XX

Replace script works if I type manually but not in script

I have a bash script, replace.sh with the following contents:
ack-grep -a -l -i --print0 --text "$1" | xargs -0 -n 1 sed -i -e 's/$1/$2/g'
When I try and run it as, eg:
replace.sh something somethingnew
The prompt returns without errors but no changes have been made to any files.
If I manually type:
ack-grep -a -l -i --print0 --text "something" | xargs -0 -n 1 sed -i -e 's/something/somethingelse/g'
The files get changed as expected.
Ths $1 syntax seems to work for other scripts I've written. I'm guessing I'm missing something to do with escaping the args or something?
Thanks!
Ludo.
Variable substitutions aren't done in single quotes, try:
ack-grep -a -l -i --print0 --text "$1" | xargs -0 -n 1 sed -i -e "s/$1/$2/g"
See the bash man page section on QUOTING.
Use "" instead of '' in the sed expression. It will not prevent the variablename-resolving. What you are actually doing now is replacing $1 to $2. You can test in console (without writing a script) like this:
$ a=something
$ b=somethingelse
$ sed 's/$a/$b/g' testfile
$ sed "s/$a/$b/g" testfile
This isn't related to your question, but some help on using ack.
The -a and --text conflict with each other. -a will give you a superset of --text. Use one or the other.
Also, it looks like you might as well use grep -Z instead of ack since you're not using any of ack's functionality that is a superset of grep.
In general, if you're using ack in a pipeline, you should probably be using good ol' grep instead.

Resources