
以下是我参考网上的教程写的部分代码,但是不能实现功能,请教一下问题出在哪?
#!/bin/bash cd /mnt/c/Data/program/library/auto_software/backup_program ARGS= `getopt -a -o aswl --long all,server,windows,local -- "$@"` if [ $? != 0 ]; then echo "Terminating..." exit 1 fi eval set -- "${ARGS}" while true do case "$1" in -a|--all) bash locsyc_r bash sbak_r bash wbak_r;; -s|--server) bash sbak_r;; -w|--windows) bash wbak_r;; -l|--local) bash locsyc_r;; \?) echo "no args" exit 1;; esac done 代码复制错了,应该是这个
#!/bin/bash ARGS= `getopt -a -o ah --long install,help -- "$@"` if [ $? != 0 ]; then echo "Terminating..." exit 1 fi eval set -- "${ARGS}" while true do case "$1" in -i|--install) ./install.sh;; -h|--help) ./help.sh;; \?) echo "no args" exit 1;; esac done 代码复制错了,应该是这个
#!/bin/bash ARGS= `getopt -a -o ah --long install,help -- "$@"` if [ $? != 0 ]; then echo "Terminating..." exit 1 fi eval set -- "${ARGS}" while true do case "$1" in -i|--install) ./install.sh;; -h|--help) ./help.sh;; \?) echo "no args" exit 1;; esac done 1 AirCrusher 2022-08-24 16:29:34 +08:00 getopt |
2 vtwoextb 2022-08-24 16:35:26 +08:00 getopts |
3 circle33 2022-08-24 16:43:08 +08:00 获取命令行参数再执行对应的逻辑 $1 可以获取第一个命令行参数,分支选择可以用 case xxx in xxx |
4 Moris OP @circle33 我写了一些代码如下。但是无法实现功能执行,我不是很懂 getopt #!/bin/bash ARGS= `getopt -a -o ah --long install,help -- "$@"` if [ $? != 0 ]; then echo "Terminating..." exit 1 fi eval set -- "${ARGS}" while true do case "$1" in -i|--install) ./install.sh;; -h|--help) ./help.sh;; \?) echo "no args" exit 1;; esac done |
6 Moris OP @AirCrusher 请问能具体说一下吗? |
11 Moris OP @circle33 我已经找到解决方案了,我参考了一下别人的案例,直接去掉 ARGS= `getopt -a -o ah --long install,help -- "$@"` if [ $? != 0 ]; then echo "Terminating..." exit 1 fi eval set -- "${ARGS}" 再更改一下 while 的条件就行 |
12 bearice 2022-08-24 17:30:22 +08:00 觉得 getopt 不好用的话 https://github.com/fumieval/clap4shell 可以试试这个 |
13 kaiger 2022-08-24 17:33:13 +08:00 |
14 Moris OP @kaiger 差不多是,我现在想要写的脚本就是参考之前的备份脚本,在上面修改。之前的备份脚本传入短参数-a -b 之类的是可以的,所以这次也想顺便修改一下备份的脚本。谢谢你的建议,我会看看的。不过,你这个终端时远程终端吗?叫啥名字?好酷啊! |
16 kaiger 2022-08-24 17:46:36 +08:00 |