LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-16-2009, 09:48 AM   #1
rillo650
LQ Newbie
 
Registered: Oct 2009
Posts: 1

Rep: Reputation: 0
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
 
Old 10-16-2009, 09:52 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 10-16-2009, 12:44 PM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
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 ?
 
Old 10-16-2009, 01:23 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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}
 
  


Reply



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
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 10:38 AM
C++: Extract numbers from an input file, find av & max, min programmernew Programming 4 10-27-2008 10:00 AM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
Testing parameter input to perl script merana Programming 1 03-17-2006 09:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:21 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration