LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 10-10-2011, 02:06 PM   #1
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slack(64|32)_v(13.37|14.0), debian6, ubuntu
Posts: 630

Rep: Reputation: 36
how to run 2 bash command back-to-back in bourneShell


hello everyone,
i'm in a bourneShell
and trying to extract some info of the folder path.

say i have:

/some/deep/path/A/1/common

i'm interesting in acquiring the 2nd to last (deep) folder name (i.e A)

I can do that by using dirname and basename commands

basically the expanded way is:

Quote:
TEMP=`dirname "${A_PATH}"`
NEEDED_FOLDER=`basename "${TEMP}"`
how can I do that in one line??

double `basename `dirname ${A_PATH}`` doesn't work

and the $( basename $( dirname ${A_PATH})) notation is used only in Bash...

any clue?

thank you for your help
 
Old 10-10-2011, 02:08 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,384

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
this is an excellent example of why backticks are obsolete. just use $( command ) notation instead of ` command `. there's clearly no way you can nest anything using syntax that is identical at both ends.
 
Old 10-10-2011, 02:17 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Chris, the /bin/sh shell doesn't provide the $(command) syntax. I think it can be easily accomplished using sed or any other editing tool, e.g
Code:
$ echo $A_PATH | sed -r 's:/[^/]+/(.*)/[^/]+:\1:'
deep/path/A/1
 
Old 10-10-2011, 02:54 PM   #4
e5150
Member
 
Registered: Oct 2005
Location: Sweden
Distribution: Slackware
Posts: 74

Rep: Reputation: 41
Escape the inner backticks
NEEDED_FOLDER=`basename \`dirname ${A_PATH}\``
 
Old 10-10-2011, 03:04 PM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,384

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
Quote:
Originally Posted by colucix View Post
Chris, the /bin/sh shell doesn't provide the $(command) syntax. I think it can be easily accomplished using sed or any other editing tool, e.g
Code:
$ echo $A_PATH | sed -r 's:/[^/]+/(.*)/[^/]+:\1:'
deep/path/A/1
I'd still presume they were using bash.
 
Old 10-10-2011, 03:16 PM   #6
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,203

Rep: Reputation: 292Reputation: 292Reputation: 292
Or:
Code:
echo $A_PATH | rev | cut -d'/' -f3 | rev
 
1 members found this post helpful.
Old 10-10-2011, 04:42 PM   #7
qweasd
Member
 
Registered: May 2010
Posts: 436

Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Chris, the /bin/sh shell doesn't provide the $(command) syntax. I think it can be easily accomplished using sed or any other editing tool, e.g
Code:
$ echo $A_PATH | sed -r 's:/[^/]+/(.*)/[^/]+:\1:'
deep/path/A/1
This code does not seem to work as advertized.

Anyway, assuming that (ba)sh is used, it is much faster (according to my naive tests, 191 times faster when used in bulk) to use the built-in substitution rather than to spawn children and open pipes:

Code:
foo=/a/b/c/d/e/f/g ; bar=${foo%/*/*} ; echo ${bar##*/}
I don't know of a way to avoid using a temp variable, but it is still a preferred solution in my eyes, on the account of monstrous performance gains.

Last edited by qweasd; 10-10-2011 at 04:59 PM. Reason: better example
 
1 members found this post helpful.
Old 10-10-2011, 05:23 PM   #8
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slack(64|32)_v(13.37|14.0), debian6, ubuntu
Posts: 630

Original Poster
Rep: Reputation: 36
Thank you all for the replies.
As I stated i am using Bourne shell and not Bash.
So it looks like i'll have to keep the not-so-elegant temp variable approach or switch to Dive's rev&cut one.

Both will work in my situation.
 
  


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
Minicom not working I connected linux machines back-to-back Prabhunath Linux - Networking 2 06-30-2010 04:39 AM
Nvidia won't load no KDE forced back to command line run level 1 nickcommandlinenewbie Mandriva 25 04-16-2010 05:23 PM
Can't log back in after changing runlevels from init 3 to 1 back to 3 manwichmakesameal Slackware 5 06-29-2009 11:36 AM
Connecting 2 linux computer back to back with cross cable. michalng Linux - Networking 8 02-11-2009 07:00 AM
startx command in FC7 reverts me back to the command prompt sriram87 Linux - Newbie 5 01-03-2008 11:18 PM


All times are GMT -5. The time now is 11:01 PM.

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