Hello, In a script I want to know in which directory I am, but I don't want to know the whole path. I thought it was easy, but I got an errormessage: drek@laptop:~> pwd | basename basename: missing operand Try `basename --help' for more information. I expected to get the output from pwd ( /home/drek/ ) piped to basename. So I expected to get "drek" as the final result. But unfortunately.... Anyone with an explanation? Thanks, André -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 29 September 2008 15:11:59 drek wrote:
Hello,
In a script I want to know in which directory I am, but I don't want to know the whole path. I thought it was easy, but I got an errormessage:
drek@laptop:~> pwd | basename basename: missing operand Try `basename --help' for more information.
I expected to get the output from pwd ( /home/drek/ ) piped to basename. So I expected to get "drek" as the final result. But unfortunately....
basename `pwd` -- with kind regards, Martin Lasarsch, Core Services SUSE LINUX Products GmbH, Maxfeldstr. 5 90409 Nürnberg GF: Markus Rex, HRB 16746 (AG Nürnberg) martin.lasarsch@suse.de - http://www.opensuse.org -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Martin Lasarsch schreef:
On Monday 29 September 2008 15:11:59 drek wrote:
Hello,
In a script I want to know in which directory I am, but I don't want to know the whole path. I thought it was easy, but I got an errormessage:
drek@laptop:~> pwd | basename basename: missing operand Try `basename --help' for more information.
I expected to get the output from pwd ( /home/drek/ ) piped to basename. So I expected to get "drek" as the final result. But unfortunately....
basename `pwd`
basename $(pwd) for those with bad eye sight. -- Jos van Kan registered Linux user #152704 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
2008/9/29 drek <drek@drek.nl>:
Hello,
In a script I want to know in which directory I am, but I don't want to know the whole path. I thought it was easy, but I got an errormessage:
drek@laptop:~> pwd | basename basename: missing operand Try `basename --help' for more information.
I expected to get the output from pwd ( /home/drek/ ) piped to basename. So I expected to get "drek" as the final result. But unfortunately....
Anyone with an explanation?
Thanks, André
basename expects an argument, use "basename $(pwd)" Regards, Ciro N�����r��y隊Z)z{.�ﮞ˛���m�)z{.��+�Z+i�b�*'jW(�f�vǦj)h���Ǿ��i�������
On Mon, 29 Sep 2008, drek wrote:-
Hello,
In a script I want to know in which directory I am, but I don't want to know the whole path.
This should do: WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/} ^ ^ And these are curly braces.
I thought it was easy, but I got an errormessage:
drek@laptop:~> pwd | basename basename: missing operand Try `basename --help' for more information.
I expected to get the output from pwd ( /home/drek/ ) piped to basename. So I expected to get "drek" as the final result. But unfortunately....
Anyone with an explanation?
As others have mentioned, basename requires an argument. As you don't supply one, basename complains. Regards, David Bolt -- Team Acorn: http://www.distributed.net/ OGR-P2 @ ~100Mnodes RC5-72 @ ~15Mkeys SUSE 10.1 32 | | openSUSE 10.3 32b | openSUSE 11.0 32b | openSUSE 10.2 64b | openSUSE 10.3 64b | openSUSE 11.0 64b RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC | RISC OS 3.11 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
----- Original Message ----- From: "David Bolt" <bcrafhfr@davjam.org> To: <opensuse@opensuse.org> Sent: Monday, September 29, 2008 10:02 AM Subject: Re: [opensuse] bash pipe
On Mon, 29 Sep 2008, drek wrote:-
Hello,
In a script I want to know in which directory I am, but I don't want to know the whole path.
This should do:
WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/}
Ahh finally! Phew I was wondering if _anyone_ was going to say anything other than the unnecessary process fork of running basename. -- Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 30 September 2008 00:19, Brian K. White wrote:
----- Original Message ----- From: "David Bolt" <bcrafhfr@davjam.org>
...
This should do:
WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/}
Ahh finally! Phew I was wondering if _anyone_ was going to say anything other than the unnecessary process fork of running basename.
I'd composed a message with this (and the dirname counterpart, ${PWD%/*}), but by the time I added all the caveats for the special cases in which this differs from basename and dirname when you're in the root directory or, for the dirname substitute, any directory whose immediate parent is the root, I decided it wasn't worth it. Here's what I wrote but didn't send at the time: -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- On Monday 29 September 2008 06:11, drek wrote:
Hello,
In a script I want to know in which directory I am, but I don't want to know the whole path. ...
Anyone with an explanation?
You've gotten that already, but I'll give you the most concise and lowest overhead way to do what you want: % echo "$PWD" /home/rschulz/Mail % echo "${PWD##*/}" Mail The reverse is possible, too: % echo "${PWD%/*}" /home/rschulz However, the second one doesn't work well if you're in "/" or a directory whose immediate parent is "/", in which case you get an empty string. The first gives you an empty string if you're in "/", which may or may not be acceptable for you. It is different than what basename and dirname yield, certainly. -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
-- Brian K. White
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tue, 30 Sep 2008, Randall R Schulz wrote:- <snip>
However, the second one doesn't work well if you're in "/" or a directory whose immediate parent is "/", in which case you get an empty string. The first gives you an empty string if you're in "/", which may or may not be acceptable for you. It is different than what basename and dirname yield, certainly.
True. To match the behavior of both dirname and basename, you'd need to check for an empty string and return '/' instead. Regards, David Bolt -- Team Acorn: http://www.distributed.net/ OGR-P2 @ ~100Mnodes RC5-72 @ ~15Mkeys SUSE 10.1 32 | | openSUSE 10.3 32b | openSUSE 11.0 32b | openSUSE 10.2 64b | openSUSE 10.3 64b | openSUSE 11.0 64b RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC | RISC OS 3.11 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 09/29/2008 04:02 PM, David Bolt wrote:
This should do: WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/} ^ ^ And these are curly braces. Thanks, but... Can you explain this? For me, it is just a magic guess with some #'s and a star and slash. How do you come up with this solution?
André -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 30 September 2008 15:57, drek wrote:
On 09/29/2008 04:02 PM, David Bolt wrote:
This should do: WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/} ^ ^ And these are curly braces.
Thanks, but... Can you explain this? For me, it is just a magic guess with some #'s and a star and slash. How do you come up with this solution?
Well, it's well-known among BASH-o-philes. It's a general mechanism for expanding a variable while simultaneously performing some pattern-directed rewriting on the expanded value (leaving the variable's value unchanged). # strips from the beginning, % from the end. A single # or % strips a minimal match (when a * is involved, other patterns or strings having a fixed length) while a double ## or %% strips a maximal match. There's also a generalized pattern editing version using (can you guess?) the slash character, '/'. Another answer to your question is: It's all in the (lengthy) BASH manual page and covered in better books on BASH scripting. I'd venture to guess that the most used case is the simple #-style prefix strip. I say this because when processing options in BASH scripts, one often has something like this: for arg; do case "$arg" in -o) optionO=1 ;; --foo=*) fooVal="${arg#--foo=}" ;; --bar=*) barVal="${arg#--bar=}" ;; -*) echo "Unknown option: \"$arg\"" >&2 eval errorCount++ ;; *) nonOptionArgs=( "${nonOptionArgs[@]}" "$arg" ) ;; esac done
André
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
----- Original Message ----- From: "drek" <drek@drek.nl> To: "opensuse" <opensuse@opensuse.org> Sent: Tuesday, September 30, 2008 6:57 PM Subject: Re: [opensuse] bash pipe On 09/29/2008 04:02 PM, David Bolt wrote:
This should do: WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/}
Thanks, but... Can you explain this? For me, it is just a magic guess with some #'s and a star and slash. How do you come up with this solution?
"man bash" and jump to the section on expansion. (or ksh, or zsh, or any bourne-type shell) No one expected you to know what that syntax was in all it's gory details. The answer was 3 things. It was the litteral answer to your specific question. But it was also the the clue or hint that such syntaxes even existed. And thirdly it was obviously an example usage. Pick up the ball and run with it. Ask others for as much illumination as necessary along the way. But at least try not to ask others to actually do all of your thinking for you or to be your personal tutor. (Asking a person to explain and spoon feed to you something that you could already read for yourself, isn't that the very definition of a tutor?) -- Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Wed, 1 Oct 2008, drek wrote:-
On 09/29/2008 04:02 PM, David Bolt wrote:
This should do: WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/} ^ ^ And these are curly braces. Thanks, but... Can you explain this? For me, it is just a magic guess with some #'s and a star and slash. How do you come up with this solution?
Randall has given you an explanation about what it does. As for where I came across it, initially I saw some mention of it in a book[0] I have, but didn't think much of it. Later I saw others suggesting similar constructs as solutions, so looked at the bash man page and reread that part of the book again. After doing so I've found that some of the useful, but unused by me, parameter expansions can come in handy. One of these useful expansions is this: SOME_VAR=${SOME_VAR:-value} which will assign the value "value" to SOME_VAR if it's empty, or leaves it alone if it's not. This would mean that you can get the exact same behaviour of basename by using: MY_DIR=${PWD##*/} MY_DIR=${MY_DIR:-/} or dirname using: MY_DIR=${PWD%/*} MY_DIR=${MY_DIR:-/} Just to give you a bit more insight, here's a quick comparison: davjam@adder:/local2/possible-viruses> MY_DIR="${PWD##*/}" ; MY_DIR="${MY_DIR:-/}" ; echo "${MY_DIR}" $(basename "${PWD}") possible-viruses possible-viruses davjam@adder:/local2/possible-viruses> davjam@adder:/local2/possible-viruses> MY_DIR="${PWD%/*}" ; MY_DIR="${MY_DIR:-/}" ; echo "${MY_DIR}" $(dirname "${PWD}") /local2 /local2 davjam@adder:/local2/possible-viruses> davjam@adder:/local2/possible-viruses> pushd / / /local2/possible-viruses davjam@adder:/local2/possible-viruses> davjam@adder:/> MY_DIR="${PWD##*/}" ; MY_DIR="${MY_DIR:-/}" ; echo "${MY_DIR}" $(basename "${PWD}") / / davjam@adder:/local2/possible-viruses> davjam@adder:/> MY_DIR="${PWD%/*}" ; MY_DIR="${MY_DIR:-/}" ; echo "${MY_DIR}" $(dirname "${PWD}") / / [0] Beginning Linux Programming, 3rd edition. ISBN 0-7645-4497-7 Regards, David Bolt -- Team Acorn: http://www.distributed.net/ OGR-P2 @ ~100Mnodes RC5-72 @ ~15Mkeys SUSE 10.1 32 | | openSUSE 10.3 32b | openSUSE 11.0 32b | openSUSE 10.2 64b | openSUSE 10.3 64b | openSUSE 11.0 64b RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC | RISC OS 3.11 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
drek wrote:
On 09/29/2008 04:02 PM, David Bolt wrote:
This should do: WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/} ^ ^ And these are curly braces. Thanks, but... Can you explain this? For me, it is just a magic guess with some #'s and a star and slash. How do you come up with this solution?
André
Andre See: http://tldp.org/LDP/abs/html/parameter-substitution.html Halfway down the page.... -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
----- Original Message ----- From: "drek" <drek@drek.nl> To: "opensuse" <opensuse@opensuse.org> Sent: Monday, September 29, 2008 9:11 AM Subject: [opensuse] bash pipe Hello,
In a script I want to know in which directory I am, but I don't want to know the whole path.
echo ${PWD##*/} -- Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (8)
-
Brian K. White
-
Ciro Iriarte
-
David Bolt
-
David C. Rankin
-
drek
-
Jos van Kan
-
Martin Lasarsch
-
Randall R Schulz