Solved

Passing unlimited arguments into a while loop

Posted on 2004-10-12
6
740 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
#Citrix #POC #XenDesktop #vCenter #VMware #ESX
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

803 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