0

I have a shell script that extracts certain values from a text file (input to it via the terminal). The script does the extraction as intended except, it doesn't print the output to the file correctly.

The script is:

#!/bin/bash

input_file=$1 
while read -r LINE
do
IFS="=" read -r -a params <<< "$LINE"
if [ -n "${params[2]}" ]
    then
    IFS=" " read -r -a param_opcode <<< "${params[2]}"
    echo "${param_opcode}"
fi
done < "$input_file"

The output on the terminal is as follows:

0xd2800140  
0xd2800061  
0x8b010000  
0x8b000042  
0xd1000821  
0xd28001e5  
0xd28000a6  
0x9ac608a5  
0xe7ff0010  
0xe7ff0010  

However, when I try to write this to a text file bu doing:

echo "${param_opcodes}" > log.txt

It prints only this to the file:

0xe7ff0010

I tried >> but I don't want to append to it. I want the file to be overwritten every time, I run the script.

Srini V
  • 11,045
  • 14
  • 66
  • 89
the_mamba
  • 119
  • 1
  • 2
  • 10

1 Answers1

3

Your redirection to log file isn't right because it's inside the while loop. Use the redirection at the end of the while loop:

while read -r LINE; do
...
done < "$input_file" > "log.txt"
oliv
  • 12,690
  • 25
  • 45