LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to extract the last digit from an input parameter??? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-extract-the-last-digit-from-an-input-parameter-762396/)

rillo650 10-16-2009 09:48 AM

How to extract the last digit from an input parameter???
 
Hi, I'm looking to find out how do I write a bash script that will take the last digit from a command and display it? Basically I have my script and I want it to take the last number from the date,extract it and place it on a separate line on its own.

This is what I have so far:

#!/bin/bash

echo 'NEST 304 UNIX Operations'
echo 'Marcus Ramsawak'
echo '803327030'
echo `date`

I'm doing this for school and I'm stuck, I hope I can get some hints and not the answer flat out.

Thanks

pixellany 10-16-2009 09:52 AM

Welcome to LQ!!-----You are the latest "poster child" for the RIGHT way to ask a homework question.

Take a look at the "cut" command--in particular, the "-c" option.

Also "tr"

"man cut" or "man tr" for details

marozsas 10-16-2009 12:44 PM

yes, it s difficult to figure out how many chars are in the string and then, getting the last.
It is more easy to get the first one, isn't ? well, reverse it first, so the last becomes the first.
Guess what is the name of a command that reverse a string ?

catkin 10-16-2009 01:23 PM

It can be done entirely in the shell (that is without using external commands such as cut and tr) by using Shell Parameter Expansion in conjunction with Arithmetic Expansion. That's not easy so a hint is that it uses ${parameter:offset}


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