LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-15-2003, 07:20 AM   #1
gmitra
LQ Newbie
 
Registered: Jun 2002
Location: Slovak Republic
Distribution: RedHat, Mandrake, Gentoo
Posts: 25

Rep: Reputation: 15
BASH: How to get current workin directory?


Hello,
is anyone who could give me an advice how to check the correctness of current working directory? Not by $PWD which
writes the full path, just the name of directocty in which you are.

Thanks. Gmitra.
 
Old 09-15-2003, 07:45 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Would this help:
Code:
#!/bin/bash

PWD="`pwd`"

echo "Normal pwd : ${PWD}"
echo ""
echo "Stripped pwd : ${PWD##/*/}"
 
Old 09-15-2003, 08:20 AM   #3
gmitra
LQ Newbie
 
Registered: Jun 2002
Location: Slovak Republic
Distribution: RedHat, Mandrake, Gentoo
Posts: 25

Original Poster
Rep: Reputation: 15
line PWD="'pwd'" sets PWD to string pwd
but working PWD=$PWD
Thanks.

Where can I find more info about tricks like this ${PWD##/*/}?
 
Old 09-15-2003, 08:27 AM   #4
gmitra
LQ Newbie
 
Registered: Jun 2002
Location: Slovak Republic
Distribution: RedHat, Mandrake, Gentoo
Posts: 25

Original Poster
Rep: Reputation: 15
Oh!
But I would like to set some variable say WD to stripped path.
 
Old 09-15-2003, 09:36 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
line PWD="'pwd'" sets PWD to string pwd
but working PWD=$PWD
PWD="'pwd'" is not what I wrote, that should be "`pwd`"
ie: not single quotes, but the other one (sorry, don't know the english name).

More info/'tricks':

- man bash (might be a bit cryptic at times)
- http://www.freeos.com/guides/lsst/index.html (Linux Shell Scripting Tutorial)
- any book about bash/ksh.

The variable name you choose is free (well, almost. There are some reserved names).

If you want to fill a variable with a certain value:
variablename=value (variable on the left and value on the right)
So WD="foo" will assign foo to the variable WD.

The PWD="`pwd`" (or WD="`pwd`") is a bit more complicated.

The PWD (or WD) part is the name of the variable, nothing strange here.

pwd is a command, so WD="`pwd`" will be interpreted as follows (command line processing):

1) pwd will be executed,
2) `pwd` will be substituted by the result of 1
3) WD will be filled with result of 2

Hope this helps a bit.
 
Old 09-15-2003, 10:55 AM   #6
gmitra
LQ Newbie
 
Registered: Jun 2002
Location: Slovak Republic
Distribution: RedHat, Mandrake, Gentoo
Posts: 25

Original Poster
Rep: Reputation: 15
I have been found possible solution:

WDir="`pwd`"
echo "${WDir##/*/}" > $$.tmp
WDir="`cat $$.tmp`"
rm $$.tmp
echo $WDir

` = back quote

Thank you druuna very much.
 
Old 09-15-2003, 11:21 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
No need for the temp-file:
Code:
WDir="`pwd`"
WDir="${WDir##*/}"
echo $WDir
Or just:
Code:
WDir="${PWD##*/}"
echo $WDir
 
Old 09-15-2003, 11:48 AM   #8
gmitra
LQ Newbie
 
Registered: Jun 2002
Location: Slovak Republic
Distribution: RedHat, Mandrake, Gentoo
Posts: 25

Original Poster
Rep: Reputation: 15
Its great!
What does mean ##*/ ?
Or where I can find more info?
Thx
 
Old 09-15-2003, 07:06 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
A=${B##*/} means: assign to variable A the string contained in variable B, but without the longest part from the start that matches the wildcard */

The wildcard works the same way as in " ls *.txt ". The asterisk (*) means "any number of any character", so " */ " means "any number of any character, followed by a slash ( / )".

So ${B##*/} means "B with the first part removed, up to, and including the last slash.

And ${B#*/} (1 single '#') means "B with the first part removed, up to, and including the first slash.

Examples: given B="/usr/local/doc/somefile.txt"

A=${B##*/} : A = "somefile.txt"
A=${B#*/} : A = "usr/local/doc/somefile.txt"

'%' and '%%' do the opposite: they remove from the end.

A=${B%%/*} : A = "" (empty, everything removed)
A=${B%/*} : A = "/usr/local/doc"

Note that because these remove from the end, the asterisk ( * ) and the slash are swapped. Otherwise nothing would happen. Do you see why?

Like druuna already said you can find this info, and also some more of these "tricks" in "man bash".

Last edited by Hko; 09-15-2003 at 07:08 PM.
 
Old 09-20-2003, 10:30 PM   #10
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
WDir=`basename $PWD`
 
  


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
cd keeps snapping back to current directory when called from bash script ghrellin Linux - General 5 08-28-2005 12:50 PM
how do I find out the current working directory? nodger Programming 2 12-26-2004 11:28 AM
couldn't get to current directory Paxmaster Linux - Newbie 0 11-22-2004 12:37 PM
setting bash to look in current directory before searching the path muhkuhmasta Linux - Newbie 4 09-21-2004 02:08 AM
How to change the path of the current directory ? ndha Linux - General 7 12-03-2003 11:05 AM

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

All times are GMT -5. The time now is 01:49 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