Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Extra informative Bash prompt
View unanswered posts
View posts from last 24 hours

Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9  
Reply to topic    Gentoo Forums Forum Index Gentoo Chat
View previous topic :: View next topic  
Author Message
Ramblurr
Tux's lil' helper
Tux's lil' helper


Joined: 18 Dec 2006
Posts: 103

PostPosted: Thu Feb 21, 2008 1:33 am    Post subject: Re: wireless signal strength? Reply with quote

PraetorZero wrote:
Does anybody know a way to add a wireless signal strength meter to the bash prompt?

I'm thinking something along the lines of
Quote:
[###..] praetor@prometheus ~ $
or
Quote:
[87%] praetor@prometheus ~ $
although the former would be preferable.


Well first you need to make some script that can generate that little ASCII graphic. Then look at External Commands in the Prompt
Back to top
View user's profile Send private message
Ramblurr
Tux's lil' helper
Tux's lil' helper


Joined: 18 Dec 2006
Posts: 103

PostPosted: Thu Feb 21, 2008 1:41 am    Post subject: Reply with quote

Here is a oneliner for getting the wireless signal strength. Note: Change IFACE to whatever your wireless interface is 'iwlan0', 'eth1' etc.

Code:
/sbin/iwconfig IFACE | grep Quality | awk '{print $2}' | cut -d '=' -f2 | cut -d '/' -f1


If you put that in a file called 'wlan_strength' or whateverand put it in /usr/local/bin/ as long as that path is in your PATH variable you can do something like

Code:
export PS1="[\$(wlan_strength)%][\u@\h:\w]\$ "



Edit #1: You don't actually have to put that oneliner in an extra file.. copy/paste it into the 'wlan_strength' bit in the PS1 line.

Edit #2: You said you would prefer a graphical output. Well I just whipped up exactly that.

Take the following snippet of code. Place it in a file like ~/prompt.bash, then edit your ~/.bashrc to have:
Code:
[ -f ~/prompt.bash ] && source ~/prompt.bash


prompt.bash
Code:

# Nifty Bash Prompt
IFACE=eth1 # change this

NUM=`/sbin/iwconfig $IFACE | grep Quality | awk '{print $2}' | cut -d '=' -f2 | cut -d '/' -f1`

PS1="\[\033[1;37m\][\[\033[0;32m\]`make_bar $NUM`\[\033[1;37m\]] \[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] "

# Takes one parameter: an integer.
# This is designed to be a % bar so the parameter must be <= 100
# or the output won't be meaningful.
# by Ramblurr
function make_bar
{
    length=5 # change this   
    delim="#" # and this if you please

    # Don't change any of this
    STAT_BAR=""
    ((lim=(($length * 10) * $1) / 1000 ))
    for ((i=0;i<lim;i+=1)); do
        STAT_BAR="${STAT_BAR}$delim"
    done
    while [ ${#STAT_BAR} -ne $length ]
    do
        STAT_BAR+=" "
    done
    echo "$STAT_BAR"
}


All said and done it should look something like this: http://img404.imageshack.us/img404/619/statbarpromptib8.png A nice feature addition would be to have it be color coded, but I don't feel like doing that now .. bed time.
Back to top
View user's profile Send private message
PraetorZero
Apprentice
Apprentice


Joined: 11 Dec 2004
Posts: 239
Location: /home

PostPosted: Thu Feb 21, 2008 3:41 pm    Post subject: Reply with quote

Ramblurr, thank you! I honestly wasn't expecting anyone to go to quite the lengths that you did to answer my question, but you did! I had to make one modification to your script, and that was to move the make_bar declaration process before setting PS1.

Otherwise, it was exactly what I was hoping for! Thanks again! :D
Back to top
View user's profile Send private message
Ramblurr
Tux's lil' helper
Tux's lil' helper


Joined: 18 Dec 2006
Posts: 103

PostPosted: Thu Feb 21, 2008 10:32 pm    Post subject: Reply with quote

PraetorZero wrote:
Ramblurr, thank you! I honestly wasn't expecting anyone to go to quite the lengths that you did to answer my question, but you did! I had to make one modification to your script, and that was to move the make_bar declaration process before setting PS1.

Otherwise, it was exactly what I was hoping for! Thanks again! :D


No problem. It was a good idea and I decided I wanted it too haha. Besides it will be useful for other people I'm sure.
Back to top
View user's profile Send private message
nasaiya
Apprentice
Apprentice


Joined: 17 May 2007
Posts: 157

PostPosted: Wed Nov 12, 2008 5:46 am    Post subject: Reply with quote

Alright everyone... this thread is too cool to let die! Here's my little attempt to get it going again.

After reading the entire thing this is the prompt I came up with... I still need to clean the code (variable names mainly) up a little but it works and looks great (to me) so here it is.

Code:
BLACK="\[\033[0;30m\]";
LBLUE="\[\033[1;34m\]";
BLUE="\[\033[0;34m\]";
YELLOW="\[\033[1;33m\]";
WHITE="\[\033[1;37m\]";
CYAN="\[\033[0;36m\]";
HRED='\[\e[01;31;31m\]';
RED="\[\033[0;31m\]";
LRED="\[\033[1;31m\]";
GREEN="\[\033[0;32m\]";

 #variations for root
   if [ `/usr/bin/whoami` = 'root' ]; then
      _USER_COLOR=$HRED;
      _SPACER_CLR=$HRED;
      _CWD_COLOR=$WHITE;
      _HIST_COLOR=$WHITE;
      _AT_COLOR=$LBLUE;
      _DIRINFO_CLR=$LBLUE;
   else
      _USER_COLOR=$YELLOW;
      _SPACER_CLR=$LBLUE;
      _CWD_COLOR=$HRED;
      _HIST_COLOR=$GREEN;
      _AT_COLOR=$LBLUE;
      _DIRINFO_CLR=$GREEN;
   fi

export PS1="\n\n$_SPACER_CLR--::$YELLOW[ $_USER_COLOR\u$_AT_COLOR@$_USER_COLOR\H  $_DIRINFO_CLR\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files, \$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b  $_CWD_COLOR\w  $YELLOW]$_SPACER_CLR::-- \n:$YELLOW[ $_HIST_COLOR\! $YELLOW]$_SPACER_CLR:-:$YELLOW[ $LRED\t $YELLOW]$_SPACER_CLR:-> \[\033[00m\]\$ ";


At the very least having the history number in it is invaluable!
_________________
If it ain't broke - fix it till it is!
Back to top
View user's profile Send private message
notHerbert
Advocate
Advocate


Joined: 11 Mar 2008
Posts: 2228
Location: 45N 73W

PostPosted: Thu Nov 13, 2008 2:16 pm    Post subject: Reply with quote

That is a nice prompt. Now if you escape the colors properly it will be almost perfect (except for the redundant $YELLOW tags)
Code:
export PS1="\n\n\[$_SPACER_CLR\] etc....



8) :P
Back to top
View user's profile Send private message
nasaiya
Apprentice
Apprentice


Joined: 17 May 2007
Posts: 157

PostPosted: Fri Nov 14, 2008 5:07 am    Post subject: Reply with quote

notHerbert wrote:
That is a nice prompt. Now if you escape the colors properly it will be almost perfect (except for the redundant $YELLOW tags)
Code:
export PS1="\n\n\[$_SPACER_CLR\] etc....



8) :P


Thanks for the info about escaping the colors.. did not know that! I have no idea what you mean about the redundant yellow tags though -- please explain? My reason for having them there is so that those brackets (that actually show up in the prompt) are colored yellow. Is there a different/less redundant way to do that? Like code that says "make every one of these yellow" etc?
_________________
If it ain't broke - fix it till it is!
Back to top
View user's profile Send private message
notHerbert
Advocate
Advocate


Joined: 11 Mar 2008
Posts: 2228
Location: 45N 73W

PostPosted: Fri Nov 14, 2008 5:29 am    Post subject: Reply with quote

nasaiya wrote:
I have no idea what you mean about the redundant yellow tags though -- please explain?


Right ok, my mistake. I didn't notice there were brackets in there that were intended to be yellow. Very cool. 8)
Back to top
View user's profile Send private message
nasaiya
Apprentice
Apprentice


Joined: 17 May 2007
Posts: 157

PostPosted: Fri Nov 14, 2008 5:35 am    Post subject: Reply with quote

notHerbert wrote:

Right ok, my mistake. I didn't notice there were brackets in there that were intended to be yellow. Very cool. 8)


lol it happens. Now that I went back to look at those escapes though I'm confused again...

Take $YELLOW for example:
Code:
YELLOW="\[\033[1;33m\]";


If I escape $YELLOW in the PS1 line wouldn't it turn into
Code:
"\[\[\033[1;33m\]\]"


I really have no idea what I'm doing with this code all I know is "this does that" but it just seems wrong to have an extra set of brackets there... am I wrong?
_________________
If it ain't broke - fix it till it is!
Back to top
View user's profile Send private message
notHerbert
Advocate
Advocate


Joined: 11 Mar 2008
Posts: 2228
Location: 45N 73W

PostPosted: Fri Nov 14, 2008 5:51 am    Post subject: Reply with quote

nasaiya wrote:
Take $YELLOW for example:
Code:
YELLOW="\[\033[1;33m\]";

If I escape $YELLOW in the PS1 line wouldn't it turn into
Code:
"\[\[\033[1;33m\]\]"

Code:
YELLOW="\[\e[1;33m\]"
PS1="\[$YELLOW\] \$ "

becomes
Code:
echo $PS1
\[\[\e[1;33m\]\] $
Which is the same really. The only difference is that the window size will be correctly calculated and the prompt will wrap correctly.

it's the same as
Code:
PS1="\[\e[1;33m\] \$ "



8) :P
Back to top
View user's profile Send private message
nasaiya
Apprentice
Apprentice


Joined: 17 May 2007
Posts: 157

PostPosted: Fri Nov 14, 2008 6:07 am    Post subject: Reply with quote

notHerbert wrote:
Code:
YELLOW="\[\e[1;33m\]"
PS1="\[$YELLOW\]\$ "

becomes
Code:
\[\[\e[1;33m\]\]$
Which is the same really. The only difference is that the window size will be correctly calculated and the prompt will wrap correctly.

it's the same as
Code:
PS1="\[\e[1;33m\]\$ "


8) :P


Ah well I think I get it... You know I actually noticed not 5 minutes ago that it seemed to have a problem wrapping properly when I opened a new konsole with a tab that was already in a really deep directory... I probably would never have figured out why... I'll try that out. Thanks alot!
_________________
If it ain't broke - fix it till it is!
Back to top
View user's profile Send private message
notHerbert
Advocate
Advocate


Joined: 11 Mar 2008
Posts: 2228
Location: 45N 73W

PostPosted: Fri Nov 14, 2008 6:12 am    Post subject: Reply with quote

You're welcome. :)

Of course you can do this
Code:
YELLOW="\e[1;33m"
PS1="\[$YELLOW\] \$ "

echo $PS1
\[\e[1;33m\] $


Which is also the same, and wrapping is handled correctly.

8)
Back to top
View user's profile Send private message
nasaiya
Apprentice
Apprentice


Joined: 17 May 2007
Posts: 157

PostPosted: Sat Nov 15, 2008 12:50 am    Post subject: Reply with quote

notHerbert wrote:
You're welcome. :)

Of course you can do this
Code:
YELLOW="\e[1;33m"
PS1="\[$YELLOW\] \$ "

echo $PS1
\[\e[1;33m\] $


Which is also the same, and wrapping is handled correctly.

8)



Well that looks like the "cleanest" way to me... I don't understand why but it seems the point is you want the "\[\]" to be in the actual line that sets the PS1 variable, and not inside a variable that gets stuck in that line. I'm sure it would make more sense if I knew what I was doing but in this particular case I'm just happy if my prompt works ;) (I know... dangerous words coming from a linux user... hehe)
_________________
If it ain't broke - fix it till it is!
Back to top
View user's profile Send private message
notHerbert
Advocate
Advocate


Joined: 11 Mar 2008
Posts: 2228
Location: 45N 73W

PostPosted: Sat Nov 15, 2008 2:57 pm    Post subject: Reply with quote

nasaiya wrote:
I don't understand why but it seems the point is you want the "\[\]" to be in the actual line that sets the PS1 variable, and not inside a variable that gets stuck in that line.


Because it's in the PS1 variable that the escaping occurs.

It would be kinda pointless to have a 5 line prompt with all kinds of gizmos and gadgets in the prompt , like keeping track of the core density of the Sun, etc... if the terminal is unreadable because of faulty line wrapping. :D

8)
Back to top
View user's profile Send private message
bigsmoke
Tux's lil' helper
Tux's lil' helper


