Operating System - HP-UX
1832069 Members
2982 Online
110034 Solutions
New Discussion

for loop with command line parameters

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

for loop with command line parameters

Hi

I'd like to do something like that: call a script named test.ksh with *.txt in command line parameter.

Example:

./test.ksh *.txt

Actually my script is

...
for target in $1
do
echo $target
done




And the output result provided is
*.txt


What is wrong ?

Thanks in advance
Regards
Den.
11 REPLIES 11
Jeff_Traigle
Honored Contributor
Solution

Re: for loop with command line parameters

Not sure why you're output would be *.txt. Assuming you want it to list all of the files ending in .txt, either of these options appears to work:

1.) Quote the argument to your existing script: ./test.ksh "*.txt"

or

2.) Change your script to use $* instead of $1
--
Jeff Traigle
Tim Nelson
Honored Contributor

Re: for loop with command line parameters

how about this.

for target in `ls $1`
do
echo $target
done

Tim Nelson
Honored Contributor

Re: for loop with command line parameters

oops. typo

for x in `eval ls $*`
do
echo $x
done

limited to either 9 or 15 command line args.


Steven Schweda
Honored Contributor

Re: for loop with command line parameters

> What is wrong ?

Where to start...

Here's a directory with a few files.

td192> ls -l
total 40
-rw-rw-rw- 1 antinode 513 4 Jun 12 17:06 a b c .c
-rw-rw-rw- 1 antinode 513 192 Mar 5 10:54 lame1.c
-rwxrwxrwx 1 antinode 513 54 Jun 12 17:03 test.ksh
-rwxrwxrwx 1 antinode 513 56 Jun 12 17:05 test2.ksh
-rwxrwxrwx 1 antinode 513 54 Jun 12 17:07 test3.ksh

td192> echo *.c
a b c .c lame1.c

td192> echo *.fred # No such files exist.
*.fred

That's what shells do nowadays when wildcard
expansion ("globbing") comes up empty-handed.

> 2.) Change your script to use $* instead of $1

Not such a good idea if you have weird file
names. For example:

td192> ./test3.ksh *.c
a
b
c
.c
lame1.c

Better:

td192> ./test2.ksh *.c
a b c .c
lame1.c

td192> diff test3.ksh test2.ksh
3c3
< for target in $*
---
> for target in "$@"


> 1.) Quote the argument to your existing
> script: ./test.ksh "*.txt"

Yeah, but it does put an excessive burden on
the victim.

And, of course, if your wildcard gets
expanded into a zillion file names, you can
overrun the shell's command-line length
limit, requiring a different approach (of
which many exist).

"man sh" and friends cover this kind of
thing, but it's not a three-minute read.
Steven Schweda
Honored Contributor

Re: for loop with command line parameters

Oh, yeah. In case it wasn't obvious:

td192> cat test2.ksh
#!/bin/ksh

for target in "$@"
do
echo $target
done
Leo The Cat
Regular Advisor

Re: for loop with command line parameters

Hi

Sorry, to be exact here what I want really to do.

DEST=$2
cd $1
for target in `ls $3`
do
if [[ ! -f $2/$target ]]
then
echo "Append ..." $DEST/$target
#cp $target $DEST
fi
done


Leo The Cat
Regular Advisor

Re: for loop with command line parameters

The script is called with 3 arguments
Source, destination and what king of file ...

Addnewfiles ${ORACLE_HOME}/jdk.orig/jre/lib/ext ${ORACLE_HOME}/jdk/jre/lib/ext *.jar

Sorry for this incomplete and important information.

Regards
Den.
Steven Schweda
Honored Contributor

Re: for loop with command line parameters

> [..] to be exact here what I want really to
> do.
> [...]

That looks to me like a shell script, which
is not a description of what you really want
to do. Unless it already does what you
really want to do, in which case, the problem
is solved, and everyone is happy at last. If
you're happy, then I'm happy. (If you're
_not_ happy, then you may need to describe
the problem better.)
Leo The Cat
Regular Advisor

Re: for loop with command line parameters

Sorry Steven

I want to copy files from a directory ($1) into another directory ($2) only for non already exists files and only for particular kind of files ($3).

Thanks for your patience ;-)
Best Regards
Den.
Dennis Handly
Acclaimed Contributor

Re: for loop with command line parameters

>I want to copy files from a directory ($1) into another directory ($2) only for non already exists files and only for particular kind of files ($3).

Your script works fine, what's the problem?
DEST=$2
cd $1
for target in $(ls $3); do
if [[ ! -f $2/$target ]]; then
echo "Append ..." $DEST/$target
cp $target $DEST
fi
done

Note you have a "$2" in the "if". You should be consistent and use $DEST.

Your only problem is you need to quote your pattern:
Addnewfiles ${ORACLE_HOME}/jdk.orig/jre/lib/ext \
${ORACLE_HOME}/jdk/jre/lib/ext "*.jar"
Leo The Cat
Regular Advisor

Re: for loop with command line parameters

Answers were enough to go in the good way !
Thanks Guys.