![在 bash 脚本中创建一个日期数组并将其与当前日期匹配](https://linux22.com/image/678105/%E5%9C%A8%20bash%20%E8%84%9A%E6%9C%AC%E4%B8%AD%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%97%A5%E6%9C%9F%E6%95%B0%E7%BB%84%E5%B9%B6%E5%B0%86%E5%85%B6%E4%B8%8E%E5%BD%93%E5%89%8D%E6%97%A5%E6%9C%9F%E5%8C%B9%E9%85%8D.png)
实际上,我想创建一个日期数组并将其与今天的日期进行比较,如果它与当前日期匹配,则执行 test.sh 文件,否则退出 bash 脚本中的循环。我确实喜欢这个...
**#!/bin/bash**
cd /home/user1
current_date=$(date +%Y-%m-%d)
array=['2016-03-02','2016-03-010','2016-05-10']
for i in "${array[@]}" do
if [ $now -eq $i ]; then
echo "executing your bash script file"
./myscript.sh
fi
done
当我执行上述脚本时,它会出现类似./sample.sh: line 6: [: 2016-03-02: integer expression expected
答案1
#!/bin/bash
cd /home/user1
current_date=$(date +%Y-%m-%d)
array=( 2016-03-02 2016-03-010 2016-05-10 )
for i in "${array[@]}"; do
if [ "$current_date" == "$i" ];then
echo "executing your bash script file";
./myscript.sh
fi
done