为不同的选项使用相同的getopts参数

时间:2018-03-30 14:55:25

标签: linux bash shell getopts

我创建了一个bash脚本,我需要有关getopts的帮助

每个选项都有参数:

drop procedure if exists p;
delimiter $$
CREATE PROCEDURE p()
BEGIN DECLARE v1 INT DEFAULT 1; 
DECLARE a INT DEFAULT 1; #autoIncrement 
DECLARE b INT DEFAULT 1; #counting upwards 
DECLARE c INT DEFAULT 1; #only 1 mixer 
DECLARE d INT DEFAULT 1; #product from 1-6

 WHILE v1 < 10 DO
    INSERT INTO `settingsoperationmixer`(`settingsOperationId`, `mixerId`, `productId`)  VALUES (b,c,d);
    IF d < 6 THEN 
        SET d = d + 1;
     ELSE 
        SET d = 1;
     end if;

    SET b = b + 1;
    SET v1 = v1 + 1;
END WHILE;
END $$
delimiter ;
drop table if exists `settingsoperationmixer`;
create table `settingsoperationmixer`
(`settingsOperationMixerId` int auto_increment primary key ,`settingsOperationId` int, `mixerId` int, `productId` int)
;
call p();
MariaDB [sandbox]> select * from `settingsoperationmixer`;
+--------------------------+---------------------+---------+-----------+
| settingsOperationMixerId | settingsOperationId | mixerId | productId |
+--------------------------+---------------------+---------+-----------+
|                        1 |                   1 |       1 |         1 |
|                        2 |                   2 |       1 |         2 |
|                        3 |                   3 |       1 |         3 |
|                        4 |                   4 |       1 |         4 |
|                        5 |                   5 |       1 |         5 |
|                        6 |                   6 |       1 |         6 |
|                        7 |                   7 |       1 |         1 |
|                        8 |                   8 |       1 |         2 |
|                        9 |                   9 |       1 |         3 |
+--------------------------+---------------------+---------+-----------+
9 rows in set (0.00 sec)

如果我想使用相同的参数,我需要为每个选项输入-option arg

while getopts ":a:b:c:" opt
do
case $opt in
  a)  echo "$OPTARG";;
  b)  echo "$OPTARG";;
  c)  echo "$OPTARG";;
  /?) echo "wrong option $OPTARG";;
esac
done

我希望能够像命令一样使用脚本,当我有相同的参数时,一起输入选项:

./script.sh -a hello -b hello -c hello

hello
hello
hello

不幸的是,bc现在是这个论点:

./script.sh -abc hello

有人可以帮我吗?

此致

0 个答案:

没有答案