|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|