LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2008, 09:39 AM   #1
Jykke
Member
 
Registered: Sep 2005
Posts: 201

Rep: Reputation: 19
is there a val() or int() kind of functions for shell


I am looking for a val() function. I am reading in an input via sed and depending it might have string as content and this results in an error later on the program. I would like to have a check if there is some string to convert it in a number like in basic val("blabla") output is 0

Is there a way to accomplish this in shell?
 
Old 04-08-2008, 09:47 AM   #2
Max Rebo
LQ Newbie
 
Registered: Dec 2005
Distribution: openSuSE
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by Jykke View Post
I am looking for a val() function. I am reading in an input via sed and depending it might have string as content and this results in an error later on the program. I would like to have a check if there is some string to convert it in a number like in basic val("blabla") output is 0

Is there a way to accomplish this in shell?
Look at the follwing page:

http://steve-parker.org/sh/test.shtml

Two-thirds down the page they show an example of a number-guessing game where they validate the input to be numbers only. It may be what you need. Search for "guess the magic number" when you have the page open.

HTH
 
Old 04-08-2008, 10:05 AM   #3
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
try doing a printf with the interger format %d...

like for example ,
awk '{printf "%d", $1}' filename >newfilename.

Gentoo

Last edited by prad77; 04-17-2008 at 03:44 AM.
 
Old 04-09-2008, 09:10 AM   #4
Max Rebo
LQ Newbie
 
Registered: Dec 2005
Distribution: openSuSE
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by prad77 View Post
try doing a printf with the interger format %d...

like for example ,
awk '{printf "%d", $1}' filename >newfilename.
I don't think he wants to format/print the number, he wants a function that will "fail" the convert so that he can know the value tested is -not- a valid number. The link I provided uses grep to test the value.

So I guess he can create a function (not tested at all):

Code:
function isNumber()
{
   echo $1 | grep "[^0-9]" > /dev/null 2>&1
   if [ "$?" -eq "0" ]; then
      return false
   else
      return true
   fi
}
Go to my suggested link above and play with this if you are interested.
 
Old 04-09-2008, 10:43 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
Bear in mind, though, that a typical Unix/Linux environment will provide you with several good, free, already-installed high level programming languages. "Shell scripting" is really not designed to be a general-purpose programming language, and so I think it is generally quite un-productive to waste your time trying to press it into such service. If you (probably) have Perl, Python, PHP, and Ruby at your beck-and-call ... pick one of those.

A program written in any of those tools is absolutely indistinguishable ... when it runs ... from a mind-mess that was cobbled together using bash. You're not here to waste your own time: use the best tool for the job.
 
Old 04-20-2008, 06:43 AM   #6
Jykke
Member
 
Registered: Sep 2005
Posts: 201

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by prad77 View Post
try doing a printf with the interger format %d...

like for example ,
awk '{printf "%d", $1}' filename >newfilename.

Gentoo
No, thanks, this was already quite what I was looking for - I just needed to direct stdout via awk, but the formulation was to the point.

shell is quite the correct choice if I already execute other commands from shell script and just want to process their outputs...
 
Old 04-21-2008, 03:15 PM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
From man bash
Code:
       ((expression))
              The expression is  evaluated  according  to  the  rules
              described  below  under  ARITHMETIC EVALUATION.  If the
              value of the expression is non-zero, the return  status
              is  0;  otherwise  the  return  status  is  1.  This is
              exactly equivalent to let "expression".
 
  


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
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
SHELL scripting/ shell functions mayaabboud Linux - Newbie 6 12-26-2007 08:18 AM
Analogous functions for shell commands. anandsba Programming 3 12-07-2005 06:14 AM
shell script functions iNET_boss Linux - General 1 08-10-2005 04:41 PM
Comparison of shell functions and aliases? slakmagik Programming 3 02-07-2004 05:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:07 PM.

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