2

I am trying to pass arg to this scripts but it does not work anyone knows how fix this ? and I do not want to use args i want to use getopts

For This part

# Get command line parameters
i=
a=
y=
while getopts iay: name
        case $name in
                i) i="$OPTARG" ;;
                a) a="$OPTARG" ;;
                y) y="$OPTARG" ;;
        ?) exit;;
        esac
done

This is the scripts

if [ $# -ne 3 ]
        then
                echo -e "\nUsage: mortgage rate amount period\n"
                exit 1
        fi
# Get command line parameters
i=
a=
y=
while getopts iay: name
        case $name in
                i) i="$OPTARG" ;;
                a) a="$OPTARG" ;;
                y) y="$OPTARG" ;;
        ?) exit;;
        esac
done

# Compute the monthly payment
x=$(echo "scale=20;1+$i/200" | bc)
z=$(echo "scale=20;1/6" | bc)
x2z=$(echo "scale=20;e($z*l($x))" | bc -l)
y12=$(echo "scale=20;-$y*12" | bc)
x2z2y12=$(echo "scale=20;e($y12*l($x2z))" | bc -l)
p=$(echo "scale=3;$a*(($x2z-1))/(1-$x2z2y12)" | bc)

# Print the monthly payment
echo $p
exit 0

========================== This is original scripts:

if [ $# -ne 3 ] then echo -e "\nUsage: mortgage rate amount period\n" exit 1 fi

Get command line parameters

i=$1; a=$2; y=$3

Compute the monthly payment

x=$(echo "scale=20;1+$i/200" | bc) z=$(echo "scale=20;1/6" | bc) x2z=$(echo "scale=20;e($z*l($x))" | bc -l) y12=$(echo "scale=20;-$y*12" | bc) x2z2y12=$(echo "scale=20;e($y12*l($x2z))" | bc -l) p=$(echo "scale=3;$a*(($x2z-1))/(1-$x2z2y12)" | bc)

Print the monthly payment

echo $p exit 0
cas
  • 78,579

3 Answers3

2

Arguments of a shell script are just $1, $2, $3 and so on. For example, put echo $2; echo $1 in a script.sh file, make it executable, and run it as ./script.sh one two.

In your script;

i=$1

a=$2

y=$3

and run it as ./myscript.sh 3 4 23. Now i will be 3, a will be 4 and y will be 23.

Quora Feans
  • 3,866
  • it was already like that I am trying to change it

    it was like this :::::::::

    if [ $# -ne 3 ] then echo -e "\nUsage: mortgage rate amount period\n" exit 1 fi

    Get command line parameters

    i=$1; a=$2; y=$3

    Compute the monthly payment

    x=$(echo "scale=20;1+$i/200" | bc) z=$(echo "scale=20;1/6" | bc) x2z=$(echo "scale=20;e($zl($x))" | bc -l) y12=$(echo "scale=20;-$y12" | bc) x2z2y12=$(echo "scale=20;e($y12l($x2z))" | bc -l) p=$(echo "scale=3;$a(($x2z-1))/(1-$x2z2y12)" | bc)

    Print the monthly payment

    echo $p exit 0

    – Siavosh Bahman Oct 16 '15 at 00:53
2

You've only told getopts that -y has an argument. You're also missing the "do" from the "while" command.

You also don't need to check that the number of args is 3 any more - in fact, there won't be 3 args, there will be 6. But arg counting is the wrong method to use with getopts. Instead, check that your required variables all have a value.

Try

#! /bin/sh

usage() {
    echo "Usage:"
    echo "       $0 -i rate -a amount -y period"
    exit 1
} 

i='' ; a='' ; y=''

while getopts i:a:y: name ; do
    case $name in
            i) i="$OPTARG" ;;
            a) a="$OPTARG" ;;
            y) y="$OPTARG" ;;
            *) usage ;;
    esac
done
shift $(( OPTIND - 1 ))

if [ -z "$i" ] || [ -z "$a" ] || [ -z "$y" ] ; then 
    usage
fi

# Compute the monthly payment
x=$(echo "scale=20;1+$i/200" | bc)
z=$(echo "scale=20;1/6" | bc)
x2z=$(echo "scale=20;e($z*l($x))" | bc -l)
y12=$(echo "scale=20;-$y*12" | bc)
x2z2y12=$(echo "scale=20;e($y12*l($x2z))" | bc -l)
p=$(echo "scale=3;$a*(($x2z-1))/(1-$x2z2y12)" | bc)

# Print the monthly payment
echo $p
exit 0
cas
  • 78,579
  • I did change it to this

    i= a= y= while getopts i:a:y: name ; do case $name in i) i="$OPTARG" ;; a) a="$OPTARG" ;; y) y="$OPTARG" ;; ?) exit;; esac done shift $(( OPTIND - 1))

    But did not work

    – Siavosh Bahman Oct 16 '15 at 00:44
  • See also http://unix.stackexchange.com/questions/20975/how-do-i-handle-switches-in-a-shell-script – cas Oct 16 '15 at 00:44
  • what, exactly, do you mean by "did not work"? FYI you can use set -x in a sh script to help debugging – cas Oct 16 '15 at 00:47
  • when i change it the scripts does not work it means it did not pass value to the options – Siavosh Bahman Oct 16 '15 at 00:52
  • still not working – Siavosh Bahman Oct 16 '15 at 01:14
  • "not working" is probably the most useless error report ever. provide details. in particular, provide details on a) what is going wrong and b) what you are doing that differs from what i wrote above. – cas Oct 16 '15 at 01:17
  • 1
    BTW, the script in my answer is tested and works. At least, the option parsing works - i have no idea if your calculations are correct because i didn't even look at those. but as far as parsing the options and setting the variables - that works exactly as intended. – cas Oct 16 '15 at 01:19
1

Finally I Find answer Thinks to everyone who helped

this is code:

# Use “getopts” to give options to the script
while getopts "i:a:y:" option; do
case $option in
i)
i=$OPTARG
;;
a)
a=$OPTARG
;;
y)
y=$OPTARG
;;
\?)
echo -e "\nUsage: mortgage rate amount period\n"
exit 1
;;
esac
done

# Compute the monthly payment
x=$(echo "scale=20;1+$i/200" | bc)
z=$(echo "scale=20;1/6" | bc)
x2z=$(echo "scale=20;e($z*l($x))" | bc -l)
y12=$(echo "scale=20;-$y*12" | bc)
x2z2y12=$(echo "scale=20;e($y12*l($x2z))" | bc -l)
p=$(echo "scale=3;$a*(($x2z-1))/(1-$x2z2y12)" | bc)

# Print the monthly payment
Echo “Your Monthly is $p”
exit 0
cas
  • 78,579