Operating System - HP-UX
1832576 Members
3889 Online
110043 Solutions
New Discussion

Why does not work a sentece kept in a variable when is delivered in other script?

 
SOLVED
Go to solution
Manuales
Super Advisor

Why does not work a sentece kept in a variable when is delivered in other script?

Hi ..
i have a program which has the following variable:

VALOR="/usr/bin/ftp_-v_-n_-i_<_$logftp_>_${logftp}_out"

then, this value is sent to other script named script2:

script2 $VALOR

script2 contains many things between them:
....
...
echo $2 | sed 's/_/ /g'
...
...

it guess $2 is an instruction apart of be a command ... but it does not work as command !!!

it does not make the ftp indicated in that variable ... why?

can you help me??

Thanks ...
9 REPLIES 9
Chan 007
Honored Contributor
Solution

Re: Why does not work a sentece kept in a variable when is delivered in other script?

Hi,

when you do echo $2 |sed 's/_//g'

it just removes the "_"

So your command will fail,

i think you need to do sed s/_/ /g

Chan
Sandman!
Honored Contributor

Re: Why does not work a sentece kept in a variable when is delivered in other script?

Hi Manuales,

You need to pipe the output of sed to the shell in order for the command to be executed i.e.

replace...
echo $2 | sed 's/_/ /g'
with...
echo $2 | sed 's/_/ /g' | sh

Only then the ftp will be executed.
James R. Ferguson
Acclaimed Contributor

Re: Why does not work a sentece kept in a variable when is delivered in other script?

Hi Manuales:

You don't need to perform the contortions of embedding underscore characters that you change to spaces. Consider:

# logftp="xxxxxx"
# VALOR="/usr/bin/ftp -v -n -i < ${logftp} > ${logftp}_out"
# ./showme DOTHISFORME "${VALOR}"

If ./showme looks like this below, then the second argument received is your ${VALOR} string and the first argument will be "DOTHISFORME":

# cat ./showme

#!/usr/bin/sh
echo $1 $2
exit 0

In 'showme' you could execute ${VALOR} by writing:

#!/usr/bin/sh
echo "I was told $1 $2"
$2
exit 0

Regards!

...JRF...
Manuales
Super Advisor

Re: Why does not work a sentece kept in a variable when is delivered in other script?

Bad news ...
do not work either "${VALOR}" or "| sh" at the end of the sentence ...

i do not know why!!!??
some time i read about comand exe or exec or something like that, do you know it?
I'm using /uar/bin/sh shell
Please help !!

Thanks .
Manuales
Super Advisor

Re: Why does not work a sentece kept in a variable when is delivered in other script?

Help ..
James R. Ferguson
Acclaimed Contributor

Re: Why does not work a sentece kept in a variable when is delivered in other script?

Hi (again) Manuales:

OK, since you are using redirection we need to modify the wrapper I provided by adding an 'eval' statement (see the 'sh-posix(1)' manpages):

Consider again:

# logftp="xxxxxx"
# VALOR="/usr/bin/ftp -v -n -i < ${logftp} > ${logftp}_out"
# ./launch DOING "${VALOR}"

...where:

# cat ./launch

#!/usr/bin/sh
echo "$1 : $2"
eval $2 # <--- NOTE
exit 0

Regards!

...JRF...
Antonio Cardoso_1
Trusted Contributor

Re: Why does not work a sentece kept in a variable when is delivered in other script?

Manuales,

if what you want to handle in your second script is
script2 param1 "/usr/bin/ftp -v -n -i < $logftp > ${logftp}_out"

i would write script1 with
VALOR="/usr/bin/ftp -v -n -i < ${logftp} > ${logftp}_out"
script2 param1 "${VALOR}"

and then in script2:
# retrieve first param
param1=$1
# retrieve rest of command line.
shift
param2=$*

hope this helps.
antonio.
Sandman!
Honored Contributor

Re: Why does not work a sentece kept in a variable when is delivered in other script?

Manuales,

What is stored inside "logftp" variable. Not sure if the hostname you're trying to ftp to is contained in $logftp. Anyway post the output of the following:

# echo $VALOR
# echo $logftp

thanks!

Re: Why does not work a sentece kept in a variable when is delivered in other script?

Manuales,

in VALOR, change the underscores in blanks

Then the line in script2 should read:
echo $*|sh

If you like to keep the underscores, you have to write it like ...<_${logftp}_>_${logftp}_out
because _$ seems to be something special for the shell and it 'eats' your variable.
In script2, then you should use:
echo $*|sed 's/_/ /g'|sh

Greetings,
Philippe