Mailinglist Archive: opensuse (1750 mails)
| < Previous | Next > |
Re: [opensuse] bash pipe
- From: David Bolt <bcrafhfr@xxxxxxxxxx>
- Date: Wed, 1 Oct 2008 12:09:16 +0100
- Message-id: <dkmVChecp14IFwq6@xxxxxxxxxxxxxxxxxxx>
On Wed, 1 Oct 2008, drek wrote:-
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@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
On 09/29/2008 04:02 PM, David Bolt wrote:
This should do:Thanks, but... Can you explain this? For me, it is just a magic guess
WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/}
^ ^
And these are curly braces.
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@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
| < Previous | Next > |