LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 09-13-2004, 02:57 AM   #1
ericcarlson
Member
 
Registered: Jan 2002
Posts: 161

Rep: Reputation: 30
Calling GAWK gurus!


I have a script I'm trying to make portable. It always runs in a dir called scripts, under a project name which can be any depth. What I need is the project name itself.

Examples:
/top/sub1/sub2/sub3/foo/scripts : I want just "foo"
/another/bar/scripts : I want just "bar"

I have used this
project=$(echo `pwd` | gawk `BEGIN (FS ="[ \t]*|[/]") {print $5}')
which worked but only when the path was a fixed depth (the $5).

Actually, all I'm really trying to do is return the parent folder name to a variable, so if theres a smarter way than GAWK I'm all for it.. Cheers.
 
Old 09-13-2004, 03:25 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
You can use the NF (number of fields) option:

project=$(pwd | gawk -F"/" ' { x = --NF } { print $x }')

Hope this helps.
 
Old 09-13-2004, 04:54 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
What about that:
project=$(pwd | sed -n 's|^.*/\([^/]*\)/[^/]*$|\1|p')

Of course, if the working-dir of the script is not .../script, but the script file actually is there, then you have to append
cd "$(dirname "$0")" &&
just in front of pwd.

Yves.

Last edited by theYinYeti; 09-13-2004 at 04:58 AM.
 
  


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
calling any apache gurus Andy@DP General 2 09-12-2003 12:19 PM
Calling all gurus (and those who think they can help) bmike1 Linux - General 4 08-10-2003 04:10 AM
Calling all PCMCIA/Wireless gurus dubG Linux - Hardware 5 05-22-2003 02:17 AM
calling all networking gurus (nfs install) N_A_J_M Linux - Networking 0 12-15-2002 07:13 PM
Calling all RedHat gurus...help needed.... gballard Linux - Distributions 1 08-03-2002 02:02 AM


All times are GMT -5. The time now is 04:42 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration