Imported from ../bash-2.02.tar.gz.
[platform/upstream/bash.git] / examples / functions / basename2
1 #From: "Grigoriy Strokin" <grg@philol.msu.ru>
2 #Newsgroups: comp.unix.shell
3 #Subject: fast basename and dirname functions for BASH/SH
4 #Date: Sat, 27 Dec 1997 21:18:40 +0300
5 #
6 #Please send your comments to grg@philol.msu.ru
7
8 function basename()
9 {
10   local name="${1##*/}"
11   echo "${name%$2}"
12 }
13
14 function dirname()
15 {
16   local dir="${1%${1##*/}}"
17   [ "${dir:=./}" != "/" ] && dir="${dir%?}"
18   echo "$dir"
19 }
20
21 # Two additional functions:
22 # 1) namename prints the basename without extension
23 # 2) ext prints extension of a file, including "."
24
25 function namename()
26 {
27   local name=${1##*/}
28   local name0="${name%.*}"
29   echo "${name0:-$name}"
30 }
31 function ext()
32 {
33   local name=${1##*/}
34   local name0="${name%.*}"
35   local ext=${name0:+${name#$name0}}
36   echo "${ext:-.}"
37 }
38
39
40
41
42
43