LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-18-2008, 06:58 PM   #1
ninj4fly
LQ Newbie
 
Registered: Feb 2008
Posts: 6

Rep: Reputation: 0
bash log and execute function


Hi,

I have to write bash function that logs the commands before executing them. I try:

function log_and_exec () { logger -p local5.info $@ && $@; }

but it is not working as I expected. When the actual command contains "|" (pipe) for example, what I found in the log file is only that part of the command preceding the "|" character although the entire command is executed.

For example: log_and_exec uptime | grep -o users
logs only the word uptime. Another example using "&&" is:

log_and_exec uptime && date
Again only uptime is logged, both commands are executed. The same behavior when using "echo" instead of "logger".

Any help would be greatly appreciated.
 
Old 09-18-2008, 07:48 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try saving $@ in a another temp var first, then escape any special chars, then pass $temp to the logger.
Call the orig version of the cmd afterwards.
 
Old 09-19-2008, 12:25 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
The function will never see the pipe or any redirection, as the shell parses and removes them before the function is called, unless they are quoted.

Last edited by Mr. C.; 09-19-2008 at 12:27 AM.
 
Old 09-20-2008, 04:35 AM   #4
ninj4fly
LQ Newbie
 
Registered: Feb 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks for your answers.

@chrism01 Saving the command in $temp did not help. I tried:
temp=`echo "$@" | sed -e 's/[|&]/\\&/g'` and logger $temp. The overall effect is exactly as logging "$@", only the part left to these characters is logged.

@Mr. C. It seems you are right.
 
Old 09-20-2008, 05:39 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by Mr. C. View Post
The function will never see the pipe or any redirection, as the shell parses and removes them before the function is called, unless they are quoted.
That's right. Bash will consume the pipe or any other similar special character before your function can read it. You could use an alternate character for redirection or a command line modifier, or pass it scaped to the function so it can see it. Of course that will require some extra work so the function can parse all the stuff and do what's adequate on each case, I hope you get the idea.
 
Old 09-20-2008, 10:31 AM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Create your function like this:

Code:
# Execute command line first, and then log and include 
# status of last command in pipeline.
# To obtain the exit status of each command in any pipeline,
# use the bash PIPESTATUS array in your logger function
# instead of $?.

function log_and_exec () {
    eval "$@"
    echo logger -p local5.info \[$?\] "$@"
}
Call your function with quotes like this:

Code:
log_and_exec 'echo Hello World | cat -n'
If you need to interpolate a variable into the call first, use double quotes:

Code:
log_and_exec "echo I am $USER | perl -pe 's/(\w+)$/\u\$1/'"
 
  


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
How to Measure Time to execute a function in Kernel space barunparichha Linux - Software 4 01-07-2008 11:48 AM
how can you pause child/parent processes in C and execute another function. expeliarmus Programming 5 08-06-2007 01:04 AM
bash scripting help want to execute external function on current item. Benanzo Linux - Software 3 02-03-2007 02:45 PM
Copy a function to other memory location and execute it amit_bst Programming 11 06-28-2006 11:34 PM
Bash looping or function within a function FirmbIT Programming 2 04-24-2006 01:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:51 PM.

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