~ubuntu-branches/ubuntu/warty/bash/warty

« back to all changes in this revision

Viewing changes to examples/functions/basename2

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2002-04-08 20:51:41 UTC
  • Revision ID: james.westby@ubuntu.com-20020408205141-qovkhu3a90201hf2
Tags: upstream-2.05a
ImportĀ upstreamĀ versionĀ 2.05a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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