Sponsored Content
Full Discussion: Bash script error problem
Top Forums Shell Programming and Scripting Bash script error problem Post 302455234 by methyl on Tuesday 21st of September 2010 06:30:52 AM
Old 09-21-2010
You may consider restructuring the script to separate the parameter validation from the processing such that the script has a beginning a middle and an end.
The actual error is caused by trying to use $num which has no value. We can prevent this by checking the value of $# first. Because there is more than one reason to reject the command line we can prepare the "usage" string in advance. In the original script the "|grep usage" has no use .
Just as an aside, your shebang line contains a lot of trailing blanks and an extra blank between the hash and /bin/bash.

Code:
#!/bin/bash
argcount=$#     # Number of arguments
num="$1"        # Value of parameter 1
usage='usage: ./area <query> ...'       # Usage string

######################
# Parameter validation
######################
if [ ${argcount} -ne 1 ]
then
        echo "${usage}"
        exit
fi
#
if [[ $num -gt 99 && $num -lt 1000 ]]
then
    :
else
    echo "${usage}"
    exit
fi

########################
# Processing starts here
########################
echo Area Codes for '"'$num'"':
grep "^$num" ~cs155/pub/area-codes | sed 's/^/   /'

########
# End
########
echo "good night"

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

bash script problem

hi I am writing a bash script that uses dialog to get user input an diplay messages to user. I have a small problem dialog --inputbox "blabla" 20 50 2> /tmp/output VAR="'cat /tmp/output'" mkdir $VAR the code below requests user for a directory path to be created. But, if the user uses... (1 Reply)
Discussion started by: fnoyan
1 Replies

2. UNIX for Dummies Questions & Answers

Bash script argument problem

I'm having problems with bash scripts. If a bash script is called with no arguments, I always get "PHIST=!" as the first argument (i.e. this is what $1 equals). Why? Where does this come from, and how can I fix it? Nothing in the bash man pages refer to this mysterious default argument. (2 Replies)
Discussion started by: sszd
2 Replies

3. Linux

Problem with crontab + bash script

Crontab: 16 14 * * * /root/bin/./empty_mail.sh >> /root/bin/log.txt L=`mailq |grep -c frozen` echo "${NUMMAIL} frozen mail to be removed from the mailq" #echo "Press Enter to continue" #read CONTINUE echo -en "-> " for i in `mailq | grep frozen | awk '{print $3}'` ; do exim4 -Mrm $i... (1 Reply)
Discussion started by: wessberg
1 Replies

4. Shell Programming and Scripting

Problem in bash script

I have written a script and I get error and I don't understand why. neededParameters=2 numOfParameters=0 correctNum=0 while getopts "s:l:" opt do case "$opt" in s) serviceName= $OPTARG #errorline 1 numOfParameters= $numOfParameters + 1 ;; l) ... (12 Replies)
Discussion started by: programAngel
12 Replies

5. Shell Programming and Scripting

Simple bash script problem

#!/bin/bash cd /media/disk-2 Running ./run.sh it's not changing directory.Why? (6 Replies)
Discussion started by: cola
6 Replies

6. Shell Programming and Scripting

help with a bash script problem

hi to everyone :) i am new to linux and bash and i am trying to build a bash script, that is quite similar to the well known cmd 'split' ... ;) it is now already "working" ... i can use it like: ./splitfix.sh -v -s 10 foo ./splitfix.sh -s 10 -v foo ./splitfix.sh -s 10 foo ./splitfix.sh -v... (5 Replies)
Discussion started by: drjodo
5 Replies

7. Shell Programming and Scripting

Problem using grep in bash script

When it comes to programing and UNIX, I know just enough to be really really dangerous. I have written a python script to parse through a file that contains ~1 million lines. Depending on whether a certain string is matched, the line is copied into a particular file. For the sake of brevity,... (4 Replies)
Discussion started by: errcricket
4 Replies

8. Shell Programming and Scripting

Problem with Bash Script.

Hi guys! I'm new to the forum and to the Bash coding scene. I have the following code paths=/test/a paths=/test/b keywords=\"*car*\" keywords=\"*food*\" for file in `find paths -type f -ctime -1 -name keywords -print 2>/dev/null` do #.... do stuff here for every $file found... (5 Replies)
Discussion started by: RedSpyder
5 Replies

9. Shell Programming and Scripting

Help with bash script problem

Hi, Below is my bash script: cat run_all.sh if && ; then Number_Count_Program $1.results $2.results > $1.$2.counts else Number_Split_Program $1.results $2.results > $1.$2.split fi After I run the following command: ./run_all.sh A B ./run_all.sh: line 1: Anybody advice to edit... (5 Replies)
Discussion started by: perl_beginner
5 Replies

10. UNIX for Beginners Questions & Answers

Array problem in Bash Script

I am trying to write a Bash Script using a couple of arrays. I need to perform a countdown of sorts on an array done once daily, but each day would start with the numbers from the previous day. This is what I'm starting with : #!/bin/bash days=(9 8 7 6 5) for (( i = 0 ; i < ${#days} ; i++... (4 Replies)
Discussion started by: cogiz
4 Replies
TCRMTTEST(1)							   Tokyo Tyrant 						      TCRMTTEST(1)

NAME
tcrmttest - test cases of the remote database API DESCRIPTION
The command `tcrmttest' is a utility for facility test under multi-thread situation. This command is used in the following format. `host' specifies the host name of the server. `rnum' specifies the number of iterations. tcrmttest write [-port num] [-tnum num] [-nr] [-rnd] [-ext name] host rnum Store records with keys of 8 bytes. They change as `00000001', `00000002'... tcrmttest read [-port num] [-tnum num] [-mul num] host Retrieve all records of the database above. tcrmttest remove [-port num] [-tnum num] host Remove all records of the database above. Options feature the following. -port num : specify the port number. -tnum num : specify the number of running threads. -nr : use the function `tcrdbputnr' instead of `tcrdbput'. -rnd : select keys at random. -ext name : call a script language extension function. -mul num : specify the number of records for the mget command. If the port number is not more than 0, UNIX domain socket is used and the path of the socket file is specified by the host parameter. This command returns 0 on success, another on failure. SEE ALSO
ttserver(1), ttultest(1), ttulmgr(1), tcrtest(1), tcrmgr(1), ttutil(3), tcrdb(3) Man Page 2010-01-20 TCRMTTEST(1)
All times are GMT -4. The time now is 11:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy