LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 04-03-2011, 10:50 PM   #1
Duo11
LQ Newbie
 
Registered: Mar 2011
Posts: 9

Rep: Reputation: 0
Question 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.
 
Old 04-04-2011, 12:28 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 04-04-2011, 12:32 AM   #3
Duo11
LQ Newbie
 
Registered: Mar 2011
Posts: 9

Original Poster
Rep: Reputation: 0
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.
 
Old 04-04-2011, 12:59 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
Old 04-04-2011, 01:13 AM   #5
Duo11
LQ Newbie
 
Registered: Mar 2011
Posts: 9

Original Poster
Rep: Reputation: 0
This is great! I didn't know you could check all the parameters like that. Thanks for the help!
 
Old 04-04-2011, 01:43 AM   #6
Duo11
LQ Newbie
 
Registered: Mar 2011
Posts: 9

Original Poster
Rep: Reputation: 0
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.
 
Old 04-04-2011, 02:32 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 04-04-2011, 02:51 AM   #8
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
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
 
Old 04-04-2011, 03:55 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 04-04-2011, 04:40 AM   #10
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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
 
Old 04-04-2011, 04:49 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Quote:
Originally Posted by slakmagik View Post
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.
 
Old 04-04-2011, 05:34 AM   #12
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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?
 
Old 04-04-2011, 06:09 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by slakmagik View Post
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!!
 
Old 04-04-2011, 07:04 AM   #14
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
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.
 
Old 04-04-2011, 08:15 AM   #15
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
If this is for non-learning purposes, use 'seq' to count.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: Check if a program is running naimslim89 Programming 12 05-08-2012 10:24 PM
Can bash check if screensaver is running? zensunni Linux - Newbie 4 11-19-2010 02:31 PM
[SOLVED] bash script - check process is running, if not start zee# Linux - Newbie 8 02-07-2010 05:50 PM
Bash script to check if process is running and send email depam Linux - Newbie 2 04-08-2009 12:11 AM
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:23 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