Problem with Ksh scripting

Hi,

Below is the code for my script

#!/bin/ksh
 
for file in "file1.txt" "file2.txt" "file3.txt" "file4.txt"
do
 L="$(tail -1 $file)"
 NUM=${L%%|*}
 DAT=${L##*|}
 echo $NUM
 echo $DAT
done

Now here,suppose for file1.txt
L will have data like 56|06-07-2010
So, it should be
NUM as 56 & DAT as 06-07-2010

but through the script NUM is getting NULL value and DAT is having 56|06-07-2010 although when am running it without script all works fine...Please tel wher am i wrong..... :confused:

What is your login shell? Post output of ps command.

What happens if you escape the | like so:

 NUM=${L%%\|*}
 DAT=${L##*\|}
1 Like
 ls file[1-4].txt |xargs -i awk -F \| '{a=$1;b=$2} END {print a,b}' {}

Thanks all.....!!!:slight_smile:

My login shell is ksh.....!!!
scrutinizer's "\" is working fine for me......but why is it required in scripting but not in normal input???

Danke!!!
C u all...!
tc.

I don't know, perhaps you are not using the exact same ksh as you are using in your script?

In this case whether | should be escaped in variable expansion is at least a grey area and probably is open to interpretation. In this case I would just escape it - even if it is not required - just to avoid problems. This should work in any Posix shell.

hmmm....!!!

Thanks :slight_smile:

i jus give ksh to change my shell.....anyways now its working fine...may be that escape thing was the problem.

NB: Please if can any one let me know how to close threads so that this will signify that my problem is solved..!

Thanks for keeping us informed, and pleased to hear "SOLVED"...

On your request I will close the thread