Do loop doesn't iterate


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Do loop doesn't iterate
# 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 06: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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using nested for loop to iterate over file names

I'm trying to grab a list of file names from a directory, then process those files 5 at a time. In the link below. Instead of using files I'm using the files array which contains 15 strings starting with AAA. So I'm trying to assign $fileset 5 of the strings at a time to pass to a command. So... (4 Replies)
Discussion started by: zBernie
4 Replies

2. Shell Programming and Scripting

Iterate array using loop over ssh

A simple script: #!/bin/bash test=test test1=(test1 test2 test3) echo ${test1 } ssh server 'echo '$test'; echo '${test1 }' ; echo '${test1}' ; for m in $(seq 1 $(echo '${test1 }' | tr " " "\n" | wc -l)); do echo $m ; echo '${test1}'; done'Here is the result: test1 test2 test3 testing... (5 Replies)
Discussion started by: mharald
5 Replies

3. UNIX for Dummies Questions & Answers

Iterate/Loop Through XML Node List

I need to load an XML file and loop through a list of nodes in it to execute a shell script for each one using the attributes for each node as parameters for the script. Any ideas? Any help will be much appreciated. (1 Reply)
Discussion started by: bradlecat
1 Replies

4. Shell Programming and Scripting

How to loop(Iterate) through List with foreach(csh)

Hey all,, I know cshell is harmful:) but I am using this just "to know" - for educational purposes!... not for a long-term use. lets say i have a list.. set arr=(x y z e f) I wanna iterate the list with foreach ,, not with while.!! foreach i $arr echo $i end does not work (2 Replies)
Discussion started by: eawedat
2 Replies

5. Shell Programming and Scripting

Nested While loop doesn't end

Hi, Below is my script in which i am using nested while loop to read two files and move the files to a remote server. My issue is that the 2nd while loop doesn't stop executing and it keeps on executing. Can someone please let me know where i have gone wrong. myFile=$ESER_TEST_FILES ... (2 Replies)
Discussion started by: funonnet
2 Replies

6. Shell Programming and Scripting

Script doesn't work in loop but does if not

I have a script that only works if I remove it from the looping scenario. #!/bin/bash # Set the field seperator to a newline ##IFS=" ##" # Loop through the file ##for line in `cat nlist.txt`;do # put the line into a variable. ##dbuser=$line echo "copying plugin..." ... (6 Replies)
Discussion started by: bugeye
6 Replies

7. Shell Programming and Scripting

Why doesn't this loop end?

Simple script, takes an cmd line argument and counts down to 1. NUMBER=$1 # One argument must be provided, otherwise don't execute if then echo "Error. Enter one argument " exit 0 elif then echo " " fi # Integer value must be greater than zero while do echo... (6 Replies)
Discussion started by: Breakology
6 Replies

8. Shell Programming and Scripting

for loop doesn't work

I have a script which uses below for loop: for (( i = 0 ; i <= 5; i++ )) do echo "Welcome $i times" done But when I run the script, it gives error message: Syntex Error : Bad for loop variable Can anyone guide to run it? Thanks in advance. (10 Replies)
Discussion started by: naw_deepak
10 Replies

9. UNIX for Dummies Questions & Answers

a for loop that doesn't make sense

I've been referring bash info for processes and came across a structure for a process which is defined like typedef struct process { struct process *next; char ** argv . . . }process; What I don't understand is that in the program there's a for loop which goes like this job... (2 Replies)
Discussion started by: sdsd
2 Replies

10. UNIX for Dummies Questions & Answers

Why script For...Loop doesn't work. Seek help

I have written a script to run on UNIX server. When I tested, it always hanged on after "date +"%D %T: XXXXXX script started." part. Then it wouldn't go further. UNIX server gave me one error message. I used the same code in another script. It works fine. I think the major problem may be in... (3 Replies)
Discussion started by: duke0001
3 Replies
Login or Register to Ask a Question