简体   繁体   English

使用参数运行Linux Shell脚本

[英]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. 我猜您从用户那里看到的参数是xy值。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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