?
Solved

Passing unlimited arguments into a while loop

Posted on 2004-10-12
6
Medium Priority
?
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 

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

Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

Question has a verified solution.

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

Just about everyone has an old PC laying around.  Ask anyone in the IT industry, whether they are a professional or play in it as a hobby.  From outdated Desktops to cheap "throwaway" laptops, they are all around and not as hard to "fix up" as you m…
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
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.

762 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