Visit Our UNIX and Linux User Community


condition inside a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting condition inside a for loop
# 1  
Old 09-25-2008
condition inside a for loop

I have a for loop in my script as shown below.

for file_path in $file_list ; do

........my code
..........
......

done

Can i restrict the number of files parsing to the variable file_path as 50?
That is, even if I have some 100 files in file_list, I need to take only 50 files for the loop every time.

Is that possible? Can someone help me with this?

Thanks,
Vijay
# 2  
Old 09-25-2008
Code:
for file_path in `cat filelist.txt|head -50`
do
        echo $file_path
--
---
YOUR_CODE_HERE
----
done

# 3  
Old 09-25-2008
Code:
head -50 file | while read file_path
do
 .......
done

# 4  
Old 09-25-2008
But.. this doesn't solve my request.
Because.. i read it from a variable $file_list.
This contains the input as following:

echo $file_list
aaa.txt bbb.txt ccc.txt ddd.txt .......and so on.

So all file names will be in a single line. so head -50 will return all the files.. right?

any other way to get the first 50 names in the variable?
# 5  
Old 09-25-2008
Just maintain a counter in the loop, and exit when it reaches 50.

Code:
n=0
for file_path in $file_list; do
  : : : stuff
  n=`expr $n + 1`
  case $n in 50) break;; esac
done

# 6  
Old 09-25-2008
Code:
filelist="1 2 3 4 5 6 7 8 9 10"
for filepath in `echo $filelist|cut -d" " -f1-5`
do
        echo $filepath
done
 
OUTPUT
1
2
3
4
5

Instead of 5 use 50.
# 7  
Old 09-25-2008
Assuming that spacing in the variable is regular. If it might contain tabs or newlines, awk might be a better solution. Or maybe

Code:
perl -le '$,=" "; print @ARGV[0..49]' "$@"


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

2. Shell Programming and Scripting

matching patterns inside a condition in awk

I have the following in an awk script. I want to do them on condition that: fext == "xt" FNR == NR { />/ && idx = ++i $2 || val = $1 next } FNR in idx { v = val] } { !/>/ && srdist = abs($1 - v) } />/ || NF == 2 && srdist < dsrmx {... (1 Reply)
Discussion started by: kristinu
1 Replies

3. Shell Programming and Scripting

Check condition inside the loop

Hi, I am in trouble. I can get inside my condition test inside a loop : I am in ksh (solaris) while read file do <commande to retrieve file> >> ${LOG_RETRIEVE_FILE.log} msg_err=$(cat ${LOG_RETRIEVE_FILE.log} | grep "error retrieve") if ; then <sendmail> exit 1 fi done I tried... (6 Replies)
Discussion started by: Aswex
6 Replies

4. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

5. Shell Programming and Scripting

grep inside if condition - need help

hi i need help with below code. if ] then log "Exiting the script as ID= NULL" log "Please run script first." fi i am calling grep inside this but its not running any ideas why ?? input file is like this -- Msg 102, Level 20, State 1: Server... (4 Replies)
Discussion started by: dazdseg
4 Replies

6. Shell Programming and Scripting

FTP Command inside If condition

Hi All, I am facing a alien problem in unix shell script. i have a requirement where i have to check first whether a particular file has been created or not if that file has been created then that file needs to be ftped to a particular dir. My Code is like that Success='code for file... (3 Replies)
Discussion started by: aryan_styles
3 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

If condition inside while???

Hi source is text file named alpha.text containing entries ABC DEF XYZ YYY BBB I need a small shell script which should do read the entries in alpha.text one by one and check it with the current date. If the date is Mon (monday) and entry read from the file is 'DEF' then this should... (4 Replies)
Discussion started by: PrasannaKS
4 Replies

9. Shell Programming and Scripting

using flag inside a for loop to check condition

I have a logic like this It initializes the flag variable as "T" at the beginning of the loop everytime Inside each loop it checks for two conditions and updates the flag variable as "A" or "B" In the end of the loop it checks for the value of the variable flag for "A" or "B" and execute... (4 Replies)
Discussion started by: codeman007
4 Replies

10. Shell Programming and Scripting

Checking condition inside the loop

Hi all, I have one clarification i am using the loop which will process for each record .suppose there is f ailure in the first record it need to send mail and process the next .my code: defcount=`cat <filename>|wc -l` while ] do if <some condiotion> then echo "mail" fi done so... (1 Reply)
Discussion started by: ithirak17
1 Replies

Featured Tech Videos