- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: How to pass a list from the command line
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
Discussions
Discussions
Forums
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
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
тАО05-25-2009 10:36 AM
тАО05-25-2009 10:36 AM
I need to call my script like this:
./myscript.ksh a1001 b1002 c1004 ....
AND
I have to list this with a loop (fow or while). I've tried somethink like that but of course, this does not work.
....
servers=$1
echo "servers: " $servers
USER=test
for i in (`$servers`)
do
echo "\n$i" >> $OUTPUT
sftp $USER@$i < /home/test/bin/oas_replication_cde.txt
done
......
How to do this ?
Thanks a lot
Regards
Den
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2009 11:03 AM
тАО05-25-2009 11:03 AM
Solution./myscript.ksh a1001 b1002 c1004 ...
the parameters are assigned like this:
$1 = a1001
$2 = b1002
$3 = c1004
...
The simplest way to handle this is to use the "shift" command. It moves the parameters so that $2 becomes $1, $3 becomes $2, etc. The old $1 is discarded.
So we can make each parameter in turn be $1 in our loop, and use only $1 for anything we need a parameter for. When $1 becomes empty, we know have processed all the parameters.
USER=test
echo "servers: $*" # all parameters in one
if [ "$1" = "" ]; then
echo "ERROR: no servers listed!"
exit 99
fi
while [ "$1" != "" ]
do
echo "\n$1" >> $OUTPUT
sftp $USER@$1 < /home/test/bin/oas_replication_cde.txt
shift
done
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2009 12:26 PM
тАО05-25-2009 12:26 PM
Re: How to pass a list from the command line
I had closed in a first time this thread but i have an ultimate question .
How to bypass the file oas_replication_cde.txt
That mean going with a full command line without any config or command file
I think this is not possible because how to distinguish command and servers in the parameters ....
Is this (something like that) possible
./myscript.ksh "ls -lrt /tmp" a1001 b1002 c1004
Wher i know that "..." ismy first parameter "!
Bests Regards
Den
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2009 06:29 PM
тАО05-25-2009 06:29 PM
Re: How to pass a list from the command line
set -A array_var -- "${@}"
>How to bypass the file oas_replication_cde.txt
What do you mean? You want to exclude that special file if on the command line?
>I think this is not possible because how to distinguish command and servers in the parameters ...
You need to put a special delimiter there or a - option. Or have a fixed count.
>Is this possible
./myscript.ksh "ls -lrt /tmp" a1001 b1002 c1004
Sure. The first parm is "ls -lrt /tmp".
>Where I know that "..." is my first parameter!
You mean starting with $2?
>Matti: When $1 becomes empty, we know have processed all the parameters.
>if [ "$1" = "" ]; then
>while [ "$1" != "" ]; do
The proper test is to check the count of parms, not their value. You can have empty parms.
if [ $# -gt 0 ]; then
while [ $# -gt 0 ]; do
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 04:42 AM
тАО05-26-2009 04:42 AM
Re: How to pass a list from the command line
In fact I'd like to replace < /home/test/bin/oas_replication_cde.txt by the command given in the command line like this:
./myscript.ksh "ls -lrt" a1001 b1002 c1004 ....
I've try this:
...
USER=applopus
echo "servers: $*" # all parameters in one
if [ "$1" = "" ]; then
echo "ERROR: no servers listed!"
exit 99
fi
echo "Command is: "$1
while [ "$2" != "" ]
do
echo "\n$2" >> $OUTPUT
sftp $USER@$i < $1
shift
done
...
But of course the syntaxe is not correct.
Any Idea ?
Best Regards
Den
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 05:30 AM
тАО05-26-2009 05:30 AM
Re: How to pass a list from the command line
USER=applopus
echo "servers: $*" # all parameters in one
if [ "$1" = "" ]; then
echo "ERROR: no servers listed!"
exit 99
fi
echo "Command is: "$1
while [ "$2" != "" ]
do
echo "\n$2" >> $OUTPUT
sftp $USER@$2 << "EOF"
ls
quit
EOF
shift
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 05:37 AM
тАО05-26-2009 05:37 AM
Re: How to pass a list from the command line
Try this:
#!/usr/bin/sh
OUTPUT=/dev/tty
USER=applopus
echo "servers: $*" # all parameters in one
if [ -z "$1" ]; then
echo "ERROR: no servers listed!"
exit 99
fi
CMD=$1
shift
echo "Command is: ${CMD}"
while [ ! -z "$1" ]
do
echo "\n$1" >> $OUTPUT
echo "sftp $USER@$1 < ${CMD}"
shift
done
...
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 06:44 AM
тАО05-26-2009 06:44 AM
Re: How to pass a list from the command line
Doesnt work. In fact sftp needs the < with a file. It's curious, but finally I've decided to go with a temporary file (Avoid this should nicest but not so bad) with code like this.
...
echo $1 > /tmp/oas_replication.$$$
while [ "$2" != "" ]
do
sftp $USER@$2 < /tmp/oas_replication.$$$
shift
done
rm -f /tmp/oas_replication.$$$
...
Thanks a lot for all your answers. I've learn again many things by this thread.
Bests Regards
Den
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 06:46 AM
тАО05-26-2009 06:46 AM
Re: How to pass a list from the command line
Thanks All
Bests Regards
Den
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2009 07:10 AM
тАО05-30-2009 07:10 AM
Re: How to pass a list from the command line
./myscript.ksh "ls -lrt" a1001 b1002 c1004 ....
sftp $USER@$i < $1
You want to echo the command or the output of that command?
You use a pipe:
CMD=$1
...
echo "$CMD" | sftp $USER@$i
>In fact sftp needs the < with a file.
A pipe should work the same.