You are not logged in.

#1 2014-11-17 04:30:51

MoonSwan
Member
From: Great White North
Registered: 2008-01-23
Posts: 881

[Solved] Trying to construct a multi-purpose prompt

I have been working for the last two days to customise a promt illustrated in Linux Format magazine.  The purpose of this brand of computing masochism is to make a prompt that will be shown as the result of three statments (see below).

(a)  If I'm logging into a remote system as a user or root I want my prompt to have :ssh: appended to it.  If it's a local session then go to the statement (b).

(b)  If I'm logging in to a local computer as root the prompt should say :root::directory:. If I am not root but it is still a local session then go to statement (c).

(c) Finally, if I'm just a regular user on a local machine I would like my prompt to be :directory:

The code I'm providing below doesn't work.  If I use it in my .bashrc just the first prompt is displayed.  I also realise I don't know how to set up the first condition (statement (a)) so I have left it out.  I'd appreciate some help to craft that code as it is beyond my current level knowledge.

Here's the code I've customised from the original source.  I've been trying to learn how the code in Bash works.  To that end I wrote down all the code I have so far that I'm building to make my prompt script functional.  I would appreciate it if a kind soul could tell me whether my analysis is correct or not.

Feel free to skip the next part of this post.  smile

### My SSH prompt code (which doesn't work as yet ...)
if [[ -n "${SSH_TTY}" ]];then
  PS1='\[\e[0;37m\]: \[\e[1;34m\]\W\[\e[0;37m\] :\[\e[0;37m\]:\[\e[1;32m\]ssh\[\e[0;37m\]: '
elif [[ ${EUID} == 0 ]];then .
  PS1='\[\e[0;37m\]:\[\e[1;32m\]\u\[\e[0;37m\]:\[\e[1;92m\]\W\[\e[0;37m\]:\[\e[0;37m\]:\[\e[1;32m\]ssh\[\e[0;37m\]: '
else
  PS1='\[\e[0;37m\]: \[\e[1;34m\]\W\[\e[0;37m\] :\[\e[m\] \[\e[0;37m\]'
fi

As I understand it the first line just sets up a condition which says "If the login being done is over an SSH connection make a prompt from the next line."  If this statement is false go to the third line.

The second line is the SSH-ed prompt that will be used if the first statement is true.

The third line is confusing for me but I think it says "If the user who is logging in is the root user then go to the next prompt on line four."

The fourth line is the root prompt that will be used if line three is true or go to the fifth line if the statment if false (I think?).  Line six provides the prompt if the other statements were false.

Thank you,
MS

Last edited by MoonSwan (2014-11-18 02:49:53)

Offline

#2 2014-11-17 08:04:46

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Solved] Trying to construct a multi-purpose prompt

For the first test, for a remote conection, I use this:

    if [[ -n $SSH_CLIENT ]]; then
      PS1="blah blah blah"

You have a stray `.` after your second test: bash will interpret that as `source`, so you'll want to remove that...

For readability, leave a space between the `;` and the `then`.

One other point, if you are using bash's `[[`, you don't need to quote the variables (as you haven't in the second test, for root).

See http://mywiki.wooledge.org/BashGuide/Te … _.5B.5B.29


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2014-11-17 10:53:31

dif
Member
From: Stalowa Wola, Poland
Registered: 2009-12-22
Posts: 137

Re: [Solved] Trying to construct a multi-purpose prompt

Hi,

Here's my PS1. It
- shows the exit code of the last command (in yellow if error)
- informs about SSH connection
- informs if you are in a "screen" session (my screen session looks the same as a normal session otherwise)
- displays the current time and directory path
- displays the user and host names
- displays information about the current TTY number or the virtual terminal number
- displays the value of $SHLVL - I think I'll remove this last information from the prompt.
To sum it up, this prompt is more or less (more "more" than not) what you want.

PS1='\n\[\e[1;$(($??33:0))m\]<$?>\[\e[0m\] \t `[[ -n "$SSH_CONNECTION" ]] && echo "\[\e[7;36m\]SSH\[\e[0m\] "`\w \[\e[0m\]\n`[[ $TERM = screen* ]] && echo -n "\[\e[7;34m\]SCREEN\[\e[0m\] "`\[\e[1m\]\u\[\e[0m\]@\[\e[34;1m\]\H\[\e[0m\]:\l+${SHLVL}\$ '

Offline

#4 2014-11-18 00:00:11

MoonSwan
Member
From: Great White North
Registered: 2008-01-23
Posts: 881

Re: [Solved] Trying to construct a multi-purpose prompt

@dif, thanks for the information.  Your example isn't quite what I'm looking for but it does give me some ideas about how to build my prompt.

@jwr, the web page you directed me to seems to actually be written for Bash newbies!  Will wonders never cease?  big_smile  And thanks for the error-hunting.  I'll definitely work on this today and get back to you.  Honestly, I was hoping you'd reply because, after reading your blog and seeing your style of guiding newbie-wannabe-coders, I like your attitude and advice.  smile

Edit:  It occurred to me that some people might interpret the last line of my original post as being someone wishing to curry favour with a Moderator.  This is not the case.  I just happen to like people who are very straight forward when asked for an opinion.  I find this style of, for lack of a better word, teaching to be more easily understood and less likely to lead to mis-interpretations or communication screw-ups.

Last edited by MoonSwan (2014-11-18 00:07:43)

Offline

#5 2014-11-18 00:32:53

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Solved] Trying to construct a multi-purpose prompt

There is no way to curry favour with a mod, other than to courier tacos... smile

That wiki is the go-to resource for bash: if you work directly from that, you can't fail.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2014-11-18 00:55:38

MoonSwan
Member
From: Great White North
Registered: 2008-01-23
Posts: 881

Re: [Solved] Trying to construct a multi-purpose prompt

Thank you for the Bash guide! I will definitely use it.  big_smile

Edit:  I started looking at the guide you've kindly passed on to this Bash acolyte.  I found on there a link to a newer guide which sounds like it will be replacing the "old" guide soon-ish.  My question is:  should I read the original guide or the new one?  I don't have the knowledge one needs in order to skim through the newer one to see if it is mostly finished and correct.  I like the layout and look of the second one but I won't use it if it isn't sufficently documented.  Feedback please! smile

Last edited by MoonSwan (2014-11-18 01:15:04)

Offline

#7 2014-11-18 01:21:15

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Solved] Trying to construct a multi-purpose prompt

The Wiki was started as the FAQs for #bash. The Bash Guide in a newer attempt to consolidate some of the information into more of a stepped introduction.

Start with whichever suits your learning style, confident that it is all good.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#8 2014-11-18 01:44:27

MoonSwan
Member
From: Great White North
Registered: 2008-01-23
Posts: 881

Re: [Solved] Trying to construct a multi-purpose prompt

Thank you!  I'll go back to educating myself now.

Offline

#9 2014-11-18 02:49:12

MoonSwan
Member
From: Great White North
Registered: 2008-01-23
Posts: 881

Re: [Solved] Trying to construct a multi-purpose prompt

Wheee!  I'm so happy because the prompt does work!  Now I can custmoise each one for each computer using all 3 conditions correctly. 

Much thankfulness to jwr (my hero! big_smile) and dif for some prompt inspiration.

Marking this as solved.  big_smile

Offline

Board footer

Powered by FluxBB