Handling newlines in Bash
I have a slight aesthetic problem that I can't seem to wrap my mind around, and I was hoping I might get some input on it here.
Basically, I want to be able to print a status message, execute a command, and then follow it with a message about it succeeding on the same line. That is easy enough to do, I just have something like this: Code:
echo -n "Running command..." Code:
ErrorMsg () So it looks like: Code:
Running command... ERROR: The command has failed! Code:
Running command... Code:
echo -e "\n ERROR: $2" So basically, my problem is that I want to be able to print the error line where I want it, in either situation. It seems that I need some method of detecting if the previous line was ended or not, and then act accordingly. My other idea was to give the newline command to ErrorMsg, so it would look like: Code:
ErrorMsg ERR "\nThis is an error." So it comes out like: Code:
ERROR: I was thinking that I could do something where I check if the first characters of the string are "\n", then strip them adjust the echo command accordingly? Like: Code:
if $string starts with \n; then P.S. Yes, I am aware this is a rather ridiculous question in the grand scheme of things, but I am a bit obsessive and it bothers me that my error message don't all look the same with the current method. |
Hi,
I've been trying to duplicate your problem and I can't (using single command to substitute command 2>/dev/null part). The only thing unclear seems to be this 'command' part of the script, could it be that this is the 'problem'? Could it be that besides the message sent to stderr there's also something going to stdout (piped commands come to mind)? Ok, that doesn't solve your problem but could point to the reason for this behavior. If you want cursor position control you could take a look at tput. It's probably a bit more work to implement, but if you like aesthetic output it could be worth the effort. Hope this helps. |
Why not addan argument to ErrorMsg to tell it whether or not to print a newline before the error?
Dave |
I was considering that as well, simply make a third variable that acts as a bool determining whether or not to do a newline. I was looking for something a bit more automated, if nothing else for the sake of elegance, but that is certainly a fallback option; and really my best one I suppose. Easier than playing around with stripping the newlines off the string and such,
Quote:
Script: Code:
ErrorMsg () Code:
This is what happens when there is no newline: |
Hi,
Quote:
Your second example isn't the same as the first (stdout vs stderr): First: command 2>/dev/null || ErrorMsg ERR "The command has failed!" Second: ping -w 1 1.0.0.0 >/dev/null || ErrorMsg ERR "The command has failed!" The second example fails to create the correct error message on both ping commands. If I change the >/dev/null to 2>/dev/null, only the second ping fails to create the correct error message (I removed the clear command): Code:
$ ./error.on.same.line.2.sh $ bash --version GNU bash, version 3.1.17(1)-release (i686-pc-linux-gnu) Copyright (C) 2005 Free Software Foundation, Inc. |
Does this work the way you want it to?
Code:
ErrorMsg () |
Quote:
Thanks a lot. |
Re your newline issue, we've prob all been there if writing large progs/lots of code.
My (eventually developed) rule is that the msg output fn should do that and no more, in the spirit of the Unix philosophy of KISS and no side-effects/suprises. It's up to the caller to format the way the msg is laid out. Of course in C, Perl that's fairly easy... |
All times are GMT -5. The time now is 01:13 PM. |