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

👤 Login to reply

    #1  
Old 06-11-2013
asandy1234 asandy1234 is offline
Registered User
 
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 05:42 PM..
Sponsored Links
    #2  
Old 06-11-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
I don't see that it would execute even once, your loop never does anything but declare variables.
Sponsored Links
    #3  
Old 06-11-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
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 06-11-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
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 06-11-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
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 06-12-2013
asandy1234 asandy1234 is offline
Registered User
 
I tried all the option specified here but nothing works still the loop only iterates one time.
Sponsored Links
    #7  
Old 06-12-2013
Scott Scott is online now Forum Staff  
Administrator
 
You set a variable called cmd. Then what? Where do you "run" it? And what is $Program?
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 03:17 AM
How to loop(Iterate) through List with foreach(csh) eawedat Shell Programming and Scripting 2 08-25-2011 06:12 PM
Nested While loop doesn't end funonnet Shell Programming and Scripting 2 07-05-2011 07:40 AM
Why doesn't this loop end? Breakology Shell Programming and Scripting 6 05-17-2009 07:34 PM
for loop doesn't work naw_deepak Shell Programming and Scripting 10 12-30-2008 01:52 AM



All times are GMT -4. The time now is 01:44 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?