You are not logged in.
Pages: 1
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
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
stripped=`pwd | sed "s|$sdir||"`
Offline
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
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//"
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
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
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.
Offline
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
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
Pages: 1