function parameter in unix


 
Thread Tools Search this Thread
Operating Systems Solaris function parameter in unix
# 1  
Old 09-27-2010
function parameter in unix

Hi,

How to use a function with passing value as a parameter in unix ?

With Regards
# 2  
Old 09-27-2010
In shell script they use the positional parameters "$1", etc. For eg:

Code:
#!/bin/sh

try() {
    echo $1
}

try foo

Will echo "foo", because that is the first paramter passed into the function try.
# 3  
Old 09-28-2010
Hi,

Here, what i want to do is,

First I am asking user to input the value i.e. minute

after user inputs the value, this value should be checked whether supplied value is correct or not.

i.e. the minute value inserted by the user should be between 0 to 59 .

for to check this, I want to call the function and want to pass this user supplied value to the calling function.

With Regards
# 4  
Old 09-28-2010
Code:
#!/bin/sh

try() {
    echo minutes are $1
}

min=-1
while [ "$min" -lt 0 -o "$min" -gt 60 ]
do
  echo "Please enter minutes"
  read min
done
try $min

# 5  
Old 09-28-2010
Code:
#!/bin/sh

get_minutes ()
{
    while true
    do
        printf "enter minutes: "
        read MIN

        case "$MIN" in
            [0-9]|[0-5][0-9]) break 2;;
            *) echo Invalid minutes, please try again.;;
        esac
    done
}

get_minutes
echo minutes = $MIN



---------- Post updated at 08:42 ---------- Previous update was at 08:40 ----------

citaylor, your approach does not check for non-numeric input:
Code:
$ ./x
Please enter minutes
abc
minutes are abc

# 6  
Old 09-28-2010
Hi,

Below is the script, but on executing getting the below error :

check_minutes ()
{
while [ "$minute" -lt "0" ] || [ "$minute" -gt "59" ]
do
echo "Invalid minutes,please try again"
done
}

printf "$FBOLD\nPlease enter the minutes (0-59): $FREG"
read minute

if [ "$minute" = "" ] then

echo "$minute" = "*"

else

if [ "$minute" -ne "*" ] then

check_minutes
fi
fi

echo minutes = $MIN

Error :

./install_cron_export_gps.sh: line 89: syntax error near unexpected token `else'
./install_cron_export_gps.sh: line 89: ` else '

With Regards
# 7  
Old 09-28-2010
then after if has to be on a new line or separated by a semicolon, like:
Code:
if [ "$minute" = "" ] ; then

or
Code:
if [ "$minute" = "" ]
then

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass function parameter to do loop?

Hi All, I have created one function for KSH and was running well with one parameter input since I just had to use $1 to get the parameter. Now I want to do loop for each parameter(actually filenames) . I have try to use do loop, but $i does not resolve to parameter instead it resolves to 1,... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

Passing command as a function parameter

Hi All, Just trying to implement the below shell script using AIX ksh shell. myfunc { eval "$*" } CMD='ls -la /etc/hosts | awk '{print $9"|"$5}'' myfunc $CMD Keeping getting "|}: not found" errors, any pointers would greatly be appreciated. Kind Regards Ed Please... (2 Replies)
Discussion started by: eo29
2 Replies

3. Shell Programming and Scripting

Passing sql as parameter to unix function

Hi, I have a function which connects to the db and runs the sql. it works fine when I run it like: function "select empname from emp;" but when I try to pass the sql string to a variable which in turn in fed to the function , it throws error. please advise. Thanks, Arnie. (1 Reply)
Discussion started by: itsarnie
1 Replies

4. UNIX for Dummies Questions & Answers

function parameter issue

hi, i have a function say "mask". i'm passing parameters to the function like file_name \$1 |. inside the function i'm naming the second parameter as position (i.e value of position will be "$1"). i'm passing the position's value like " $position=a" But it is nor picking the value "$1".... (5 Replies)
Discussion started by: amar1003
5 Replies

5. Shell Programming and Scripting

How to use parameter with perl function?

am a beginer of shell Unix, plesase tell me how to get parameter with $perl in loop $ perl -lne '$/="DOCEND";print $_."DOCEND" if /$ACC/' file_input > output i can't get parameter in loop with perl fucntion like this pass paramerter to $ACC not accept in this script $ACC = paramerter to... (4 Replies)
Discussion started by: krai
4 Replies

6. Shell Programming and Scripting

pass parameter to function

HI all I have a code like ############################################## minyear() { curryear=$1 echo $curryear } ##Main Program ## minyear exit ####### when i execute "sh scriptname 2005" output should be like 2005 but the output is blank. I guess i need to pass parameter to... (3 Replies)
Discussion started by: vasuarjula
3 Replies

7. Shell Programming and Scripting

KSH list as function parameter

Hello, Could anyone help me with some KSH syntax? I'm trying to pass a list as a function parameter in a KSH? For example I have code like this: print_counter() { N=$1 C=$2 for A in $C; do echo "This is $N number $A" done } NAME=BRICK COUNT=" 1 2 3 4" ... (2 Replies)
Discussion started by: pn8830
2 Replies

8. Shell Programming and Scripting

function parameter

Hi, i have this code : awk -v s=string_to_find 'BEGIN{ n="" } { if ($0 ~ /FEATURE TESTED /) { n=$0 } if (index($0,s)!=0) { if (n!="") print n n=="" } }' $file1 > $file2 (4 Replies)
Discussion started by: kamel.seg
4 Replies

9. UNIX for Advanced & Expert Users

Parameter passing in a function

I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in. How can I make the function receive the whole string (and not terminate at the first space it encounters)?. part of... (2 Replies)
Discussion started by: fastgoon
2 Replies

10. Shell Programming and Scripting

Passing a string parameter to a function

I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in. How can I make the function receive the whole string (and not terminate at the first space it encounters)?. part of... (1 Reply)
Discussion started by: fastgoon
1 Replies
Login or Register to Ask a Question