Afficher un message
Vieux 30/05/2007, 20h03   #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
 
Page generated in 0,05480 seconds with 9 queries