Solved

Passing unlimited arguments into a while loop

Posted on 2004-10-12
6
737 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
Comment Utility

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
Comment Utility
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
Comment Utility

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:elwayisgod
Comment Utility
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
Comment Utility
Increasing point value
0
 

Author Comment

by:elwayisgod
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Many people tend to confuse the function of a virus with the one of adware, this misunderstanding of the basic of what each software is and how it operates causes users and organizations to take the wrong security measures that would protect them ag…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

17 Experts available now in Live!

Get 1:1 Help Now