is there a val() or int() kind of functions for shell
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.
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
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
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.
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.
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.
((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".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.