You are not logged in.

#1 2005-09-02 01:33:46

butters
Member
Registered: 2005-08-31
Posts: 35

shell scripting advice [solved]

I'm in need of a little shell scripting help, and I'm sure someone here knows the answer:

I want to echo the current pwd, but minus a constant subpath.  For example suppose I have a variable:

sdir=/sandbox/butters/current

and I'm always in some directory under that at all times when my script is executing.  So if my pwd is:

/sandbox/butters/current/src/usr/bin

I want to echo:

src/usr/bin

I know that the following isolates a filename from a full path:

file=${path##*/}

And I know the following isolates a directory from a full path:

dir=${path%/*}

But I have no clue why these work the way they do.  So how do I strip away $sdir and isolate the changing part of the pwd?  Any help is appreciated.

Offline

#2 2005-09-02 01:44:10

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: shell scripting advice [solved]

PATHX=/foo/blah/bar/sub_bar
PATHY=`echo $PATHX | sed "s#/foo/blah/##"`
echo $PATHY

"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2005-09-02 02:02:05

T-Dawg
Forum Fellow
From: Wilmington, NC
Registered: 2005-01-29
Posts: 2,734

Re: shell scripting advice [solved]

stripped=`pwd | sed "s|$sdir||"`

Offline

#4 2005-09-02 03:39:05

butters
Member
Registered: 2005-08-31
Posts: 35

Re: shell scripting advice [solved]

Penguin wrote:
stripped=`pwd | sed "s|$sdir||"`

Thanks, that works the way I want if I put a / after $sdir in the regex.  I didn't realize that sed would recognize shell variables inside a regex.  I know in perl you need to the the /e switch to make variables expand like that.  In any case, I like the use of the pipe character as a delimiter.  I've been using # as my delimiter for a while since / is pretty stupid when you're dealing with *nix filenames all the time.  But | is much easier on the eyes, IMHO.

Offline

#5 2005-09-02 07:29:14

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: shell scripting advice [solved]

First off, about the sed thing - bash will expand anything that is in double quotes automatically.  So sed 's/$var//' is not the same as sed "s/$var//"

butters wrote:

I know that the following isolates a filename from a full path:
file=${path##*/}

And I know the following isolates a directory from a full path:
dir=${path%/*}

But I have no clue why these work the way they do.  So how do I strip away $sdir and isolate the changing part of the pwd?  Any help is appreciated.

stripme=/sandbox/butters/current/
dir=/sandbox/butters/current/src/usr/bin
dir = ${dir##$stripme}

it goes like this: The # sign means "strip at the front", the % sign means "strip at the end" - one of them means "strip the shortest match", two means "strip the longest match"

so ${x##$y} = strip the longest match from the fron
${x%$y} = strip the shortest match from the end

The longest/shortest stuff really only come into play with wildcards...

x=abcabcabcd
echo ${x%%abc} #does nothing
echo ${x%%abc*} #prints "" (matches the first abc, the rest is the *)
echo ${x%abc*} #prints "abcabc" (matches the last abc, the * is d)

For reference:
http://www.tldp.org/LDP/abs/html/refcards.html#AEN16958

Offline

#6 2005-09-02 09:09:39

butters
Member
Registered: 2005-08-31
Posts: 35

Re: shell scripting advice [solved]

Sweet, that info will certainly come in handy.  I can now get rid of a lot of nasty cut statements in my scripts.

I wonder if anyone has published a guide that covers the differences between scripting in sh, ksh, and bash?  I actually need to support machines that don't have bash installed...  I know that the substring matching stuff works in ksh.  I think I read something about the [[ ]] conditional mode being somehow nonstandard in one of the dialects (in favor of the less-feature-rich [ ] mode).

Offline

#7 2005-09-02 11:24:19

agilyben
Member
Registered: 2005-08-22
Posts: 31

Re: shell scripting advice [solved]

I couldn't help myself, I just had to reply.

You guys are awesome!  I've been doing this for years, but I learn something new every day I come to the archlinux forum.  This is one of the best user communities I have seen.  There's always help at hand and hardly ever any flames. 

It makes one proud to be an Arch'r.  <---- Couldn't resist the play on words.  ;-)


How about a cool ArchLinux T'shirt.  On the front: Arch'rs like to KISS, On the back: The ArchLinux Logo with ArchLinux below it.   big_smile

Offline

#8 2005-09-02 21:10:38

alterkacker
Member
From: Peoples Republic of Boulder
Registered: 2005-01-08
Posts: 52

Re: shell scripting advice [solved]

I never could remember between '#' & '%' which did which end until one day I noticed that '#' is to the left of the '$' in the keyboard row and '%' is to the right. Coincidence? I think not!

On '[' vs. '[[' - in the Bourne sh (the ancestor to both bash & ksh)  '[' is not builtin but is an external program actually linked to 'test' (try ls -l /usr/bin/[); after executing the test the 'if' statement simply examined the return code the way it will for any external command, and '[[' doesn't exist at all.  Whereas in both bash & ksh both '[' & '[[' are interpreted by the shell but with slightly different semantics. I don't know of any reason to use the '[' test anymore - '[[' is more flexible.

Offline

#9 2005-09-03 08:41:00

butters
Member
Registered: 2005-08-31
Posts: 35

Re: shell scripting advice [solved]

The main difference I find between [ ] and [[ ]] with ksh or bash is with compound conditions (conjuncts, disjuncts, etc.):

[[ expression1 && expression2 ]]

can only be rewritten as:

[ expression1 ] && [ expression2 ]

I read somewhere that the first statement is not "POSIX compliant" whereas the second one is.  This is probably because POSIX only assumes the functionality implemented in sh.

Offline

Board footer

Powered by FluxBB