Unix Shell - shell script question - changing PS1 based on tty

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > shell script question - changing PS1 based on tty





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author shell script question - changing PS1 based on tty
mowgli

2007-05-30, 1:21 am

I just made a simple shell script for changing the color username in
the PS1 prompt. The same commands if given one by one on the command
line, change the prompt as per the commands, all fine. But if the
script is executed, the prompt does not change. However the echo
statement is echoed fine and echo $? returns true.

Can someone check what is the problem with the script that these
commands execute fine on the CLI and not from inside the script?


Here's the script:

#!/bin/bash
# Purpose: To change prompt color based on tty number

# root's PS1, regardless of tty
ROOTPS=3D'\[\]root\[\]:\[\]\w\[^[[00m\]$ '
# Colored PS1 based on tty number
TTY1PS=3D'\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
TTY2PS=3D'\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
TTY3PS=3D'\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
TTY4PS=3D'\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
TTY5PS=3D'\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
TTY6PS=3D'\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '

if [ `echo $UID` =3D 0 ]
then
PS1=3D$ROOTPS # Set root's PS1

elif
[ `tty` =3D '/dev/tty1' ]
then
PS1=3D$TTY1PS
echo "You are on `tty`"

elif
[ `tty` =3D '/dev/tty2' ]
then
PS1=3D$TTY2PS
echo "You are on `tty`"

elif
[ `tty` =3D '/dev/tty3' ]
then
PS1=3D$TTY3PS
echo "You are on `tty`"

elif
[ `tty` =3D '/dev/tty4' ]
then
PS1=3D$TTY4PS
echo "You are on `tty`"

elif
[ `tty` =3D '/dev/tty5' ]
then
PS1=3D$TTY5PS
echo "You are on `tty`"

elif
[ `tty` =3D '/dev/tty6' ]
then
PS1=3D$TTY6PS
echo "You are on `tty`"

else
# This won't ever get echoed, or shouldn't.
echo "You are not a valid user of this system."
exit 1
fi



Regards,
mowgli

Bill Marcum

2007-05-30, 1:21 am

On 29 May 2007 20:15:45 -0700, mowgli
<knowledgeless@gmail.com> wrote:
>
>
> I just made a simple shell script for changing the color username in
> the PS1 prompt. The same commands if given one by one on the command
> line, change the prompt as per the commands, all fine. But if the
> script is executed, the prompt does not change. However the echo
> statement is echoed fine and echo $? returns true.
>

Your script changes PS1, but that change is only in effect until the
script ends, unless you source the script.



--
The cost of living has just gone up another dollar a quart.
-- W. C. Fields
mowgli

2007-05-30, 1:21 am


Chris F.A. Johnson wrote:

> Don't call tty every time; store the result in variable and compare that.


Thanks for the suggestion

> I'd shorten the script to:
>
> if [ "$UID" -eq 0 ]

[snip]

I now used `id -u` to get the userid number directly

> num=$(( ${tty#/dev/tty} + 1 ))


Can you please explain this one ? I do not understand the # and {}
thing


Regards,
mowgli

Chris F.A. Johnson

2007-05-30, 1:25 pm

On 2007-05-30, mowgli wrote:
>
> Chris F.A. Johnson wrote:
>
>
> Thanks for the suggestion
>
> [snip]
>
> I now used `id -u` to get the userid number directly


Why use another process? You can get it directly from the $UID
variable.

>
> Can you please explain this one ? I do not understand the # and {}
> thing


Read the Parameter Expansion section of the bash man page.

${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in pathname
expansion. If the pattern matches the beginning of the value of
parameter, then the result of the expansion is the expanded
value of parameter with the shortest matching pattern (the ``#''
case) or the longest matching pattern (the ``##'' case) deleted.
If parameter is @ or *, the pattern removal operation is applied
to each positional parameter in turn, and the expansion is the
resultant list. If parameter is an array variable subscripted
with @ or *, the pattern removal operation is applied to each
member of the array in turn, and the expansion is the resultant
list.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com