Understanding Functions, sh, and arguments

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Understanding Functions, sh, and arguments
# 1  
Old 11-16-2010
Understanding Functions, sh, and arguments

1. The problem statement, all variables and given/known data:
So I have to write a Bourne script that will add two numbers, taking only two arguments and informs the user when it is used incorrectly.

2. Relevant commands, code, scripts, algorithms:

It has to be done in sh. I think overall, it is a pretty basic script.

3. The attempts at a solution (include all code and scripts):

#!/bin/sh

addnumbers()
{ if [$3 = ""]; then
sum = 'expr $1 + $2'
echo $sum
else
echo "Please only enter two arguments."
fi
}

done

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

University of Notre Dame, IN, US, Freeland, CSE30189 Basic Unix for Engineers


What I am having trouble with is testing it out. From what I understand, I should have the file in my directory, chmod to like 755 or something, then just type in the name of the file. However, I was wondering how I could pass arguments into it to actually run it through the function. Also, I was unsure as to how I could make sure it was only two values, so I just made it so that if $3 was anything other than blank, it would just exit it.

Thanks for any advice you guys can give!
# 2  
Old 11-16-2010
Code:
#!/bin/sh

addnumbers()
{ if [ $# -eq 2 ]; then          
#in this line space after "[" and before "]" are important! $# stand for the number of arguments
sum=`expr $1 + $2`            
#in this line Do not use space befor and after "=" and use back quote instead of single quote
#not that inside the function, $1 and $2 stand for the parameter of your function !!!
echo $sum
else
echo "Please only enter two arguments."
fi
}

# if you define a function and then never call it, then your script will never give you an output so in the following line, we are calling the function
addnumbers $1 $2           
#in this line, $1 and $2 stand for the parameter of your script !!! 

Code:
$ cat myfile
#!/bin/sh

addnumbers()
{ if [ $# -eq 2 ]; then
sum=`expr $1 + $2`
echo $sum
else
echo "Please only enter two arguments."
fi
}
addnumbers $1 $2
$ sh myfile 4 5
9
$

if you run it $ ./myfile 3 4, you will need it to have the execution right

but if you run it $ sh myfile 3 4 , then the read permission are sufficient since sh has the execute permissions to run it.

Last edited by ctsgnb; 11-16-2010 at 12:17 PM..
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 11-16-2010
Thanks, that really helped. I had to modify it to add $3 to make it have the "else" part, but then it worked! Also, didn't know that you shouldn't have 'done' at the end too, but I just took that out.

Thanks for your help, I really appreciate it.

Quick question though: what does the -eq in the

{ if [ $# -eq 2 ]; then

mean? I know that we want to see if there are any more than 2 arguments. What does -eq do for that?

Last edited by lee.n.doan; 11-16-2010 at 12:31 PM.. Reason: wanted to add a question
# 4  
Old 11-16-2010
-eq equal
-ne not equal
-gt greater than
-lt less than
done is used to end a loop :

Code:
while [ condition ... ]
do
cmd1
cmd2
...
done

Code:
for i in $LIST
do
cmd1
cmd2
...
done

This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Understanding C++ template partial specialization with pointer datatype arguments

When I compile the below code, I am getting error as template<typename T> T AddFun(T i, T j) { return i + j; } template<> T* AddFun<T*>(T* i, T* j) { return new T(*i + *j); } int main() { int n = AddFun<int>(10, 20); int i = 10, j = 20; int* p = AddFun<int*>(&i,... (1 Reply)
Discussion started by: royalibrahim
1 Replies

2. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

3. Programming

Functions

Hi All, Can any one help me. I am calling in a function2 with string as parameter from function1, the function1 gives 3 values. how i get the 3 values from funciton2 to function1. i have to give a return or something. thanks in advance. (2 Replies)
Discussion started by: uday.sena.m
2 Replies

4. Homework & Coursework Questions

trouble understanding file option and command line arguments

Hi, I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves... (1 Reply)
Discussion started by: heywoodfloyd
1 Replies

5. UNIX for Dummies Questions & Answers

== vs -eq and functions

Hey I have a question.... what is the difference between using == vs -eq when testing in WHILE loops. I use the following test that only worked with == signs.... if why do i need == and not -eq? 2. I need to re-use some code in a couple places in this script. is functions my best... (5 Replies)
Discussion started by: danieldcc
5 Replies

6. Shell Programming and Scripting

grep with two arguments to arguments to surch for

Hello, is it possible to give grep two documents to surche for? like grep "test" /home/one.txt AND /home/two.txt ? thanks (1 Reply)
Discussion started by: Cybertron
1 Replies

7. Shell Programming and Scripting

bash functions arguments

This script is called fuu; #!/bin/bash speak() { case $1 in 1)echo one ;; 2)echo two ;; 3)echo three ;; esac } speak exit 0 when i run fuu 2 i expect "two" like... (2 Replies)
Discussion started by: Tártaro
2 Replies

8. Shell Programming and Scripting

Need a little help with functions

I'm semi new to unix/linux and am trying to convert a program I wrote in C++ to a bash script. It's a program that prints Fibonacci's series. I have found scripts that will do it, but I'm trying persistently to get this one to work. The problem occurs when I try to return a value from the function.... (3 Replies)
Discussion started by: Glowworm
3 Replies

9. Shell Programming and Scripting

Use of functions

Hi my shell is tcsh can I have functions in my shell scripting? Is the below shell script correct. Can I have two functions and call one of them as required. ---------- echo "functions" f1 f1 () { echo "hello" } f2 () (1 Reply)
Discussion started by: amitrajvarma
1 Replies

10. Shell Programming and Scripting

Regarding functions

Hi, I have a function or script like this. show() { echo "Hi" } | tee -a log show This creates a logfile and prints Hi in it. Now when I try to do the same for sql like this: show() { sqlplus -s scott/tiger<<! select * from details; ! } | tee -a log show Then it gives me a... (2 Replies)
Discussion started by: sendhilmani
2 Replies
Login or Register to Ask a Question