After the geekdinner of last Tuesday, I couldn’t resist creating my own blog.
As an appetizer, I’ll start with some code snippets I’ve found very usefull. For example the part of my .bashrc which configures the prompt:
smiley() {
ret_val=$?
if [ "$ret_val" = "0" ]
then
echo ":)"
else
echo ":( ($ret_val)"
fi
}
Green="\033[0;32m"
Yellow="\033[1;33m"
Normal="\033[0m"
PS1="\[$Yellow\]\u@\h\[$Normal\]:\[$Green\]\w \$(smiley) \[$Normal\]"
I always use this because:
- the coloring makes it easy to see where my previous command started
- the smiley quickly shows me if and how the previous program exited
2008-02-08 at 9:38 |
Jan,
Welcome to the blog community
2008-02-08 at 13:56 |
Welcome. If you want to be syndicated on planet.geekdiner.be, drop me a line with your rss url and a link to your avatar of choice.
2008-02-12 at 0:04 |
ma godverdekke seg… hier nog al ene.
‘t amusement ermee!
2008-02-12 at 10:18 |
[...] reading this post I tried out the code snippet of Jan but honestly my eyes did not like it. The yellow didn’t [...]
2008-02-14 at 17:00 |
Interesting idea, but why not using the color to show if your command exited with or without error? An example (inspired by you and Bert Deferme):
Red=”33[0;31m"
Green="33[0;32m"
Yellow="33[1;33m"
Normal="33[0m"
correct() {
ret_val=$?
if [ "$ret_val" = "0" ]
then
echo -e “$Green+”
else
echo -e “$Red- ($ret_val)”
fi
}
PS1=”\[$Yellow\]\u@\h\[$Normal\]: \$(correct) \[$Normal\]”
This gives you a green + if the command goes well and a red – with the error code if something goes wrong.
2008-02-14 at 22:12 |
Koen: thx for the suggestion!
2008-02-18 at 18:08 |
Good start! And useful info
cheers,
paul
2008-02-23 at 22:31 |
Reading the comments (thank you all!), I improved the scripts a bit.
Delete sections to your own taste:
Yellow=”\033[1;33m"
Blue="\033[0;34m"
Green="\033[0;32m"
Red="\033[0;31m"
LightRed="\033[1;31m"
Normal="\033[0m"
when_ok() {
ret_val=$?
if [ "$ret_val" = "0" ]
then
echo -e “$@”
fi
return $ret_val
}
when_failed() {
ret_val=$?
if [ "$ret_val" != "0" ]
then
echo -e “$@”
fi
return $ret_val
}
smiley() {
when_ok “:-)”
when_failed “:-( ($?)”
}
PS1=”\$(when_ok ‘\[$Green\]+ ‘)\$(when_failed ‘\[$LightRed\]- ‘)\[$Yellow\]\u@\h\[$Normal\]:\$(when_ok ‘\[$Green\]‘)\$(when_failed ‘\[$LightRed\]‘)\w \$(smiley) \[$Normal\]”
export PS1
(too bad these comments aren’t formatted correctly: the quotes are somehow converted and spaces stripped. If someone knows how to correct this, please let me know)
Note:
it’s important to escape the when_ok/when_failed so that it’s executed at the correct time
when_ok/when_failed need to return the result so that the correct return value can be used in the next when_ok/when_failed
ANSI color codes are explained on wikipedia
2008-02-24 at 0:03 |
Hi Jan,
thanks for the suggestion. I’m definitely going to use it!
And of course, I’m glad to see that you ‘ve finally created your own blog.
Kind regards,
Anje
2008-04-23 at 14:19 |
[...] reading this post I tried out the code snippet of Jan but honestly my eyes did not like it. The yellow didn’t [...]
2009-03-31 at 12:30 |
just for the record:
http://www.thehobbsfamily.net/how-tos/2008/07/28/custom-bash-prompt
2009-05-12 at 15:45 |
[...] reading this post I tried out the code snippet of Jan but honestly my eyes did not like it. The yellow didn’t look [...]
2009-08-19 at 20:12 |
[...] refer to Jan’s smiley system. I like the idea, and tried to implement something similar in [t]csh… with only limited [...]