1

I'm trying to do a while loop into bash script but I don't know what's going on with the ed (text editor) when I tried to insert a varible in its arguments. As you can see, I'm using a loop as well, but the problem seem to be in the use of varibles inside ed command.

Here is the script code (obvious the awk and sed programs):

#!/bin/bash

x=1
y=5

SCRIPTDIR=~/bin

awk 'NR % 2==0' test > output1
awk -v n=5 '1; NR % n == 0 {print ""}' output1 > output1b
sed -i 's/\r//' output1b

while [ $x -le 50 ]; do
    ed -s output1b <<< $"$xm$y\nw"
let x=x+5
let y=y+5
done
git
  • 151
  • 9

1 Answers1

4
ed -s output1b <<< $"$xm$y\nw"

should be

ed -s output1b <<ED_COMMANDS
${x}m$y
w
ED_COMMANDS

or

printf "%s\n" "${x}m$y" "w" | ed -s output1b

The $"..." form won't translate \n to a newline, and the $'...' form won't expand variables.

Also, note the braces in ${x}m$y otherwise the shell is looking for variables $xm and $y

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thank you very much Glenn, the second option was good for me. I should study the bash manual (quotes, especial characters....) but I'm starting. – git Sep 03 '15 at 06:47
  • It's good to read the section on [quoting](http://www.gnu.org/software/bash/manual/bashref.html#Quoting) and [redirections](http://www.gnu.org/software/bash/manual/bashref.html#Redirections) (applicable here), and familiarize yourself with the order of [shell expansions](http://www.gnu.org/software/bash/manual/bashref.html#Shell-Expansions) – glenn jackman Sep 03 '15 at 13:23
  • @glennjackman I've been looking this for more than 5 hours, thank you so much! – Paulo Pedroso Feb 19 '16 at 15:55