Solved

Passing unlimited arguments into a while loop

Posted on 2004-10-12
6
741 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Occasionally Windows/Microsoft Updates will fail to update. We have found a code that will delete all temporary files and re-register all dll's related to Windows/Microsoft Updates! This works 99% of the time to get the updates working again! The…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
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.

837 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