LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Help Running a Check in Bash Script (https://www.linuxquestions.org/questions/linux-general-1/help-running-a-check-in-bash-script-872795/)

Duo11 04-03-2011 10:50 PM

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.

druuna 04-04-2011 12:28 AM

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.

Duo11 04-04-2011 12:32 AM

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

druuna 04-04-2011 12:59 AM

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.

Duo11 04-04-2011 01:13 AM

This is great! I didn't know you could check all the parameters like that. Thanks for the help!

Duo11 04-04-2011 01:43 AM

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.

druuna 04-04-2011 02:32 AM

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.

kurumi 04-04-2011 02:51 AM

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


druuna 04-04-2011 03:55 AM

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.

slakmagik 04-04-2011 04:40 AM

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


druuna 04-04-2011 04:49 AM

Hi,

Quote:

Originally Posted by slakmagik (Post 4313168)
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.

slakmagik 04-04-2011 05:34 AM

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?

druuna 04-04-2011 06:09 AM

Hi,
Quote:

Originally Posted by slakmagik (Post 4313197)
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!!

slakmagik 04-04-2011 07:04 AM

Quote:

Originally Posted by druuna (Post 4313216)
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. :)

H_TeXMeX_H 04-04-2011 08:15 AM

If this is for non-learning purposes, use 'seq' to count.


All times are GMT -5. The time now is 06:14 PM.