Joined: 09 Aug 2003
Posts: 92
Location: The city of Groningen in The Netherlands

PostPosted: Thu Dec 04, 2008 11:09 pm    Post subject: The quest continues Reply with quote

The quest for the ultimate bash prompt continues...
Back to top
View user's profile Send private message
albright
Advocate
Advocate


Joined: 16 Nov 2003
Posts: 2588
Location: Near Toronto

PostPosted: Fri Dec 05, 2008 12:00 am    Post subject: Reply with quote

also this thread has a nice prompt script linked to it:

https://forums.gentoo.org/viewtopic-t-717037-highlight-.html
_________________
.... there is nothing - absolutely nothing - half so much worth
doing as simply messing about with Linux ...
(apologies to Kenneth Graeme)
Back to top
View user's profile Send private message
bigsmoke
Tux's lil' helper
Tux's lil' helper


Joined: 09 Aug 2003
Posts: 92
Location: The city of Groningen in The Netherlands

PostPosted: Fri Dec 05, 2008 1:10 pm    Post subject: Reply with quote

albright wrote:
also this thread has a nice prompt script linked to it:

https://forums.gentoo.org/viewtopic-t-717037-highlight-.html


Cool! Thanks. :-)
_________________
Gentoo's kind community
has build up an immunity
for the kind of rash
that makes others bash(1)
the newer GNU/Linux user,
a man(1) that is inapt
to flawlessly adapt,
and therefore called a luser.
Back to top
View user's profile Send private message
lvvlvv
n00b
n00b


