0
    rm -rf xyz.log
    echo "Enter n"
    read n
    x[0]=ABC
    x[1]=DEF
    x[2]=GHI
    y[0]=MNO
    y[1]=PQR
    y[2]=STQ
    z[0]=RTY
    z[1]=LKJ
    z[2]=LDF
    for (( i=0; i<n; ++i ))
    do
      echo "${x[i]} ---- ${y[i]} ---- ${z[i]}" >> xyz.log
    done
    o=`cat xyz.log`
    echo $o

When I execute the above script, I get all output in one line, but in xyz.log, it's printed line by line. I want the same thing to be printed in echo command. Thanks in advance.

$ ./val.sh

Enter n

3

ABC ---- MNO ---- RTY DEF ---- PQR ---- LKJ GHI ---- STQ ---- LDF

$ cat xyz.log

ABC ---- MNO ---- RTY

DEF ---- PQR ---- LKJ

GHI ---- STQ ---- LDF

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
Jayavinoth
  • 544
  • 1
  • 9
  • 24
  • Possible duplicate of http://stackoverflow.com/questions/1483721/shell-script-printing-contents-of-variable-containing-output-of-a-command-remove?rq=1 – Henk Langeveld Jan 20 '13 at 14:17

3 Answers3

4

You're missing quotes in the last line:

o=`cat xyz.log`
echo "$o"
# ---^--^

Adding quotes will allow the shell to use the contents of the variable exactly. Otherwise, you're asking the shell to perform wordsplitting, where any sequence of whitespace (including newlines) will be replaced by a single space (by default).

rule of thumb: Always quote "$variables" except when you specifically want to not quote them.

Also, this is wrong

`rm -rf xyz.log`

You're performing the deletion in a subshell, and then using the output as a command. In this case, there is no output, but you can see the effect like this

`echo hi there` # -- command not found 'hi'
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • +1 as this is THE cause of your specific problem. Also, get rid of the ticks around the rm command line and consider using `$(command)` instead of backtick-notation. – Ed Morton Jan 20 '13 at 13:57
0

The -e option is used to enable echo's interpretation of additional instances of the newline character as well as the interpretation of other special characters, such as a horizontal tab, which is represented by \t. Thus, for example, the following would produce a formatted output:

echo -e "\n Projects: \n\n\tplan \n\tcode \n\ttest\n"

So use Your echo command like:

echo -e "${x[i]} ---- ${y[i]} ---- ${z[i]} \n" >> xyz.log

It will do the trick and will give you the answer you are looking for.

  • `for (( i=0; i> xyz.log done o=`cat xyz.log` echo $o` I changed it but still i am getting the same output. **$ ./val.sh** Enter n 3 ABC ---- MNO ---- RTY DEF ---- PQR ---- LKJ GHI ---- STQ ---- LDF – Jayavinoth Jan 20 '13 at 06:02
  • In the xyz.log, there is blank line in inbetween the output, but i want it to be printed in the output at the end of program. – Jayavinoth Jan 20 '13 at 06:06
0

Use the -n option on echo. -n means "do not write a newline at the end of the line as usual." It's documented in the bash man page.

I also suggest just putting echo >> xyz.log just to put a newline at the end of the file.

OUTFILE="xyz.log"

`rm -rf "$OUTFILE"`
echo "Enter n"
read n
x[0]=ABC
x[1]=DEF
x[2]=GHI
y[0]=MNO
y[1]=PQR
y[2]=STQ
z[0]=RTY
z[1]=LKJ
z[2]=LDF
for (( i=0; i<n; ++i )) ; do
    echo -n "${x[i]} ---- ${y[i]} ---- ${z[i]}" >> "$OUTFILE"
done
o=`cat "$OUTFILE"`
echo $o
steveha
  • 74,789
  • 21
  • 92
  • 117
  • Huh, I'm not sure how that happened. I'll edit the answer with the *changed* code. Sorry about the mixup. – steveha Jan 20 '13 at 07:22