I can't get this script to do what I want. Can someone help it's supposed to read a text file line by line and then echo the result into another file. But it's not doing recognizing the blank line. As it supposed to print the line unless it's blank then it's suppose to print to the file.
iname = checktestwhile read line do if [ "$line" == "" ];thenecho "<blank>" >> $iname2.txtelse echo "$line" >> $iname2.txtfidone <$iname.txt
Best Answer
You cannot have spaces between variable assignment in bash.
#!/bin/bashiname=checktest #Should be no space between = signwhile read line; do if [ -z "$line" ]; thenecho "I saw an empty line ... will report this"else echo "$line" fidone <"$iname".txt > newfile.txt
Also, use >
after the end of loop. This prevents unnecessary I/O to open the file for writing during each loop cycle.
make some edits like this :
iname = checktest
while read linedo if [ -z "$line" ]then echo "" >> ${iname}2.txtelseecho "$line" >> ${iname}2.txtfidone < ${iname} ;
it should work now , hope this helps ...
iname = checktestwhile read line do if [ "$line" != "" ];thenecho $line >> fidone < $iname > ${iname}2.txt
It seems more logic to me to do something when it match only.