Joined: 26 Dec 2008
Posts: 1

PostPosted: Fri Dec 26, 2008 4:43 am    Post subject: Reply with quote

If you are using GIT or SVN
Back to top
View user's profile Send private message
eyoung100
Veteran
Veteran


Joined: 23 Jan 2004
Posts: 1428

PostPosted: Tue Apr 20, 2010 12:01 am    Post subject: Reply with quote

I know this thread s a bit old, but I can't help but comment on the laziness of some of you script writers. I say you guys are sometimes lazy, because you are all "re-inventing the wheel" by rewriting your own .bashrc file. Programmers have been borrowing each others code for years now. I say since Gentoo has already given us the code, we should modify the code that already exists instead of making it harder on our systems by sourcing files that may or may not work. This post will show you all how to integrate your .bashrc's into the mechanisms that Gentoo already has in place. After googling for "customize bash prompt," I found this:

Bash Prompt HOWTO

and this, taken from the IBM website, and written by our own Daniel Robbins:

Prompt magic
Use this one, as the link may not exist anymore at IBM.

In the HOWTO, I found the following:
Quote:

Setting the PS? Strings Permanently

Various people and distributions set their PS? strings in different places. The most common places are /etc/profile, /etc/bashrc, ~/.bash_profile, and ~/.bashrc . Johan Kullstam (johan19 at idt dot net) writes:

