Echo is replacing characters

Hi All,
I'm using KSH and am writing a small script that counts the lines of various files in several folders under one root folder.

Below is the script:

 
#!/usr/bin/ksh
common_path_in="/interface_in/rsc"
file_out="/interface_in/rsc/record_count.csv"
tot_rec_count=-1
act_rec_count=-1
echo ${common_path_in}
echo ${file_out}
echo ${tot_rec_count}
echo ${act_rec_count}
set -A folders horizon mysteryshopper rcc reference
echo ${folders[@]}
for i in ${folders[@]}
do echo ${common_path_in}
echo "${common_path_in}/$i"
echo `ls -1 ${common_path_in}/$i/*.csv`;done

Please find the output screenshot attached for your reference. Following are my questions regarding the output that I'm getting -

  1. The output of the line marked in red is the problem:
    common_path_in="/interface_in/rsc"
    $i in first iteration is - ${folders[0]}=horizon
    so I should get "/interface_in/rsc/horizon", but I'm getting "/horizonce/rsc/". horizon is coming at the beginning of the string.

  2. Also I've noticed that, if I give line breaks before the loop, then I'm getting some error - 'unexpected `do' ' or something like this. Even I'm getting this error if I place 'do' and 'done' in a separate lines alone.

Could anyone of you please look into it and let me know where I'm doing wrong?

Best Regards,
Jagari

You've probably edited the script with a Microsoft editor such as Notepad.

Please post the output from this "sed" command which is designed to make control characters visible. A normal unix text file line terminator will be displayed as a dollar sign. One which has been edited with a Microsoft editor will also have carriage-return characters displayed as "\r".

sed -n l scriptname

Where "scriptname" is the name of your script file.

1 Like

Hi Methyl,

Yes, I was editing the script in notepad/wordpad.
Please find attached the screenshot of 'sed' that you've suggested.

I'm pasting the output below:

 
#!/usr/bin/ksh\r$
common_path_in="/interface_in/rsc"\r$
file_out="/interface_in/rsc/record_count.csv"\r$
tot_rec_count=-1\r$
act_rec_count=-1\r$
echo $common_path_in\r$
echo $file_out\r$
echo $tot_rec_count\r$
echo $act_rec_count\r$
set -A folders horizon mysteryshopper rcc reference\r$
echo ${folders[@]}\r$
for i in ${folders[@]}\r$
do echo $common_path_in\r$
echo $i\r$
name=${common_path_in}/$i\r$
echo $name\r$
echo `ls -1 $common_path_in/$i/*.csv`;done$

Thanks,
Jagari

Should be:

for i in ${folders[@]}
do
    echo ${common_path_in}
    echo "${common_path_in}/$i"
    echo `ls -1 ${common_path_in}/$i/*.csv`
done

To fix your script we need to remove those carriage-returns:

cat oldscript | tr -d '\r' > newscript
# Then make new script executable
chmod 755 newscript

Obviously you sould be using a unix editor.

Hi Methyl,

Now I've edited the script in vi and have done this identation change that you've suggested.

I'm getting the below output and error message -

/interface_in/rsc
/interface_in/rsc/record_count.csv
-1
-1
horizon mysteryshopper rcc reference
test.ksh: line 10: syntax error at line 17: `for' unmatched

Intially I was following indentation and line breaks, but I was getting this same error. When I remove all extra spaces and indentation then this error was not there.

Please advise.

Regards,
Jagari

---------- Post updated at 11:58 AM ---------- Previous update was at 11:57 AM ----------

okay, let me try this... getting back to you after changing it...

---------- Post updated at 12:03 PM ---------- Previous update was at 11:58 AM ----------

great!!! It's working now :slight_smile:
thanks a lot Methyl.

Warm Regards,
Jagari

1 Like