LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   how to run 2 bash command back-to-back in bourneShell (http://www.linuxquestions.org/questions/slackware-14/how-to-run-2-bash-command-back-to-back-in-bourneshell-907443/)

nass 10-10-2011 02:06 PM

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

acid_kewpie 10-10-2011 02:08 PM

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.

colucix 10-10-2011 02:17 PM

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


e5150 10-10-2011 02:54 PM

Escape the inner backticks
NEEDED_FOLDER=`basename \`dirname ${A_PATH}\``

acid_kewpie 10-10-2011 03:04 PM

Quote:

Originally Posted by colucix (Post 4494854)
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.

dive 10-10-2011 03:16 PM

Or:
Code:

echo $A_PATH | rev | cut -d'/' -f3 | rev

qweasd 10-10-2011 04:42 PM

Quote:

Originally Posted by colucix (Post 4494854)
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.

nass 10-10-2011 05:23 PM

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 09:20 PM.