Slackware This Forum is for the discussion of Slackware Linux.
|
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-10-2011, 02:06 PM
|
#1
|
Member
Registered: Apr 2006
Location: Athens, Greece
Distribution: slackware, debian, ubuntu
Posts: 666
Rep:
|
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
|
|
|
10-10-2011, 02:08 PM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
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.
|
|
|
10-10-2011, 02:17 PM
|
#3
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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
|
|
|
10-10-2011, 02:54 PM
|
#4
|
Member
Registered: Oct 2005
Location: Sweden
Distribution: lackware and alpine
Posts: 134
Rep:
|
Escape the inner backticks
NEEDED_FOLDER=`basename \`dirname ${A_PATH}\``
|
|
|
10-10-2011, 03:04 PM
|
#5
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
Quote:
Originally Posted by colucix
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.
|
|
|
10-10-2011, 03:16 PM
|
#6
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467
Rep:
|
Or:
Code:
echo $A_PATH | rev | cut -d'/' -f3 | rev
|
|
1 members found this post helpful.
|
10-10-2011, 04:42 PM
|
#7
|
Member
Registered: May 2010
Posts: 621
Rep:
|
Quote:
Originally Posted by colucix
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.
|
10-10-2011, 05:23 PM
|
#8
|
Member
Registered: Apr 2006
Location: Athens, Greece
Distribution: slackware, debian, ubuntu
Posts: 666
Original Poster
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 10:37 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|