PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > shell script question - changing PS1 based on tty
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

shell script question - changing PS1 based on tty

Réponse
 
LinkBack Outils de la discussion
Vieux 30/05/2007, 04h15   #1
mowgli
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut shell script question - changing PS1 based on tty

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='\[\]root\[\]:\[\]\w\[^[[00m\]$ '
# Colored PS1 based on tty number
TTY1PS='\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
TTY2PS='\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
TTY3PS='\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
TTY4PS='\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
TTY5PS='\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
TTY6PS='\[\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '

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

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

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

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

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

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

elif
[ `tty` = '/dev/tty6' ]
then
PS1=$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

  Réponse avec citation
Vieux 30/05/2007, 04h34   #2
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script question - changing PS1 based on tty

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
  Réponse avec citation
Vieux 30/05/2007, 05h13   #3
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script question - changing PS1 based on tty

On 2007-05-30, mowgli 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.
>
> Can someone check what is the problem with the script that these
> commands execute fine on the CLI and not from inside the script?


You must source the script, or it will have no effect in the
current shell.

> Here's the script:
>
> #!/bin/bash
> # Purpose: To change prompt color based on tty number


tty=`tty` ## See below

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


if [ "$UID" -eq 0 ]

> then
> PS1=$ROOTPS # Set root's PS1
>
> elif
> [ `tty` = '/dev/tty1' ]


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

elif [ "$tty" = /dev/tty1 ]

> then
> PS1=$TTY1PS
> echo "You are on `tty`"
>
> elif
> [ `tty` = '/dev/tty2' ]
> then
> PS1=$TTY2PS
> echo "You are on `tty`"
>
> elif
> [ `tty` = '/dev/tty3' ]
> then
> PS1=$TTY3PS
> echo "You are on `tty`"
>
> elif
> [ `tty` = '/dev/tty4' ]
> then
> PS1=$TTY4PS
> echo "You are on `tty`"
>
> elif
> [ `tty` = '/dev/tty5' ]
> then
> PS1=$TTY5PS
> echo "You are on `tty`"
>
> elif
> [ `tty` = '/dev/tty6' ]
> then
> PS1=$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


I'd shorten the script to:

if [ "$UID" -eq 0 ]
then
PS1='\[\]root\[\]:\[\]\w\[^[[00m\]$ '
else
tty=`tty`
case $tty in
/dev/tty[1-6])
echo "You are on $tty"
num=$(( ${tty#/dev/tty} + 1 ))
PS1='\[[01;3${num}m\]`whoami`\[^[[00m\]:\[\]\w\[\]$ '
;;
*) echo "You are not a valid user of this system."
exit 1
;;
esac
fi

If the name of the script is setPS1, call it this way:

.. setPS1

Or (bash only):

source setPS1


--
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
  Réponse avec citation
Vieux 30/05/2007, 07h01   #4
mowgli
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script question - changing PS1 based on tty


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

  Réponse avec citation
Vieux 30/05/2007, 19h03   #5
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script question - changing PS1 based on tty

On 2007-05-30, mowgli wrote:
>
> 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


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

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

>
> 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h32.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,49246 seconds with 13 queries