ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The above is just an example of the kind of code I'M using. I need a way to represent the end of the string. Is there a way to start from the right side of a line of text instead of the left? What I'M trying to do here is grab the last three digits on end, then the next three and so on. NUM in this case will be a user input so there is no way to hard code the end position. Thanks.
OLDSTRING=$(( ${NUM}-${NEWSTRING} )) # I need to set OLSTRING to NUM - NEWSTRING
My syntax is all wrong here if someone could give me a hand. Thanks.
Why do you think the syntax is wrong? It looks correct to me. In the arithmetic operator you can omit the $ sign to evaluate variables, but it's not mandatory. What the problem is exactly?
Okay, now I see that it is working but with unintended results. I was hoping to set OLDSTRING minues the digits in new string but instead I'M getting OLDSTING - 1. If I were to input 1000 I would want NEWSTRING to hold 000 and old string to hold everything that is left over to the left, in this case, 1. Any ideas?
Code:
for n in ${PROCESSGROUPS};
do
DIGITS=3
echo ${NUM: -$DIGITS}
NEWSTRING=${NUM: -$DIGITS}
OLDSTRING=$(( ${NUM}-${NEWSTRING} )) # I need to set OLSTRING to NUM - NEWSTRING
echo ${OLDSTRING}
done
Quote:
Originally Posted by colucix
Why do you think the syntax is wrong? It looks correct to me. In the arithmetic operator you can omit the $ sign to evaluate variables, but it's not mandatory. What the problem is exactly?
Last edited by Garrett85; 01-07-2013 at 12:59 PM.
Reason: Solved myself
If I were to input 1000 I would want NEWSTRING to hold 000 and old string to hold everything that is left over to the left, in this case, 1. Any ideas?
I think Guttorm already gave the idea:
Code:
NEWSTRING=${NUM:(-3)}
OLDSTRING=${NUM:0:(-3)}
# or
OLDSTRING=${NUM:0:(-${#NEWSTRING})}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.