Passing two variables to function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing two variables to function
# 1  
Old 01-27-2011
Passing two variables to function

HI ,I am a new in Bash and ,I dont know how to pass a second parameter to this fuction,if the name of the passed argument is num works fine,but if I try to pass secondNum,dont recognized it
thanks
Code:
 
function check()
{
 if(($(echo ${#num}) == 0 ))
   then
    echo No arguments passed.Try again
 else
    rem=$(echo $num | tr -d [0-9])
    lenght=$(echo ${#rem})
      if(( $lenght > 0))
        then
       echo "$num is not a number"
      else
       echo "$num is a number"
       fi
 fi
}

# 2  
Old 01-27-2011
Code:
$ cat check.sh
#!/bin/bash

function check()
{
 echo "num_args = $#"

 for myarg in "$@"
 do
    echo "$myarg"
 done
}

check 1 2

exit

output:
Code:
$ ./check.sh  
num_args = 2
1
2



# 3  
Old 01-27-2011
Thanks for the respond.I try to do it this way,why is not working
Thanks
Code:
#!/bin/bash
function check()
{
 if(($(echo ${#num}) == 0 ))   
   then
    echo No arguments passed.Try again
 else
    rem=$(echo $num | tr -d [0-9])
    lenght=$(echo ${#rem})
      if(( $lenght > 0))
        then
       echo "$num is not a number"
      else
       echo "$num is a number"
       fi
 fi
}

echo -n "Enter: "
read fnum
let num=fnum  
check "$num"  
echo -n "Enter: "
read snum
let num=snum  
check "$num"

# 4  
Old 01-27-2011
Code:
#!/usr/bin/ksh or bash or dash or ...
####
check()
{  
   # function args are just like command line args - you call function as command
   # return in function works same way as exit in script
   [ $# -lt 1 ] && echo "No arguments passed.Try again" >&2 && return 1

   for num in $@
   do
       other=${num//[0-9]/} # remove numbers = like tr -d "[0-9]"

       [ "$other" != "" ] && echo "$num is not a number" >&2 && return 2
       echo "$num is number"
   done
}   

#####MAIN####
echo -n "Enter: "
read num1
check $num1
stat=$?
echo $stat

echo "________________"
echo -n "Enter: "
read num2
check $num2
stat=$?
echo $stat
echo "_________________"
check $num1 $num2


Last edited by kshji; 01-27-2011 at 02:52 PM..
# 5  
Old 01-27-2011
Thank you very much ,but I didnt understand the sintacsis ,could you show me what to replace in my file,because mine is working fine with one variable passed
Thanks
# 6  
Old 01-28-2011
Use args, not global variable, if you are writing function with args. I and jsmithstl have given examples how to use arguments in function case. Same way as for script.
# 7  
Old 01-28-2011
in bash $1 $2 $3 ...... $N where N is any number (once you hit 10+ enclose them ex: ${10}) refers to the arguments passed to the function. So...
Code:
myFunction arg1 arg2 arg3

can be read with
Code:
echo "$1" "$2" "$3"

Respectively...$# gives you the amount of arguments (useful if you want to make sure the correct amount of args are given before continuing.) I hope I helped. I'm not that good at explaining things...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

Passing variables to a sed function

Hi everyone, I've re-written some of our scripts to use more functions and I've run into a situation where passing a variable to a sed function does not work. My function is a one-liner sed command as follows: function StringSub() { sed -i "${1}/${2}/${3}/${4}" ${5} } Where ${1} through... (4 Replies)
Discussion started by: richardsantink
4 Replies

4. Shell Programming and Scripting

Passing Variables

I have below data: DAY1=10202013 I am trying below but not getting the desired output: COUNT=1 DATE=DAY$COUNT echo "Date is $DATE" However output I am getting is: Date is DAY1 I wanted the output as: Date is 10202013 I tried following as well: DAY1=10202013 COUNT=1... (3 Replies)
Discussion started by: rockyr1985
3 Replies

5. Shell Programming and Scripting

Passing the parameters using a function

Hi All, I am new to shell scripting required some help in passing the parameter value to the shell script. I am writing a shell script, in the script I have created two functions as below. first function get_trend_ids () { Here I am connecting to the database and getting all the... (3 Replies)
Discussion started by: shruthidwh
3 Replies

6. Shell Programming and Scripting

Passing 2 variables

Hi All, I need to pass 2 variables name 'vamskt' and 'vamsi'. Here is my question: delete from gpi.usergroup where usg_user_id in ('vamskt'); delete from gpi.userroles where uro_user_id in ('vamskt'); delete from gpi.user where usr_id in ('vamskt'); insert into gpi.user... (3 Replies)
Discussion started by: tvamsikiran
3 Replies

7. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

8. Shell Programming and Scripting

passing variables to sed function in a script ....

Hello , I have a script named testscript.sh wherein I have two variables $var and $final (both of which contain a number) I have a sed write function inside this script as follows: sed '1,2 w somefile.txt' fromfile.txt Now , in the above i want to pass $var and $final instead of... (2 Replies)
Discussion started by: shweta_d
2 Replies

9. Shell Programming and Scripting

passing variables

Hi, Is there any way to pass variable to a sed script.For awk we have -v option.like that do we have any way to pass variable to a sed script from a awk script or from normal script? Thanx, sounder (1 Reply)
Discussion started by: sounder123
1 Replies

10. UNIX for Dummies Questions & Answers

Passing Argument to Function

May i know how to pass an argument to a function in a shell script? Sorry, i din stated that it is in a shell script in my previous post. Means: checkStatus() { ........... } read status; I wanna use the status in the function checkstatus, how... (2 Replies)
Discussion started by: AkumaTay
2 Replies
Login or Register to Ask a Question