LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 18.04 / CentOS 7.6
Posts: 397

Rep: Reputation: 62
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
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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 18.04 / CentOS 7.6
Posts: 397

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


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:52 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration