Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 06-11-2013
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..
# 2  
Old 06-11-2013
I don't see that it would execute even once, your loop never does anything but declare variables.
# 3  
Old 06-11-2013
Quote:
Originally Posted by Corona688
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
Also see if this makes a difference:
Code:
ls -rt *.txt | tail -n 2 |
while read filelist
do
  printf "%s\n" "$filelist"
done

# 5  
Old 06-11-2013
Quote:
Originally Posted by Scrutinizer
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
# 6  
Old 06-12-2013
I tried all the option specified here but nothing works still the loop only iterates one time.
# 7  
Old 06-12-2013
You set a variable called cmd. Then what? Where do you "run" it? And what is $Program?
Login to Reply

« Previous Thread | Next Thread »
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
Using nested for loop to iterate over file names zBernie Shell Programming and Scripting 4 01-22-2015 04:59 AM
Iterate array using loop over ssh mharald Shell Programming and Scripting 5 07-15-2013 12:24 PM
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
Script doesn't work in loop but does if not bugeye Shell Programming and Scripting 6 08-03-2010 02:10 AM
tail -XXX with grep doesn't work in while loop sidobre Shell Programming and Scripting 8 04-07-2010 04:25 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
a for loop that doesn't make sense sdsd UNIX for Dummies Questions & Answers 2 09-27-2008 08:17 AM


All times are GMT -4. The time now is 06:14 AM.

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