- Community Home
- >
- Servers and Operating Systems
- >
- Operating System - Linux
- >
- System Administration
- >
- How to pass a list from the command line
-
- Forums
-
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
- HPE Blog, Austria, Germany & Switzerland
- Blog HPE, France
- HPE Blog, Italy
- HPE Blog, Japan
- HPE Blog, Middle East
- HPE Blog, Russia
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
-
Blogs
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Blog, Latin America
- HPE Blog, Middle East
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
-
Information
- Community
- Welcome
- Getting Started
- FAQ
- Ranking Overview
- Rules of Participation
- Tips and Tricks
- Resources
- Announcements
- Email us
- Feedback
- Information Libraries
- Integrated Systems
- Networking
- Servers
- Storage
- Other HPE Sites
- Support Center
- Aruba Airheads Community
- Enterprise.nxt
- HPE Dev Community
- Cloud28+ Community
- Marketplace
-
Forums
-
Blogs
-
Information
-
English
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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.
Hewlett Packard Enterprise International
- Communities
- HPE Blogs and Forum
© Copyright 2021 Hewlett Packard Enterprise Development LP