help - scripting command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help - scripting command
# 1  
Old 11-28-2011
help - scripting command

hello

I'n new to the unix environment Smilie
I'm trying to script a command that takes 2+ integers and adds them together in bash.

eg

$ add 10 12 13
35

probably kind of basic but i also have to verify that they are indeed integers and that the "add" command has at least 2 parameters.

any ideas?
# 2  
Old 11-28-2011
Code:
#! /bin/bash

if [ $# -lt 2 ]
then
    echo "Less than 2 paramaters"
    exit
fi

# The below if condition checks if the number contains a decimal point or not.
# Actually in the for-loop below expr used will not work if the number is not an integer. If you try doing "expr 1.1 + 2.2", it'll say something like this: "expr: non-numeric argument"

echo $* | grep -qe "\."
if [ $? -eq 0 ]
then
    echo "One of the input in not an integer"
    exit
fi

sum=0
for x in $@
do
    sum=`expr $sum + $x`
done

echo "The sum is $sum"

# 3  
Old 11-28-2011
awesome i will try this out!

thanks!
# 4  
Old 11-28-2011
Quote:
Originally Posted by balajesuri
Code:
# The below if condition checks if the number contains a decimal point or not.
# Actually in the for-loop below expr used will not work if the number is not an integer. If you try doing "expr 1.1 + 2.2", it'll say something like this: "expr: non-numeric argument"

echo $* | grep -qe "\."
if [ $? -eq 0 ]
then
    echo "One of the input in not an integer"
    exit
fi

sum=0
for x in $@
do
    sum=`expr $sum + $x`
done

echo "The sum is $sum"


There's no need for external commands (grep and expr).

Use a function to determine whether an argument is an integer:
Code:
is_int()
{
  case $1 in
    *[!0-9]*|"") return 1 ;;
  esac
}

Then check each arg before using it:
Code:
total=0
for int
do
  is_int "$int" || exit 1
  total=$(( $total + $int ))
done
printf "Total: %s\n" "$total"

# 5  
Old 11-29-2011
thanks
# 6  
Old 11-29-2011
is there a way after the "then" to just continue in the script without displaying the error message?

Code:
if [ $? -eq 0 ]
then
    echo "One of the input in not an integer"
    exit
fi

# 7  
Old 11-29-2011
If you just want to continue in the script, why would you want a check to see if the number is an integer or not. Remove the whole if-block.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Escape and command substitution in scripting

I have one question in shell script for escape "\" with command substitution "` `". I post there to seek help to understand how it works. My original one piece of code in script like this: This piece of code in whole script is working without errors chk_mode=`sqlplus -s /nolog<<EOF connect /... (4 Replies)
Discussion started by: duke0001
4 Replies

2. Shell Programming and Scripting

Scripting with arguments for ping command

This is the script I already have but I have problems with two arguments the first argument -t , I want to count 200 by the last digit of the IP address for example when I run the script ./ping.sh -t 17, the output would be192.168.0.217 is upThe second arguments --up won't work. Could anybody... (1 Reply)
Discussion started by: Roggy
1 Replies

3. Shell Programming and Scripting

Which is the best way/command to do mathematics in UNIX scripting?

Hello all! I used to use expr for doing simple mathematics, but has a main advantage and a main disadvantage: The advantage is that it can take variables for numbers (e.g.{1}: echo "Give me first" read lol echo "Give other" read lil sum=`expr $lol + $lil` echo "The sum of $lol and $lil =... (5 Replies)
Discussion started by: hakermania
5 Replies

4. UNIX for Dummies Questions & Answers

Linux scripting problem using ps command

when I try to apply this principle to my script, is not working, it tells me something like 'command not found' what i wrote is this.. if ; then echo "message1"; else echo "message2"; fi I'm supposed to count number of processes of the user, and if they're five or more, then a message... (2 Replies)
Discussion started by: aric87
2 Replies

5. Shell Programming and Scripting

shell scripting best practices - trap command

I know there is a command called trap which can be used to capture the signals from a shell script and redirect the control to a required function (such as a cleanup). My question is - Those of you who have written lot of shell scripts - do you always write a set of trap commands to capture... (4 Replies)
Discussion started by: sagar_evc
4 Replies

6. UNIX for Dummies Questions & Answers

Trying a arithmatic on a command line vs scripting.

Hi Everyone! I'm trying to do simple math on a single command line instead of a script which I've already set up using let etc. I can not get the same output to display on a command line. Essentially I would like a=20, b=50, and c=a*b. When I tried: let "A=20, B=50"; let C=A*B; echo $C ... (2 Replies)
Discussion started by: CasperQuiet
2 Replies

7. Shell Programming and Scripting

Help required on scripting the rm -f command

When i execute rm -f $1 via a script file named rmf, it is not deleting all the files, say starting with "sec". i have execute rmf for many times to remove all the occurrences... $rmf sec* - this should delete all files starting with sec, but not. The rm -f sec* is working fine. kindly help... (3 Replies)
Discussion started by: frozensmilz
3 Replies

8. Shell Programming and Scripting

Scripting using the FIND command

I'm working on a 'find' command which uses the -exec option to tar a listing of files that it finds. Is there a single option available that would both 1) create the TAR file if it doesn't exist and/or 2) update the TAR file if it already exists? Currently when I want manually create a TAR... (3 Replies)
Discussion started by: buechler66
3 Replies

9. Shell Programming and Scripting

Need help with Scripting diff command

Here is where I am at so far..... ------------------------- #!/bin/bash echo "Enter the output file name" read output_file echo "Enter the Orginal file Name" read write_file d= ' diff $write_file $output_file ' if $d = 1 ;then echo "files are not identical" else echo "they... (2 Replies)
Discussion started by: tlfletcher05
2 Replies

10. UNIX for Dummies Questions & Answers

shell scripting my own diff command

Hi I would like to run the diff command and recieve a little different output. I am on a linux machine. I am pretty new to shell scripting. So far my idea has shaped up to this, unworking, script. I would like file1: and file2: instead of the usual > or < output you recieve, diff | sed -e ... (4 Replies)
Discussion started by: axcxe
4 Replies
Login or Register to Ask a Question