the PS1 string should be set in .bashrc. this is because non-interactive bashes go out of their way to unset PS1. the bash man page tells how the presence or absence of PS1 is a good way of knowing whether one is in an interactive vs non-interactive (ie script) bash session.

the way i realized this is that startx is a bash script. what this means is, startx will wipe out your prompt. when you set PS1 in .profile (or .bash_profile), login at console, fire up X via startx, your PS1 gets nuked in the process leaving you with the default prompt.

one workaround is to launch xterms and rxvts with the -ls option to force them to read .profile. but any time a shell is called via a non-interactive shell-script middleman PS1 is lost. system(3) uses sh -c which if sh is bash will kill PS1. a better way is to place the PS1 definition in .bashrc. this is read every time bash starts and is where interactive things - eg PS1 should go.

therefore it should be stressed that PS1=..blah.. should be in .bashrc and not .profile.

I tried to duplicate the problem he explains, and encountered a different one: my PROMPT_COMMAND variable (which will be introduced later) was blown away. My knowledge in this area is somewhat shaky, so I'm going to go with what Johan says.


Using this logic, it is advised that all editing should be done in one place, so I found the one place Gentoo stores/and reads the configuration:

In our /etc/profile there is this:
Code:

<snip>
if [ -n "${BASH_VERSION}" ] ; then
   # Newer bash ebuilds include /etc/bash/bashrc which will setup PS1
   # including color.  We leave out color here because not all
   # terminals support it.
   if [ -f /etc/bash/bashrc ] ; then
      # Bash login shells run only /etc/profile
      # Bash non-login shells run only /etc/bash/bashrc
      # Since we want to run /etc/bash/bashrc regardless, we source it
      # from here.  It is unfortunate that there is no way to do
      # this *after* the user's .bash_profile runs (without putting
      # it in the user's dot-files), but it shouldn't make any
      # difference.
      . /etc/bash/bashrc
   else
      PS1='\u@\h \w \$ '
   fi
