Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REDIRECTING COMMAND TO LOGFILE 1

Status
Not open for further replies.

hubud

Instructor
Oct 18, 2002
123
GB
The script I have is keeping a logfile. This is done using a redirection of the results of a command, this command being held in a variable.
The command seems to work at the command line however when I run it in the script the results are not appended to the log file.
The relevant part of the script is as follows:

atlist1=`echo "job_id" >> /users/emsadmin/current_job_id`
atlist2=`at -l >> /users/emsadmin/current_job_id`

if [ $time -ge 00 -a $time -le 12 ]
then

at 14:00 today < $leadfile
$atlist1
$atlist2
fi


The result in the file I get is:

job_id: Date: Fri Jan 9 13:05:00 MET 2004


The $atlist command does not seem to give a result, it works at the command line.

Does anyone have any ideas.

cheers

simmo
 
Try something like this:
Code:
atlist1='echo &quot;job_id&quot; >> /users/emsadmin/current_job_id'
atlist2='at -l >> /users/emsadmin/current_job_id'
if [ $time -ge 00 -a $time -le 12 ]
then
        at 14:00 today < $leadfile
        eval $atlist1
        eval $atlist2
fi


Hope This Help
PH.
 
Perhaps it's more logical to put the commands into a function instead of being held in a variable? e.g....

function fnatlist {
echo &quot;job_id&quot; >> /users/emsadmin/current_job_id
at -l >> /users/emsadmin/current_job_id
}

if [ $time -ge 00 -a $time -le 12 ]
then
at 14:00 today < $leadfile
fnatlist
fi

 
I'm running this using the Korn shell. I have no experience using functions and have only seen text on using them in the bash shell.

Do you know of any documentation which covers this?

Further question along a similar line.
Why doesn't the following work?

atlist1=`echo &quot;job_id: Date: `date`&quot; >> /users/emsadmin/current_job_id`

$atlist1

I get the feeling that nesting `` with `` causes it but I'm at a loss how to include the `date` as part of an echo statment whilst held in a variable like that. I'm trying to cut down on typing and this will be used a lot in my scripting.




cheers

simmo
 
#if in bash/ksh

$(date)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
It seems to me that atlist1 and atlist2 had been actually executed when they were defined, since there are no double quotes around the commands. The redirection >> had been executed instead of being retained during definition. So when it was executed later, nothing else had been redirected. I suggest you change the first two lines to the following:

atlist1=&quot;`echo \&quot;job_id\&quot; >> /users/emsadmin/current_job_id`&quot;
atlist2=&quot;`at -l >> /users/emsadmin/current_job_id`&quot;
 
On second thought, have you tried PHP's response, removing the back-quotes and adding eval within the if statement?

The backquotes generally cause the contents to execute immediately. PHP's solution delays the execution till the eval command is encountered.

Please let us know the results.

Regards.
 
Along the same lines, instead of:
========================================
Further question along a similar line.
Why doesn't the following work?

atlist1=`echo &quot;job_id: Date: `date`&quot; >> /users/emsadmin/current_job_id`

$atlist1
=========================================

Try this (immediate execution of `date` on the assignment line)
=========================================
atlist1=&quot;echo \&quot;job_id: Date: `date`\&quot; >> /users/emsadmin/current_job_id&quot;

eval $atlist1
=========================================

And try this (delayed execution of `date` on the eval line:
=========================================
atlist1=&quot;echo \&quot;job_id: Date: \`date\`\&quot; >> /users/emsadmin/current_job_id&quot;

eval $atlist1
=========================================

 
cheers pc888, I had been seeing that when testing the variable assignement on the command line but hadn't really appreciated its significance.
Good learning point.
Will try it out monday.

cheers

simmo
 
hubud,
Glad to hear that you're on the right track. Do keep us posted as yours tests unfold.
Regards.
 
cheers for everyones help.

pc888, not sure if I followed your previous suggestions but this is my result.

The differences I see are that 1)using immediate assignment the variable $atlist1 holds the actual date,2) whereas the delayed assignment holds the $date variable which is not actually computed to the actual date until the eval statement occurs.

There doesn't seem to be any actual difference in practise as to which one to use in the script.

Helped a lot thanks

cheers

simmo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top