Solved

Passing unlimited arguments into a while loop

Posted on 2004-10-12
6
738 Views
Last Modified: 2008-03-03
I have a korn shell script called ex6.ksh.  I invoke it by typing

./ex6.ksh /home/stoteve/brio/essbase 0904.txt 0905.txt 0906.txt

I need the path argument passed in, which it does.  Then I need it to find all the files I list after the first argument.  When I run the below script it ony finds the last one. I made sure all the .txt files above exist in the directory I'm searching in.  I'm not sure how to pass the list of file arguments into my while loop to make it work.  Any help is appreciated.


#!/usr/bin/ksh

#Script to look for many files in a specified directory

while (( $# > 0 ))

do

find $1 -name "$*" -print

shift

done
0
Comment
Question by:elwayisgod
  • 4
  • 2
6 Comments
 
LVL 3

Expert Comment

by:JRSharpUK
ID: 12292391

Make an array of the input (filenames) and then the loop can just read from the array moving down on each loop until all have been passed.


Jim
0
 

Author Comment

by:elwayisgod
ID: 12292417
I have not done arrays enough to know how to get it to work.
0
 
LVL 3

Accepted Solution

by:
JRSharpUK earned 500 total points
ID: 12292464

This link will explain how to produce an array and loop using the array, not only will it explain it but also teach how it all works which i think is more benifical then me just writing all out, plus its not my best subject :)


http://www.tech-recipes.com/bourne_shell_scripting_tips636.html


hope it helps


Jim
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:elwayisgod
ID: 12299701
I have a korn shell script called ex6.ksh.  I invoke it by typing

./ex6.ksh /home/stoteve/brio/essbase 0904.txt 0905.txt 0906.txt

I need the path argument passed in, which it does.  Then I need it to find all the files I list after the first argument.  When I run the below it is just in a non stoppable loop printing 'It works to this point".  How do I have it count down the variables and do it one at a time until $number is 0? I'm just using the echo command to test how far the script gets.  I need it to find all the files in a directory that I put in as a argument $1.  Please help as I'm stumped and have spent too much time on this already.


#!/usr/bin/ksh

#Script to look for many files in a specified directory


number=$#

echo "$number"

while (( $number > 0 ))

do

echo "It works to this point"

find $1 -name $# -print ; $number+- >> newfile


#shift

cat newfile


done
0
 

Author Comment

by:elwayisgod
ID: 12300284
Increasing point value
0
 

Author Comment

by:elwayisgod
ID: 12302069
I got the below code to work.  It took a while but it works. Is there a way to validate the path that is entered on the command line and if it does not exist, spit out a error message?

sam


#!/bin/ksh

DIR=$1
shift

OPTS="-name '$1'"
shift

while [[ "$1" != "" ]]
do
OPTS="$OPTS -o -name '$1'"
shift
done

eval "find $DIR $OPTS"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Sometimes a user will call me frantically, explaining that something has gone wrong and they have tried everything (read - they have messed it up more and now need someone to clean up) and it still does no good, can I help them?!  Usually the standa…
In a recent article here at Experts Exchange (http://www.experts-exchange.com/articles/18880/PaperPort-14-in-Windows-10-A-First-Look.html), I discussed my nine-month sandbox testing of the Windows 10 Technical Preview, specifically with respect to r…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now