首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >shell脚本中的星号问题

shell脚本中的星号问题
EN

Ask Ubuntu用户
提问于 2020-03-07 05:36:09
回答 1查看 710关注 0票数 1

我有这个问题。问题是当我使用星号(*)作为参数时,将它与if语句上的正则表达式进行比较。正则表达式可以正常工作,直到使用星号为止。您知道,当作为参数使用时,它会在文件系统中扩展--我尝试过命令shopt -s nullglob,但似乎不起作用。这是我的剧本的内容

代码语言:javascript
复制
#!/bin/bash
#an array of regex expresion to match basic crontab time values
declare -a regex_array=('^(\*|([0-9]|[0-5][0-9]))
                        '^(\*|([0-9]|[0-1][0-9]|2[0-3]))
                        '^(\*|([1-9]|0[1-9]|[1-2][0-9]|3[0-1]))
                        '^(\*|([1-9]|0[1-9]|1[0-2]))
                        '^(\*|[0-6]))
#another array for output errors
declare -a value_cront=('minutes'
                        'hours'
                        'days'
                        'month'
                        'day of the mont')
#initializing the counter
count_i=0
#a loop that will exit until a false condition (exit codes)
while :
do
  #this loop will check the 5 time parameters with a regex expresion
  for count_param in "$@"
  do
    if [[ "$count_param" =~ ${regex_array["$count_i"]} ]]; then
      exit 0
    else
      echo "The parameter ${value_cront["$count_i"]} does not match"
      exit 1
    fi
    ((count_i++))
  done
done
EN

回答 1

Ask Ubuntu用户

发布于 2020-03-07 08:08:47

代码语言:javascript
复制
#!/bin/bash

set -o noglob
set -- $@

# Regex to match basic crontab time/date values.
a=('^(\*|([0-9]|[0-5][0-9])) \
   '^(\*|([0-9]|[0-1][0-9]|2[0-3])) \
   '^(\*|([1-9]|0[1-9]|[1-2][0-9]|3[0-1])) \
   '^(\*|([1-9]|0[1-9]|1[0-2])) '^(\*|[0-6]))

# Time/date field names.
b=('minute' 'hour' 'day of the month' 'month' 'day of the week')

if [[ $# -ne 5 ]]; then
    echo "Invalid number of time/date fields."
    exit 1
fi

for c in $@; do
    if [[ ! $c =~ ${a[$d]} ]]; then
        echo "The time/date field '${b[$d]}' does not match."
        exit 1
    fi
    ((d++))
done

echo "Cron ok!"
票数 1
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1215459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档