else
   # Setup a bland default prompt.  Since this prompt should be useable
   # on color and non-color terminals, as well as shells that don't
   # understand sequences such as \h, don't put anything special in it.
   PS1="`whoami`@`uname -n | cut -f1 -d.` \$ "
fi

for sh in /etc/profile.d/*.sh ; do
   if [ -r "$sh" ] ; then
      . "$sh"
   fi
done
unset sh


The first comment states that BASH only looks for /etc/profile to setup PS1. The IF after the comment forces /etc/bash/bashrc to be used in all cases. This is because Gentoo stores all the code for Directory Colors etc. in one place. Since home users likely only have a few users on their systems you want to EDIT THIS FILE to set up the prompt for all users. Only use the .bahrc method when in a corporate environment WHERE NOT HAVING ROOT ACCESS forces you to override the admin's default settings. Using this thought process, I found the snippet of important lines in a Gentoo setup.

Code:

   # Setup PS1 after enabling colors.  Setting it here
   # gaurantees all users will receive the same prompt
   if [[ ${EUID} == 0 ]] ; then
   # Test for Root access.  This way root will be able to have
   # a different more descriptive prompt.
      PS1='\[\e[01;31m\]\h\[\e[01;34m\]  \d \@ \W \$\[\e[00m\] '
   else
   # Normal user prompt, that will be default for even newly
   # created ones.  This allows the .bashrc in /etc/skel to
   # only contain settings that pertain to how a user should
   # be setup in regards to system security.  This approach is
   # more proper as it keeps security moduarized.
      PS1='\[\e[01;32m\]\u@\h\[\e[01;34m\] \d \@ \w \$\[\e[00m\] '
   fi

   alias ls='ls --color=auto'
   alias grep='grep --colour=auto'
else
   if [[ ${EUID} == 0 ]] ; then
   # Same tests as above when using terminals
   # that don't support colors.
      # show root@ when we don't have colors
      PS1='\u@\h \d \@ \W \$ '
   else
      PS1='\u@\h \d \@ \w \$ '
   fi


I added the comments inside the if test, and changed the /033 that were in the originals to /e, and a /a where there was a /007, to make it more understandable and readable. While adding the comments, I also added the Date (/d) and the AM/PM time (/@) to each of the prompts. I'll add something more to the root prompt later when I figure out what I want to see as root.
_________________
The Birth and Growth of Science is the Death and Atrophy of Art -- Unknown
Registerd Linux User #363735
Adopt a Post | Strip Comments| Emerge Wrapper
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Gentoo Chat All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9
Page 9 of 9

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum