我有这个问题。问题是当我使用星号(*)作为参数时,将它与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发布于 2020-03-07 08:08:47
#!/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!"https://askubuntu.com/questions/1215459
复制相似问题