LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-16-2009, 05:41 AM   #1
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 384

Rep: Reputation: 61
Shell scripting - Getting just directory string from file path string?


I have a script which has this: -

Code:
DIRECTORY=${filename}
$filename will have a value of something like: -

Code:
/home/sambashare/projects/COMPANY/Office Admin/Year End/BankAccount09.xls
I want the $DIRECTORY variable to just have: -

Code:
/home/sambashare/projects/COMPANY/Office Admin/Year End
i.e. Just the directory. How can I get this in the script? Any ideas?
 
Old 10-16-2009, 05:52 AM   #2
madmadmod
Member
 
Registered: Sep 2009
Distribution: Fedora, Red Hat
Posts: 52

Rep: Reputation: 18
Hi

Code:
echo "/home/sambashare/projects/COMPANY/Office Admin/Year End/BankAccount09.xls"| sed 's/[^\/]*$//'
cheers
 
Old 10-16-2009, 05:56 AM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
DIRECTORY=$(dirname "$filename")

The bash command is dirname

jlinkels
 
Old 10-16-2009, 06:50 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,746

Rep: Reputation: 458Reputation: 458Reputation: 458Reputation: 458Reputation: 458
dirname is not a bash command -it is a separate binary program.
Here is the way to do it in bash:
Code:
DIRECTORY="${filename%/*}"
But, if the path has a trailing '/' then that won't work.
Code:
if [[ ${test:$((${#test}-1)):1} = '/' ]] ; then
 filename=${filename:0:$((${#filename}-1))}
fi
DIRECTORY="${filename%/*}"
That's still not a perfect substitute for the real dirname command because it doesn't take into account the case of having just 'filename=/'

Here's a full version of 'dirname' in pure posix shell:
Code:
dirname()
{ 
    _dirname "$1" && printf "%s\n" "$_DIRNAME"
}

_dirname()
{ 
    _DIRNAME=$1;
    strip_trailing_slashes;
    case $_DIRNAME in 
        "")
            _DIRNAME='/'
	    return
        ;;
        */*)
            _DIRNAME="${_DIRNAME%/*}"
        ;;
        *)
            _DIRNAME='.'
        ;;
    esac;
    strip_trailing_slashes;
}

strip_trailing_slashes () 
{ 
    while [ "${_DIRNAME%/}" != "$_DIRNAME" ]; do
        _DIRNAME=${_DIRNAME%/};
    done
}

dirname "$@"
 
Old 10-16-2009, 08:04 AM   #5
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
Quote:
Originally Posted by gnashley View Post
dirname is not a bash command -it is a separate binary program.
Yeah, I know....

It was shorthand for:
"The command you can issue directly on the Bash command line, or probably in any shell, without any express or implied warranties including but not limited to the fitness for a particular purpose is dirname"

But what I wrote was more to the point

jlinkels
 
Old 10-16-2009, 08:21 AM   #6
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 384

Original Poster
Rep: Reputation: 61
Thanks all - got it
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting problem: string manipulation Philip87 Linux - General 4 09-27-2008 01:29 PM
bash scripting...is a file also a string? Daravon Linux - Newbie 3 09-03-2008 03:38 AM
output the path for files whose names include string "string" (case insensitive) sean_zhang Linux - Newbie 1 03-04-2008 11:59 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Replace string in Shell scripting anandjk1 Programming 2 10-19-2007 08:16 PM


All times are GMT -5. The time now is 12:15 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration