PDA

View Full Version : Bash Help


dawnofmourning
13th December 2011, 06:29 AM
I'm sure this is an easy questions, but here we go...

Trying to take a variable which is:

myid=123456789

And I would like to cut the last digit of the variable and save it to a new variable called LSD

Then, I would like to check if that variable (the last digit), is an even number

Any help would be awesome!

Smoking Tux
13th December 2011, 09:41 AM
Shall do anyone here your homework? :rolleyes:

Maybe this could be helpful:

man sed
man cut
man tail
man case
man for


...good luck! ;)

Skull One
13th December 2011, 10:03 AM

Well, if it is some homework I will not give the answer, but SmokingTux forgot the more important:

man bash

and read carefully the 'Parameter Expansion' section... ;)

aesir
13th December 2011, 10:05 AM
if myid is an integer you can do all that with "let"
you can find it in
man bash
;)

Smoking Tux
13th December 2011, 10:13 AM
Yeah you're right.
Shame on me. How could this ever happen? :doh:
I also forgot 'man test'..

smr54
13th December 2011, 01:55 PM
Well, it may be homework, but this really seems the equivalent of someone asking me how to say hello in Japanese, and me handing you a Japanese textbook. Or, perhaps writing it down in sloppy handwritten Japanese characters. And tell you, search through the textbook for the word nichi, which will be about as useful as telling someone to search the bash man page for let.

If it is homework, then you should REALLY follow the advice given above. On the chance that it's not homework, then, some quick answers.....


If you know how many characters in the string, that is, if it's always 6 numbers, grab the last one with cut.

echo 123456|cut -c 6 will give you the 6th character. If you don't know the amount of characters, here are several ways of doing it.


http://www.unix.com/shell-programming-scripting/49756-how-display-only-last-character-string.html

As for testing if it's odd or even, test is probably the quickest.

At any rate, a quick google came up with this.

http://bash.cyberciti.biz/decision-making/find-whether-number-is-odd-even/

If this is homework, you should be properly embarrassed--on the other hand, one could argue that in most cases, one shouldn't have to justify their asking of a question, but whether I assume the worst of a post, or give them the benefit of a doubt depends upon my mood, whimsical creature that I am.

And, of course, even if it's homework, maybe the person has an excuse that we'd accept--for example, it's due today, but last night, they met their future spouse and gave it priority. Or, they were attacked by two secret agents, and after fighting their way free, something like



http://www.youtube.com/watch?v=wg7UWKPBhXQ

gthill
13th December 2011, 02:30 PM
eg 1
$ ~> myid=123456789
$ ~> LSD=${myid#${myid%?}} # why bother with this?. see eg 2
$ ~> if [ $(expr $LSD % 2) = 0 ];then echo "even";else echo "odd";fi
odd

eg 2
$ ~> myid=123456788
$ ~> if [ $(expr $myid % 2) = 0 ];then echo "even";else echo "odd";fi
even

Gareth Jones
13th December 2011, 05:04 PM
$ ~> myid=123456788
$ ~> if [ $(expr $myid % 2) = 0 ];then echo "even";else echo "odd";fi
even

If it's specifically bash rather than generic POSIX shell:
if (( 12321361 % 2 ))
then
echo Odd
else
echo Even
fi

No need for external commands at all.

See gthill's first example if you really need LSD.

(Trying desparately to find a pun on LSD but failing...)

Gareth

RupertPupkin
13th December 2011, 07:30 PM
I agree that the LSD variable doesn't seem necessary, but if it is then my preference would be to get it like this:
LSD=$((myid % 10))