Output in for loop (ksh)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output in for loop (ksh)
# 1  
Old 06-26-2013
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.
Code:
#!/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
Code:
/etc/dat/fln_data_day1
/etc/dat/fln_data_day2

Where i wanted the output to be printed as
Code:
fln_data_day1
fln_data_day1

How can i achieve that.
Thank you
# 2  
Old 06-26-2013
just change below

Code:
 
echo $test_date to echo "`basename $test_date`"

This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 06-26-2013
Thank You so much for quick reply.
And it worked . Smilie

---------- 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
Code:
 #!/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 !

Last edited by smile689; 06-26-2013 at 08:48 AM.. Reason: appended code
# 4  
Old 06-26-2013
Quote:

#!/bin/ksh

file_nm="/etc/dat"
for test_data in $file_nm/fln*
do
txt = `basename $test_date`"
echo "$txt"

done
Hello,

Could you please place
Code:
 `basename $test_date`

and try. We should be good then.


Thanks,
R. Singh

Last edited by RavinderSingh13; 06-26-2013 at 08:56 AM.. Reason: To remove " from the code.
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 06-26-2013
Thank You Ravinder,
after changing the code like below
Code:
#!/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

Last edited by smile689; 06-26-2013 at 09:20 AM.. Reason: Updated with error
# 6  
Old 06-26-2013
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:
Code:
#!/bin/ksh

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

# 7  
Old 06-26-2013
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple if within while loop ksh

Hi All, I'm trying to write while loop with multiple if conditions. Each and every if condition with different variables. whenever one if condition fails i have remove the file from filename and have to pick another file and loop should exit until the last file found in filename. Please help... (4 Replies)
Discussion started by: Kayal
4 Replies

2. Shell Programming and Scripting

Help with loop in ksh script

Hi, I am new to UNIX. I am working on a script where it takes the input and produces a desired output and it works fine for one instance. Input(One Instance): CREATE TABLE TAB1 ( COL1, COL2 ); CREATE UNIQUE INDEX XPKTAB1 ( COL1 )TAB1; Output: CREATE TABLE TAB1 ( COL1, COL2... (8 Replies)
Discussion started by: varun2327
8 Replies

3. Shell Programming and Scripting

ksh for loop

Any reason why this thing doesn't works in Korn Shell for (( expr1; expr2; expr3 )) do ..... ... repeat all statements between do and done until expr2 is TRUE Done Rgds, TS (4 Replies)
Discussion started by: targetshell
4 Replies

4. Shell Programming and Scripting

ksh loop

I need an echo "hit enter" running every 3 seconds till user hit enter key. 10x (1 Reply)
Discussion started by: LiorAmitai
1 Replies

5. Shell Programming and Scripting

ksh - for loop with variables

Hi, I 'm trying to send an e-mail for every different line in the .txt for i in {1..$variable} do sed -n "/$i$/p" text.txt done I have two problems about this. First one is that for loop doesn't work and the second one is that i cant get the output of sed (4 Replies)
Discussion started by: ozum
4 Replies

6. Shell Programming and Scripting

Why does my for loop does not work right in ksh?

hi all, i have a for loop statement in my ksh code. it only returns the first value retrieved not the value for the other rows. ex: acct_id value = returned value in the for loop 1 = 1 2 = 1 (instead of 2) 3 = ... (1 Reply)
Discussion started by: ryukishin_17
1 Replies

7. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

8. Shell Programming and Scripting

Help with While loop in KSH

Hi, I want to write a while loop like this can any one say me whats wrong with my loop USAGE="Usage: Mail.ksh" integer i=3 while ((1<i<=3)) do . . . . (( CMD_JUL = LSD_JUL - i )) CUR_MAINT_DATE=$(julian2date ${CMD_JUL}) . . . i=i-1 done (1 Reply)
Discussion started by: bhagya2340
1 Replies

9. Shell Programming and Scripting

For loop in ksh..Please help..

Hi ALL, I need to take some command line arguments for my script and then want to run a function for each argument.I thought of using for loop as below, but its not working , can some one please help... #!/bin/ksh lpar1=$1 lpar2=$2 lpar3=$3 lpar4=$4 lpar5=$5 echo "$lpar1" >>lpar.txt echo... (4 Replies)
Discussion started by: prashant43
4 Replies

10. Shell Programming and Scripting

ksh while loop

hi all, got this silly problem and i just can't seem to make sense of the error message its is saying 1400: cannot open. its my first time at writing a while loop but tried all sorts to get it working without success. #!usr/bin/ksh integer max=1400 set file="afilename" integer i=1 ... (3 Replies)
Discussion started by: scriptingmani
3 Replies
Login or Register to Ask a Question