Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Do loop doesn't iterate

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-11-2013   -   Original Discussion by asandy1234
asandy1234's Unix or Linux Image
asandy1234 asandy1234 is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 11 June 2014, 4:24 PM EDT
Posts: 37
Thanks: 4
Thanked 1 Time in 1 Post
Do loop doesn't iterate

I'm trying to send the file list as parameter to another job and execute it.
But the loop doesn't work, the inner job is running only once and not twice as expected


Code:
for filelist in $(ls -rt *.txt | tail -2)
do
echo $filelist
export filelist
cmd="$Program -config $configfile -autoexec $autoexecfile -log $LogFile -sysin $SourceDir/Load.sas"

done


Last edited by Corona688; 06-11-2013 at 06:42 PM..
Sponsored Links
    #2  
Old Unix and Linux 06-11-2013   -   Original Discussion by asandy1234
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 14 December 2017, 4:58 PM EST
Location: Saskatchewan
Posts: 22,547
Thanks: 1,159
Thanked 4,287 Times in 3,955 Posts
I don't see that it would execute even once, your loop never does anything but declare variables.
Sponsored Links
    #3  
Old Unix and Linux 06-11-2013   -   Original Discussion by asandy1234
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 14 December 2017, 5:06 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,647
Thanks: 109
Thanked 1,613 Times in 1,184 Posts
Quote:
Originally Posted by Corona688 View Post
I don't see that it would execute even once, your loop never does anything but declare variables.
At least the "echo" statement should be executed, no?

At first, make shure there are indeed at least 2 files fitting the globbing expression in the PWD. (In case you have overseen it: the "ls" uses no path, so it works in the current directory.)

Second: you should use "tail -n 2" instead of "tail -2".

I hope this helps.

bakunin
    #4  
Old Unix and Linux 06-11-2013   -   Original Discussion by asandy1234
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 17 December 2017, 12:04 AM EST
Location: Amsterdam
Posts: 11,660
Thanks: 521
Thanked 3,386 Times in 2,985 Posts
Also see if this makes a difference:

Code:
ls -rt *.txt | tail -n 2 |
while read filelist
do
  printf "%s\n" "$filelist"
done

Sponsored Links
    #5  
Old Unix and Linux 06-11-2013   -   Original Discussion by asandy1234
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 14 December 2017, 5:06 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,647
Thanks: 109
Thanked 1,613 Times in 1,184 Posts
Quote:
Originally Posted by Scrutinizer View Post
Also see if this makes a difference:
Good catch: if some filenames contain control characters the original construct with the subshell "$(...)" might result in somewhat obfuscated output because this is interpreted by the shell a second time.

It will be a good idea to test which output ls -rt *txt | tail -n 2 really produces.

I hope this helps.

bakunin
Sponsored Links
    #6  
Old Unix and Linux 06-12-2013   -   Original Discussion by asandy1234
asandy1234's Unix or Linux Image
asandy1234 asandy1234 is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 11 June 2014, 4:24 PM EDT
Posts: 37
Thanks: 4
Thanked 1 Time in 1 Post
I tried all the option specified here but nothing works still the loop only iterates one time.
Sponsored Links
    #7  
Old Unix and Linux 06-12-2013   -   Original Discussion by asandy1234
Scott's Unix or Linux Image
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 17 December 2017, 6:35 AM EST
Posts: 8,946
Thanks: 380
Thanked 1,238 Times in 1,052 Posts
You set a variable called cmd. Then what? Where do you "run" it? And what is $Program?
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Iterate/Loop Through XML Node List bradlecat UNIX for Dummies Questions & Answers 1 06-05-2012 04:17 AM
How to loop(Iterate) through List with foreach(csh) eawedat Shell Programming and Scripting 2 08-25-2011 07:12 PM
Nested While loop doesn't end funonnet Shell Programming and Scripting 2 07-05-2011 08:40 AM
Why doesn't this loop end? Breakology Shell Programming and Scripting 6 05-17-2009 08:34 PM
for loop doesn't work naw_deepak Shell Programming and Scripting 10 12-30-2008 02:52 AM



All times are GMT -4. The time now is 07:57 AM.