Register
It is currently Sat Sep 20, 2014 5:58 am

subsrting expantion *solved*


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Dec 16, 2009 11:46 pm   

Joined: Thu May 14, 2009 1:22 am
Posts: 5
I have read http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion and it seems that I don't understand it. I am trying to do two different things with the command (at different times of course).

I take the ${parameter:offset:length} section to say that ${SHELL:0:-4} will say "/bin/" and it doesn't. I get:
Code:
[user@localhost ~]$ echo ${SHELL:0:-4}
bash: -4: substring expression < 0


I am trying to avoid using sed or awk at all costs in this portion of the script because the number of times it gets called can be over 10,000 per script use... I need to cut just the last 4 characters off of the end, and in another spot I need to remove the first one and last two characters from the variable.

EDIT: I found it at http://bashscripts.org/forum/viewtopic.php?f=7&t=827&sid=82f34f94cc3ff3a4e0587467c841ee7f
Someone needs to write a search engine that works on regexp!


Top
 Profile  
 PostPosted: Thu Dec 17, 2009 3:18 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Just to clarify, the offset and length makes a negativ value uneccesery.

you could just aswell have done it like this:
Code:
len=${#SHELL}
let len=len-4
echo ${SHELL:0:$len}
# short version is:
echo ${SHELL:0:${#SHELL}-4}


or since you're out to get the base directory of a file this is much easier none the less:
Code:
# dirname $SHELL
/bin

or the reverse
Code:
# basename $SHELL
bash


Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP