function parameter in unix

Hi,

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

With Regards

In shell script they use the positional parameters "$1", etc. For eg:

#!/bin/sh

try() {
    echo $1
}

try foo

Will echo "foo", because that is the first paramter passed into the function try.

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

#!/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
#!/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:

$ ./x
Please enter minutes
abc
minutes are abc

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

then after if has to be on a new line or separated by a semicolon, like:

if [ "$minute" = "" ] ; then

or

if [ "$minute" = "" ]
then

Hi,

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

goes into infinite loop.

With Regards

---------- Post updated at 04:51 PM ---------- Previous update was at 04:37 PM ----------

Hi,

can u explain this ?

[0-9]|[0-5][0-9] in case structure ?

---------- Post updated at 06:40 PM ---------- Previous update was at 04:51 PM ----------

Hi,

As this is the script for scheduling cron job, so in the below case, it also should allow *

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

can u please explain this :
[0-9]|[0-5][0-9]
With Regards