Operating System - Linux
1753325 Members
5230 Online
108792 Solutions
New Discussion юеВ

Re: How to pass a list from the command line

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

How to pass a list from the command line

Hi

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
9 REPLIES 9
Matti_Kurkela
Honored Contributor
Solution

Re: How to pass a list from the command line

When you call a script like this:

./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
MK
Leo The Cat
Regular Advisor

Re: How to pass a list from the command line

Huh

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

Dennis Handly
Acclaimed Contributor

Re: How to pass a list from the command line

As Matti said, the standard way to crack the command line is with shift. You can also put them in an array, if no more than 1023:
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
Leo The Cat
Regular Advisor

Re: How to pass a list from the command line

Hi Dennis

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




Leo The Cat
Regular Advisor

Re: How to pass a list from the command line

I've try this but not correct

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
James R. Ferguson
Acclaimed Contributor

Re: How to pass a list from the command line

Hi Den:

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...
Leo The Cat
Regular Advisor

Re: How to pass a list from the command line

Hi James

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
Leo The Cat
Regular Advisor

Re: How to pass a list from the command line

Not solved without a temporary file.

Thanks All
Bests Regards
Den
Dennis Handly
Acclaimed Contributor

Re: How to pass a list from the command line

>In fact I'd like to replace < .../oas_replication_cde.txt by the command given in the command line like this:
./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.