Operating System - Linux
1753882 Members
7382 Online
108809 Solutions
New Discussion юеВ

Using a file with messages and using in the message with commands in a function ...

 
SOLVED
Go to solution
Manuales
Super Advisor

Using a file with messages and using in the message with commands in a function ...

Hi ..
i have a file where there are messages:

001#$(date) This is message one with $valor files
002#$(date) There are $v files on $v path
...
...
...

in other file i obtain the message, it depends the case:

one=`cat messages.txt | grep 001 | cut -d"#" -f2`

i invoke a function named hola
hola $one

function hola contains:
echo $1

and the result is:
$(date) This is message one

and it mus be:

Thu Dec 29 16:17:06 MET 2005 This is a message one with 300 files.

Why it can not shown date?

Thanks, Manuales.
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: Using a file with messages and using in the message with commands in a function ...

Hi Manuales:

You need to use 'eval' :

#!/usr/bin/sh
one=`cat messages.txt | grep 001 | cut -d"#" -f2`
valor=13
function hola
{
eval echo $@ #...note!
}
hola $one
exit 0

Notice that instead of $1 I used $@ since I (and you) wanted the whole string, not just the first argument passed to 'hola'.

Regards!

...JRF...
Manuales
Super Advisor

Re: Using a file with messages and using in the message with commands in a function ...

i did not understand very well ..
do i have next? :

function hola
{
eval echo $@1
or
eval echo $@#1
}

thanks my friend !!!
James R. Ferguson
Acclaimed Contributor

Re: Using a file with messages and using in the message with commands in a function ...

Hi Manuales:

The Forum mangles spaces in posts. I added a comment after the statement. Your script should look like this:

#!/usr/bin/sh
one=`cat messages.txt | grep 001 | cut -d"#" -f2`
valor=300
function hola
{
eval echo $@
}
hola $one
exit 0

The variable is $@ which is the whole set of arguments passed, not $1 which is only the first argument. Run the code the way I posted it and then run it again having changed $@ to $1 to see the difference.

Regards!

...JRF...
Manuales
Super Advisor

Re: Using a file with messages and using in the message with commands in a function ...

o.k. thanks,
i'm going to explain you exactly how script looks like:

File named messages:
001#`date=+'%Y%m%d'`=DEPURACION:=$vv=bytes=from=$hora=inicial=to=$hora=final
002#`date=+'%Y%m%d'`=$vv=bytes=deleted=from=$horainicial=to=$horafinal
003#`date=+'%Y%m%d'`=There=are=not=core=files=to=be=deleted
004#`date=+'%Y%m%d'`=PROBLEMS=TO=DELETE=FILES=$v=bytes=con=$r2
005#"CORE=FILES"

Function:
function notifica
{
one=`echo $1 | sed 's/=/ /g'`
two=`echo $2 | sed 's/=/ /g'`
three=`echo $3 | sed 's/=/ /g'`
echo "$one" >> ${log_monitoring} #Sending message to log
echo "$two" | mailx -s "$three" 123456789@patito.com #Sending message to cellular
#echo "$three" > /dev/console #Sending message to server console
}

If i see $log_monitoring has:
..........
..........
`date +'%Y%m%d'` DEPURACION: $vv bytes from $hora inicial to $hora final
`date +'%Y%m%d'` DEPURACION: $vv bytes from $hora inicial to $hora final
`date +'%Y%m%d'` DEPURACION: $vv bytes from $hora inicial to $hora final
`date +'%Y%m%d'` DEPURACION: $vv bytes from $hora inicial to $hora final

What do i must do for format date appears correctly and not the command `date +'%Y%m%d'` ?

Thanks, Manuales.
James R. Ferguson
Acclaimed Contributor

Re: Using a file with messages and using in the message with commands in a function ...

Hi Manuales:

As I noted, you need to do something like this:

function notifica
{
one=`echo $@ | sed 's/one/ONE/g'`
eval echo ${one} >> ${log_monitoring}
}

Regards!

...JRF...
Manuales
Super Advisor

Re: Using a file with messages and using in the message with commands in a function ...

Hi James !!! how are you !!!
thanks for your answer !!!

look at this function, now is:

Function:
function notifica
{
one=`echo $1 | sed 's/=/ /g'`
two=`echo $2 | sed 's/=/ /g'`
three=`echo $3 | sed 's/=/ /g'`
eval echo $one >> ${log_monitoring} #Sending message to log
eval echo $two | mailx -s "$three" 123456789@patito.com #Sending message to cellular
eval echo $three > /dev/console #Sending message to server console
}

You have already taught how execute a string !!
really thank you very much !!!

Manuales.
God Bless you !!
<><
Manuales
Super Advisor

Re: Using a file with messages and using in the message with commands in a function ...

Thanks James !!!