Linux - General This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-03-2011, 10:50 PM
|
#1
|
|
LQ Newbie
Registered: Mar 2011
Posts: 9
Rep:
|
Help Running a Check in Bash Script
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.
|
|
|
|
04-04-2011, 12:28 AM
|
#2
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi,
Can you post the script you have this far.
You are probably going to need regular expressions to check the numbers and $# to check the amount of parameters given.
|
|
|
|
04-04-2011, 12:32 AM
|
#3
|
|
LQ Newbie
Registered: Mar 2011
Posts: 9
Original Poster
Rep:
|
Sorry about that. Here's what I have so far:
#!/bin/bash
declare -i INDEX
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
else
echo $1
Last edited by Duo11; 04-04-2011 at 12:43 AM.
|
|
|
|
04-04-2011, 12:59 AM
|
#4
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi,
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]*" ]]
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.
Hope this helps.
|
|
|
1 members found this post helpful.
|
04-04-2011, 01:13 AM
|
#5
|
|
LQ Newbie
Registered: Mar 2011
Posts: 9
Original Poster
Rep:
|
This is great! I didn't know you could check all the parameters like that. Thanks for the help!
|
|
|
|
04-04-2011, 01:43 AM
|
#6
|
|
LQ Newbie
Registered: Mar 2011
Posts: 9
Original Poster
Rep:
|
Actually, I might've spoke too soon. While the first check for the number of parameters works perfectly, the second check doesn't seem to work:
Code:
unknown-hacker|603> count.sh 2 1
parameter must be a number
It doesn't seem to matter what two parameters you put in.
|
|
|
|
04-04-2011, 02:32 AM
|
#7
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi,
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.....
Hope this helps.
Last edited by druuna; 04-04-2011 at 03:56 AM.
|
|
|
|
04-04-2011, 02:51 AM
|
#8
|
|
Member
Registered: Apr 2010
Posts: 217
Rep:
|
you can use a programming language such as Ruby(1.9+) instead of shell (if you are not doing homework)
Code:
#!/usr/bin/env ruby
if ARGV.size != 2 || ( ARGV[1] < ARGV[0] )
puts "Usage: ruby #{$0} [num1] [num2]. num2 must be larger than num1"
exit
end
num1=ARGV[0]
num2=ARGV[1]
num1.upto(num2).each {|x| puts x}
test run:
Code:
$ ruby test.rb 2 4
2
3
4
$ ruby test.rb 4 2
Usage: ruby test.rb [num1] [num2]. num2 must be larger than num1
$ ruby test.rb
Usage: ruby test.rb [num1] [num2]. num2 must be larger than num1
|
|
|
|
04-04-2011, 03:55 AM
|
#9
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi again,
Just had some more time and had another look: I made a mistake, sorry about that.
Change this line:
if [[ "$1" != "[0-9]*" ]] || [[ "$2" != "[0-9]*" ]]
to this:
if ! [[ "$1" =~ "^[0-9]+$" ]] || ! [[ "$2" =~ "^[0-9]+$" ]]
hOPE THIS HELPS.
|
|
|
|
04-04-2011, 04:40 AM
|
#10
|
|
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113
Rep: 
|
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
|
|
|
|
04-04-2011, 04:49 AM
|
#11
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi,
Quote:
Originally Posted by slakmagik
You can't quote the rhs of =~ and have it treated as a regex, so you need to remove those.
|
Are you sure?? I tested it with the quotes and is does work.....
Just curious.
|
|
|
|
04-04-2011, 05:34 AM
|
#12
|
|
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113
Rep: 
|
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?
|
|
|
|
04-04-2011, 06:09 AM
|
#13
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi,
Quote:
Originally Posted by slakmagik
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!!
|
|
|
|
04-04-2011, 07:04 AM
|
#14
|
|
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113
Rep: 
|
Quote:
Originally Posted by druuna
Does anything since bash 3.1 include 3.1? Otherwise it is explained.
|
Yep, I meant after 3.1, the behavior changed but your using 3.1 explains why it's working for you. 
|
|
|
|
04-04-2011, 08:15 AM
|
#15
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,707
|
If this is for non-learning purposes, use 'seq' to count.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:25 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|