Help Running a Check in Bash Script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help Running a Check in Bash Script
# 1  
Old 04-04-2011
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.
Code:
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.

Edit: Here's what my script looks like so far:
Code:
#!/bin/bash

declare -i INDEX
declare -i RESULTS
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 Scott; 04-04-2011 at 02:37 AM.. Reason: Code tags, please...
# 2  
Old 04-04-2011
Code:
if [[ $# != 2 ]]
then
   echo "ERROR - Number of arguments is not equal to 2"
   exit 1
fi

case $1 in
  *[^0-9]*) echo "ERROR - First argument is not integer"
  exit 1
  ;;
esac

case $2 in
  *[^0-9]*) echo "ERROR - Second argument is not integer"
  exit 1
  ;;
esac


if (( $1 > $2 ))
then
    for ((i=$1;i>=$2;i--))
    do
       echo $i
    done
elif (( $1 < $2 ))
then
    for ((i=$1;i<=$2;i++))
    do
       echo $i
    done
else
    echo $1
fi

# 3  
Old 04-04-2011
You just try to write the code for the exist command seq.
Code:
seq 1 3

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to check if the script is already running?

I have one shell script in NAS shared location. Since NAS is mounted in many servers, script can be run from any of those servers. I want to make sure if the script is already running, it should not allow others to run it. I googled it and got some idea that i can touch one empty file in the... (8 Replies)
Discussion started by: thomasraj87
8 Replies

2. Shell Programming and Scripting

Check to see if script is already running

Happy New Year Is there a quick way to check to see if a script is already running. I want to put in a check in the script to exit, if already running. Currerntly i can only think of doing it the following way. # ps -ef | grep -i 3_HOUSEKEEPING_FFTVTL_TO_FFTDSSU_DUPLICATION.ksh |... (5 Replies)
Discussion started by: Junes
5 Replies

3. UNIX for Dummies Questions & Answers

[Solved] How to Check if a script is running?

Hi All, I am new to Unix... Can you please let me know how we can check if a script is running or not on Solaris box? (4 Replies)
Discussion started by: Rahul466
4 Replies

4. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

5. Shell Programming and Scripting

bash script to check if a program is running

I'm a bit new to bash programming and I was assigned the job of writing a script that will check to see if a program server is running and to restart the program if it is not up. The script is supposed to check the program every hour (which I have looked up and I believe I know how to do) and send... (3 Replies)
Discussion started by: mcknz
3 Replies

6. Shell Programming and Scripting

Script to check running of process

Hi, Can anyone please tell me how to write a shell script to check whether a process if running or not.... if its still running then wait for sometime and if not then run the next query. Also, Under my one main script main.sh I have to run 2 scripts simutaneously which take some time to... (2 Replies)
Discussion started by: lovepujain
2 Replies

7. Shell Programming and Scripting

script to check if another script is running and if so, then sleep for sometime and check again

Hi, I am a unix newbie. I need to write a script to check wheteher another script is still running. If it is, then sleep for 30m and then check again if the script is running. If the script has stopped running then, I need to come out of the loop. I am using RHEL 5.2 (2 Replies)
Discussion started by: mathews
2 Replies

8. Shell Programming and Scripting

script to check if process is running

How do I make a shell script to see if a certain process is running. The process shows up on ps aux as /usr/sbin/daemon eg: if /usr/sbin/daemon else #nothin basically i want to run the process if it isnt running/ has been stopped. Thanks. (2 Replies)
Discussion started by: daydreamer
2 Replies

9. Shell Programming and Scripting

Running a check platform script

Hi, I want to run a check platform & application script under ksh (Soaris boxes). It runs some commands and it take some time. I want to customize it like that: - output is too big, hence I want some output of the commands to be redirect ed in an output file (or maybe two or three) - not... (4 Replies)
Discussion started by: heartwork
4 Replies

10. Shell Programming and Scripting

check that script is not running twice

using ps -ef | fgrep "ld_data" how do i write a script to check that it didn't already run Thanks (2 Replies)
Discussion started by: Link_02
2 Replies
Login or Register to Ask a Question