Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
Hey guys, so I wrote a small script that pretty much just takes in two numbers and counts from the first to the second, e.g.
unknown-hacker|544> count.sh 1 3
1
2
3
My problem is I want to make it so that if you input invalid parameters, such as non-numerical characters, more than 2 numbers, etc., you'd get an error message. Any suggestions? Thanks in advance.
#!/bin/bash
declare -i INDEX
echo "number of parameters : $#"
# check amount of parameters
if [ "$#" -gt "2" ]
then
echo "Wrong amount of parameters"
exit 1
fi
# are parameters numbers
if [[ "$1" != "[0-9]*" ]] || [[ "$2" != "[0-9]*" ]]
then
echo "parameter must be a number"
exit 1
fi
# your original code
if [ $1 -gt $2 ]
then
INDEX=$1
while [[ $INDEX -gt $2 ]] || [[ $INDEX -eq $2 ]]
do
echo $INDEX
INDEX=$INDEX-1
done
elif [ $1 -le $2 ]
then
INDEX=$1
while [[ $INDEX -le $2 ]] || [[ $INDEX -eq $2 ]]
do
echo $INDEX
INDEX=$INDEX+1
done
fi
echo $1
Have a look at the bold part, the first part checks the amount (in this case if there are more then 2), the second checks for a number.
I also edited your code (you have an else statement near the end that should be a fi.
Are you sure you did not make a typo in the second check, I did check it at home and it worked. I'm not home any more and don't have access to a linux box at the moment.
What that piece of code does: $1 and $2 hold the parameters that were given when starting the script.
This part "$1" != "[0-9]*" checks if $1 is not (!=) a number. Same is done for $2. The OR part (||) makes sure that both are checked and if 1 (or both) are not a number the message is shown.
I made a mistake, see post #9 for the solution.....
You can't quote the rhs of =~ and have it treated as a regex, so you need to remove those. Also, there's some errors in the original script: I get '1 2 3 1' when I give it '1 3', for instance.
And I hope this is for the exercise. 'seq' is a perfectly good pre-existing tool.
-- Oh - I forgot to mention that I don't know how you'd want to handle both numbers being the same, but I'd also changed it to just replicate seq's behavior and print the same digit.
Code:
#!/bin/bash
declare -i INDEX
echo "number of parameters : $#"
# check amount of parameters
if [ "$#" -gt "2" ]
then
echo "Wrong amount of parameters"
exit 1
fi
# are parameters numbers
#if [[ "$1" != "[0-9]*" ]] || [[ "$2" != "[0-9]*" ]]
if ! [[ "$1" =~ ^[0-9]+$ ]] || ! [[ "$2" =~ ^[0-9]+$ ]]
then
echo "parameter must be a number"
exit 1
fi
# your original code
if [ $1 -gt $2 ]; then
INDEX=$1
while [[ $INDEX -ge $2 ]]; do
echo $INDEX
INDEX=$INDEX-1
done
elif [ $1 -lt $2 ]; then
INDEX=$1
while [[ $INDEX -le $2 ]]; do
echo $INDEX
INDEX=$INDEX+1
done
else
echo $1
fi
Last edited by slakmagik; 04-04-2011 at 04:43 AM.
Reason: if equal else
Yep - it definitely doesn't work on anything since bash 3.1. Do you have compat* set via shopt? Or are you using an old bash? Or has Debian (or your distro) done something weird?
Yep - it definitely doesn't work on anything since bash 3.1. Do you have compat* set via shopt? Or are you using an old bash? Or has Debian (or your distro) done something weird?
Tested this on:
OS: SLES 10.2 SP2
Bash version: 3.1.17
Just a bare test with a minimal amount of lines:
Code:
~> ./blaat.sh 1 1
No parms : 2
~> ./blaat.sh 1 a
No parms : 2
param must be a number
~> ./blaat.sh a a
No parms : 2
param must be a number
~> ./blaat.sh a 1
No parms : 2
param must be a number
~> cat blaat.sh
#!/bin/bash
echo "No parms : $#"
if ! [[ "$1" =~ "^[0-9]+$" ]] || ! [[ "$2" =~ "^[0-9]+$" ]]
then
echo "param must be a number"
fi
Works like a charm
Does anything since bash 3.1 include 3.1? Otherwise it is explained.
I'm convinced that the OP should use your example, just to make sure!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.