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 > Finding out the username
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Finding out the username

Réponse
 
LinkBack Outils de la discussion
Vieux 05/12/2006, 22h08   #1
FC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Finding out the username

I have a problem finding out the username of the following strings:
"/usr/home/craigf/scripts/exec" or "/usr/home/craigf" knowing that if
there are directories listed after the username, they may change. I
only need to find out in whose directory I am.

But in front of the username there is always /usr/home/ and it is
sometimes followed by a "/", sometimes the username is the last
characters of the string (ie: /usr/home/craigf)
How can I extract "craigf" from these strings using sed or awk?
If you could me that would be great.
Thanks in advance,

Craig.

  Réponse avec citation
Vieux 06/12/2006, 00h02   #2
Charles A. Landemaine
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding out the username


FC wrote:
> I have a problem finding out the username of the following strings:
> "/usr/home/craigf/scripts/exec" or "/usr/home/craigf" knowing that if
> there are directories listed after the username, they may change. I
> only need to find out in whose directory I am.
>
> But in front of the username there is always /usr/home/ and it is
> sometimes followed by a "/", sometimes the username is the last
> characters of the string (ie: /usr/home/craigf)
> How can I extract "craigf" from these strings using sed or awk?
> If you could me that would be great.
> Thanks in advance,
>
> Craig.


This should work

pwd | cut -d "/" -f4

  Réponse avec citation
Vieux 06/12/2006, 03h23   #3
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding out the username

On 2006-12-05, FC wrote:
> I have a problem finding out the username of the following strings:
> "/usr/home/craigf/scripts/exec" or "/usr/home/craigf" knowing that if
> there are directories listed after the username, they may change. I
> only need to find out in whose directory I am.
>
> But in front of the username there is always /usr/home/ and it is
> sometimes followed by a "/", sometimes the username is the last
> characters of the string (ie: /usr/home/craigf)
> How can I extract "craigf" from these strings using sed or awk?


You don't need sed or awk (or any external command).

dir=$PWD ## or whatever; used in examples below

If the string you want is always the third element in the path:

IFS=/
set -f
set -- $PWD
user=$3

(Reset IFS and perhaps set +f afterwards.)

If it is the last element, with or without a trailing slash, and
perhaps no leading path elements:

dir=${dir/}
case $dir in
*/*) user=${dir##*/} ;;
*) user=$dir ;;
esac


--
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 06/12/2006, 16h00   #4
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding out the username

FC wrote:
> I have a problem finding out the username of the following strings:
> "/usr/home/craigf/scripts/exec" or "/usr/home/craigf" knowing that if
> there are directories listed after the username, they may change. I
> only need to find out in whose directory I am.
>
> But in front of the username there is always /usr/home/ and it is
> sometimes followed by a "/", sometimes the username is the last
> characters of the string (ie: /usr/home/craigf)
> How can I extract "craigf" from these strings using sed or awk?
> If you could me that would be great.
> Thanks in advance,
>


Extract from the string:

echo "/usr/home/craigf/scripts/exec" |
awk -F/ '/^\/usr\/home\// {print $4}'
or
echo "/usr/home/craigf/scripts/exec" |
sed -n 's#^/usr/home/\([^/]*\).*#\1#p'
or
expr "/usr/home/craigf/scripts/exec" : '^/usr/home/\([^/]*\).*'

Maybe it is more precise to look for the file owner:

ls -l "/usr/home/craigf/scripts/exec" | awk '{print $3}'

--
Michael Tosch @ hp : com
  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 05h20.


É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,09841 seconds with 12 queries