LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2007, 12:37 PM   #1
yvovandoorn
LQ Newbie
 
Registered: Oct 2002
Location: Renton, WA
Distribution: Debian 4, Ubuntu 8.04, RHEL 5, RHEL 4, CentOS 5
Posts: 11

Rep: Reputation: 0
[Bash] Command inside a variable


In a script I am writing I have it write to an error log when it doesn't complete certain tasks. I have the following set as a variable:

Code:
WRITERR="tail -n50 $LOG | grep PRODBACKUP-knik > $ERRLOG"
However what seems to be happening when I call for $WRITERR is that tail tries to tail the entire command, thus it errors out with:

Code:
tail: cannot open `|' for reading: No such file or directory
tail: cannot open `grep' for reading: No such file or directory
tail: cannot open `PRODBACKUP-knik' for reading: No such file or directory
tail: cannot open `>' for reading: No such file or directory
When I run the command inside the script but not as a variable, thus like this:
Code:
tail -n50 $LOG | grep PRODBACKUP-knik > $ERRLOG
Then it works fine, however I have the $WRITERR all over the place in the script and it just seems a lot simpler to go about it that way.

What am I doing wrong?

Below is an excerpt from the script where this is used in a real life scenario:

Code:
HOSTNAME="knik"
LOG="/var/log/backup.log"
ERRLOG="/var/log/backup-err.log"
WRITERR="tail -n50 $LOG | grep PRODBACKUP-knik > $ERRLOG"
ADMINS="emailhere"
TEMPDUMP="/usr/local/dumps/prodbackup"

if [ -f $TEMPDUMP/$HOSTNAME.1.tar.gz ];
	then
		echo "PRODBACKUP-$HOSTNAME: Gzipped file has been downloaded successfully!" >> $LOG
	else
		echo "PRODBACKUP-$HOSTNAME: No gzipped file found!" >> $LOG
		echo "PRODBACKUP-$HOSTNAME: FAILURE!!" >> $LOG
		$WRITERR
		mail -s "Weekly Production Backup for $HOSTNAME FAILED" $ADMINS < $ERRLOG
		exit 2
fi
 
Old 01-17-2007, 12:52 PM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
try using backticks ` (not apostrophes ') to surround the tail command:
Code:
WRITERR=`tail -n50 $LOG | grep PRODBACKUP-knik > $ERRLOG`
You can also use VARIABLE=$(command):
Code:
WRITERR=$(tail -n50 $LOG | grep PRODBACKUP-knik > $ERRLOG)
These two examples are synonymous.

I'm not sure about writing to another variable ($ERRLOG) which is a file though. I'd try getting rid of > $ERRLOG. You would then have a variable WRITERR which contains exactly what $ERRLOG would contain. If you wanted to write that to a log, a $WRITERR > $ERRLOG would need to be put into the script somewhere.

I think.

edit: To execute the variable so that you could write it to a log file, you'd need to use echo (since all that's contained in the variable is text). Thus:
Code:
echo "PRODBACKUP-$HOSTNAME: FAILURE!!" >> $LOG
echo $WRITERR > $ERRLOG
mail -s "Weekly Production Backup for $HOSTNAME FAILED" $ADMINS < $ERRLOG
Again, I think.

Last edited by pwc101; 01-17-2007 at 01:02 PM.
 
Old 01-17-2007, 01:52 PM   #3
yvovandoorn
LQ Newbie
 
Registered: Oct 2002
Location: Renton, WA
Distribution: Debian 4, Ubuntu 8.04, RHEL 5, RHEL 4, CentOS 5
Posts: 11

Original Poster
Rep: Reputation: 0
Awesome with your tips I was able to get it to work.

End result:
Code:
-WRITERR="tail -n50 $LOG | grep PRODBACKUP-knik > $ERRLOG"
+WRITERR=`tail -n50 $LOG | grep PRODBACKUP-knik > $ERRLOG`
Code:
echo "PRODBACKUP-$HOSTNAME: FAILURE!!" >> $LOG
-$WRITERR
+echo $WRITERR
mail -s "Weekly Production Backup for $HOSTNAME FAILED" $ADMINS < $ERRLOG
Thanks for the help.
 
Old 01-17-2007, 04:20 PM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
No worries Glad I got it right! I was learning as much as you were
 
Old 01-17-2007, 07:08 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I'd suggest making it a function because when using a variable like "$WRITERR" it gets filled at the time the interpreter parses the position in the script (do a "sh -x scriptname" to see) and not when you need actually it (of course this is more useful if the log fills rapidly).
Code:
function WRITERR() { 
 tail -n50 "${LOG:=/var/log/backup.log}" 2>/dev/null \
 | grep PRODBACKUP-`hostname -s 2>/dev/null` \
 >> "${ERRLOG:=/var/log/backup-err.log}" 2>/dev/null
 }
Making it a function you can
- use it in the same spot you do now,
- reuse it whenever necessary,
- keep your code easy to read and
- easier extendable.
 
Old 01-20-2007, 06:48 PM   #6
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Just as a complement for this topic, we have to remember the "eval" builtin shell command. It evaluates the content of a variable and then executes it.

In your first question, all you would need execute "eval $WRITERR" to obtain the expected result.

This command is useful when we have to programmatically assemble a command line; we can even use apostrophes to delay command substitution.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipe inside variable isn't working in bash Reginald0 Linux - General 6 12-09-2015 09:56 AM
Variable expansion inside of a bash script! A.S.Q. Linux - Newbie 4 09-29-2006 09:09 AM
simple bash variable command itz2000 Programming 6 06-19-2006 08:24 PM
Bash: command works but not inside of for loop. RijilV Programming 3 05-21-2006 08:29 PM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 03:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration