LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-26-2009, 08:45 PM   #1
anonguy9
LQ Newbie
 
Registered: Mar 2009
Posts: 25

Rep: Reputation: 16
Best way to do ${variable:-3:1} ?


(in pure bash)

Expecting Ruby-like syntax is asking too much, I know.. but I'm trying to do this equivalent:

Code:
duration="6.27"
if [ ${duration:-3:1} = "." ]; then
  echo hey!
fi
This fails because "-3" is not valid.


I don't know how to be sneaky and get the exit code of a substitution. This can't work since the setting of duration is a success and returns 0.

Code:
duration=${duration#*.}
if [ ! $? = 0 ]; then
  echo hey!
fi

My solution:

I could research iterating through the string in some other way, but converting it into an array seems sensible to me.

Props to this post for the hint.

Code:
string="1234567"
array=( )
for i in $(seq 0 $((${#string} - 1))); do
  array=( "${array[@]}" "${string:$i:1}" )
done


# Now, since this is impossible:
# ${duration[-3]}

# Count the number of elements.
element_count=${#array[*]}

# Third from the last would be:
my_element_number=$(( $element_count - 3 ))

# And that element in the array would be:
echo ${array[my_element_number]}

So.. My questions are..

- Is my solution sensible?

- Is there a better way to do this (in pure bash) ?
-- Regular expressions are possible, and I think that working with them on my variable would be best. But I couldn't wrap my brain around that. I'm positive this could all be wrapped into a oneliner. There are some great examples in tldp but none seem quite like what I want (third from the right).

- Has anyone already built a nicer function to work with strings as though they were arrays?

Last edited by anonguy9; 03-27-2009 at 10:02 AM.
 
Old 03-27-2009, 05:44 AM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
A negative position parameter needs to be escaped with parentheses or a space.
The escaping avoids it being mistaken for this other type of substitution
${var:-value} Use 'var' if set; otherwise, use 'value'.
Code:
string=1234567
echo ${string:(-3):1}
5
echo ${string: -3:1}
5
 
Old 03-27-2009, 10:03 AM   #3
anonguy9
LQ Newbie
 
Registered: Mar 2009
Posts: 25

Original Poster
Rep: Reputation: 16
Haha! So simple!

Well, I guess I learned more about arrays and iteration with my other solution. Hehe.

Thanks for the help.
 
  


Reply

Tags
array, bash, convert, count, elements, from, string, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
function showing a list of variable and value: (dynamic variable name) budhax Linux - Newbie 1 09-19-2008 07:05 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM


All times are GMT -5. The time now is 11:08 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration