shell脚本的Asterisk问题

shell脚本的Asterisk问题

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

#!/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

答案1

#!/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!"

相关内容