Output in for loop (ksh)

Hi ,
I'm writing the for loop script in home directory and wanted to get the files from /etc/data directory.

#!/bin/ksh

file_nm="/etc/dat"
for test_data in $file_nm/fln*
do
 echo "$test_data"
done

the code is executing successfully , but in the output it is showing

/etc/dat/fln_data_day1
/etc/dat/fln_data_day2

Where i wanted the output to be printed as

fln_data_day1
fln_data_day1

How can i achieve that.
Thank you

just change below

 
echo $test_date to echo "`basename $test_date`"
1 Like

Thank You so much for quick reply.
And it worked . :slight_smile:

---------- Post updated at 05:13 PM ---------- Previous update was at 04:08 PM ----------

Hi
I had a small issue while assignig echo output to a variable
in the following example

 #!/bin/ksh
 
file_nm="/etc/dat"
for test_data in $file_nm/fln*
do
 txt = "`basename $test_date`"
echo "$txt"
 
done

The above script throws error
./test.sh[8]: txt: not found

Where i'm doing wrong in this script !

Hello,

Could you please place

 `basename $test_date`  

and try. We should be good then.

Thanks,
R. Singh

1 Like

Thank You Ravinder,
after changing the code like below

#!/bin/ksh
 
file_nm="/etc/dat"
for test_data in $file_nm/fln*
do
 txt = `basename $test_data`
echo "$txt"
 
done

txt: not found

Thanks

---------- Post updated at 05:53 PM ---------- Previous update was at 05:31 PM ----------

I got the output..

Thank You

As far as I know basename is not a shell built-in. Use parameter substitution instead.

Also there shouldn't be any blank space around assignment operator:

#!/bin/ksh

file_nm="/etc/dat"
for file in ${file_nm}/fln*
do
        fname="${file##*/}"
        print "$fname"
done

I think your error is also because you have spaces around the = in the code you have posted. The assignment statement in ksh is var=expression, no spaces. The expression could be the the output of a command, a numeric value or a string.

Robin
Liverpool/Blackburn
UK