ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Here's a wrapper function meant to accept a command to execute and a description.
This should "wrap" commands in a script so that user would see whether smth went wrong.
Code:
COMMAND_WRAPPER () {
printf "%-50s" "$1"
"`$2` 1> /tmp/ws-man.tmp 2>> /tmp/ws-man.tmp" # can't figure out how to make this work
if [ "$?" = "0" ] # execute the command passed and evaluate the result
then
printf "[\E[1;32mdone"
tput sgr0 #reset color; know this is ugly, yet to find a better solution
printf "]\n\n"
return 0
else
printf "[\E[1;31merror"
tput sgr0 #reset color
printf "]\n\n"
printf "=== log start ===\n"
cat /tmp/ws-man.tmp
printf "\n=== log end ===\n"
return 1
fi
}
Now, I can't make the command get executed. I've tried numerous variants, neither worked as expected. The best thing I've got is that the command got executed with redirections treated wrong when doing smth like
Using backticks the way you are will cause the output of the command to be (attempted to be) executed. I am not sure that is what you want. (Although perhaps I misunderstand.)
I am not sure whether your redirection might cause problems since you are redirecting two streams to the same file. Since one is a ">" and the other is a ">>" I am not sure what your are trying to do. If you just want to start a new file that contains both stdout and stderr you would usually do it one of the followings ways (all three lines do the same thing):
This assumes that the command and all of its arguments are different parameters. In other words, don't quote them together as a single argument when you call the function.
Last edited by blackhole54; 04-16-2007 at 02:38 AM.
you have quoted the whole line, (and you don't need the `backticks`)
so it is read by the shell as a single program with an extremely unusual name!
including the spaces.
Complex quoting still gives me problems. With that disclaimer (after doing some experiments), I don't see anything wrong with what you are doing (in post #5) unless you need those (what appear to be) variables evaluated when you give the command COMMAND_WRAPPER. Variables are not evaluated inside single quotes. But when your function executes the command trac-admin I would think the single quotes would already be dropped and the variables would get evaluated at that time.
A couple of things you might try (no promises):
You could replace the single quotes with double quotes. If you do that, then you need to "escape" the inner double quotes with a back slashes like so:
Code:
"\"The inner double quotes here are escaped with a back slash\""
The other thing you could try is totally removing the outer (single) quotes and changing this
Code:
$2 > /tmp/ws-man.tmp
to this
Code:
shift
$* > /tmp/ws-man.tmp
The shift throws away the existing first parameter and then changes the orignal second parameter to be first, original third parameter to be second, etc.
If none of this helps, maybe if you post the error message you are getting it will help somebody figure out what is wrong.
EDIT: Clarified that this post was in response to post #5
Last edited by blackhole54; 04-17-2007 at 01:32 AM.
Blackhole, thanks for your promptly answers.
I've tried "escaping" double quotes and not using single quotes at all, but this won't help (I'd get a message that only 1 arg is passed to initenv).
For the
so "$TEAM $COMPANY project" is obviously treated wrong. So replace it with "$TEAM_$COMPANY_project" and move a bit forward to an error telling that "$TEMPLATES_DIR/trac/templates" is not found. TEMPLATES_DIR is assigned a value right at the previous step (it's an if-then-else). Trying to assign the value manually (TEMPLATES_DIR=/etc/opt/templates) here or at the COMMAND_WRAPPER() won't help either. So I provide a value directly at the command line and finally bump into
Code:
Failed to create environment. [Errno 2] No such file or directory: '/var/opt/ws-man/sample/"$VAR_TEAM_ROOT/trac"
So that's the way the pre-final version of the function looks like:
Code:
# command wrapper; prints the text and executes commands supplied; then displays the exit status
# $1 - message to display; $* - commands to execute
COMMAND_WRAPPER () {
printf "%-50s" "$1" # print the message
while [ "$#" != "0" ]
do
eval $1 &> /tmp/ws-man.tmp # execute the command
shift
done
if [ "$?" = "0" ] # execute the command passed and evaluate the result
then
printf "[\E[1;32mdone"
tput sgr0 #reset color
printf "]\n"
return 0
else
printf "[\E[1;31merror"
tput sgr0 #reset color
printf "]\n"
printf "=== log start ===\n"
cat /tmp/ws-man.tmp
printf "\n=== log end ===\n\n"
return 1
fi
}
I want to drop out of the cycle if any of the commands returns an error state. So that's what I try to do:
Code:
while [ "$#" != "0" ] && [ "$?" = "0" ]
do
shift
eval $1 &> /tmp/ws-man.tmp # execute the command
done
Yet it won't work (so the loop continues until the last argument).
Thanks. To me it was not obvious, but after reading your reply I recalled that [ "some statement" ] is a shortcut to test "some statement". Thanks again, I'll try this.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.