Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash脚本上使用regex识别选项参数_Regex_Bash_Shell_Parameters_Sh - Fatal编程技术网

在bash脚本上使用regex识别选项参数

在bash脚本上使用regex识别选项参数,regex,bash,shell,parameters,sh,Regex,Bash,Shell,Parameters,Sh,我对这个话题还比较陌生,如果这个问题不相关,我很抱歉。我彻底搜索了网站,但没有找到答案 我正在为一个大学项目制作这个shell脚本,我们使用rsync和crontab同步文件夹。我试图为用户提供定制rsync和crontab参数的可能性,如下所示: rsync接受-auvn。我试着在我的.sh文件中创建了folling regex: #!/bin/bash #(...) lots of previous code if [[ $1 =~ ^-a?u?v?n? ]]; then if

我对这个话题还比较陌生,如果这个问题不相关,我很抱歉。我彻底搜索了网站,但没有找到答案

我正在为一个大学项目制作这个shell脚本,我们使用rsync和crontab同步文件夹。我试图为用户提供定制rsync和crontab参数的可能性,如下所示:

rsync接受-auvn。我试着在我的.sh文件中创建了folling regex:

#!/bin/bash

#(...) lots of previous code

if [[ $1 =~ ^-a?u?v?n? ]]; then
    if [ $1 != "-" ]; then
但它接受诸如-x之类的参数。你可以看到,第二个if表明我不知道自己在做什么

crontab接受五个参数:

最小值,[0,59]或*如有; 小时,[0,23]或*如有; 星期一、[周一、周二、…、周日]或*如有; 月份、[1月、2月、…、12月]或*如有; 2017年及以后,或*如有; 我现在不担心crontab正则表达式,但我正在努力使rsync正则表达式工作。我下载了rsync源代码以了解它们是如何处理选项参数的,但大多数脚本都是用C编写的,它超出了本项目的范围。我也可以将用户的任何请求发送到rsync选项,然后看着它爆炸,但我正在尝试先对它进行一些处理

谢谢大家!

[[ $1 =~ ^-[auvn]+$ ]]
即,勾选减号,然后多次勾选任何auvn字母,直到结束。如何在命令行中进行测试:

$ [[ '-auvn' =~ ^-[auvn]+$ ]] && echo yes || echo no
yes
$ [[ '-a' =~ ^-[auvn]+$ ]] && echo yes || echo no
yes
$ [[ '-x' =~ ^-[auvn]+$ ]] && echo yes || echo no
no