简体   繁体   中英

Run Linux shell script with arguments

Can somone help me with this: so i have this script

#!/bin/bash


echo -n "Enter a value for X:(999 to exit): "
read x

until [[ $x == 999 ]]
do

echo -n "Enter a value for Y: "
read y
echo "X="$x
echo "Y="$y
((a=y+x))
echo "X+Y="$a
((s=y-x))
echo "X-Y="$s
((m=y*x))
echo "X*Y="$m
((d=y/x))
echo "X/Y="$d
((m=y%x))
echo "X%Y="$m

echo -n "Enter a value for X:(999 to exit): "
read x
if [[ $x == 999 ]];
then
    exit 0
fi

done
exit 0

but i didnt know how to write the rest of it, the missing thing is: Use the two command line arguments when the script starts if the user supplied them, and then prompt for more numbers to continue in the loop.

Am guessing the arguments you are looking from the user are x and y values. The easiest way to check if user provided arguments is to use $# which gets you the number of arguments given by the user.

So use it like this:

if [ "$#" -eq 2 ];       #2 arguments provided by user
then
    x=$1
    ... 
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM