- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- for loop with command line parameters
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 12:59 PM
06-12-2008 12:59 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 01:05 PM
06-12-2008 01:05 PM
Solution1.) Quote the argument to your existing script: ./test.ksh "*.txt"
or
2.) Change your script to use $* instead of $1
Jeff Traigle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 01:06 PM
06-12-2008 01:06 PM
Re: for loop with command line parameters
for target in `ls $1`
do
echo $target
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 01:10 PM
06-12-2008 01:10 PM
Re: for loop with command line parameters
for x in `eval ls $*`
do
echo $x
done
limited to either 9 or 15 command line args.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 01:22 PM
06-12-2008 01:22 PM
Re: for loop with command line parameters
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 01:36 PM
06-12-2008 01:36 PM
Re: for loop with command line parameters
td192> cat test2.ksh
#!/bin/ksh
for target in "$@"
do
echo $target
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 01:52 PM
06-12-2008 01:52 PM
Re: for loop with command line parameters
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 01:57 PM
06-12-2008 01:57 PM
Re: for loop with command line parameters
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 01:59 PM
06-12-2008 01:59 PM
Re: for loop with command line parameters
> 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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 02:11 PM
06-12-2008 02:11 PM
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).
Thanks for your patience ;-)
Best Regards
Den.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2008 08:11 PM
06-12-2008 08:11 PM
Re: for loop with command line parameters
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2008 05:44 AM
06-13-2008 05:44 AM
Re: for loop with command line parameters
Thanks Guys.