Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more 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".
@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.
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.
# 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/'"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.