function monitor(){ #set -x #if [ -e `/var/log/program_monitor/*.log` ];then if [ $(find /var/log/program_monitor/ -maxdepth 1 -name "*.log" | wc -l) -gt 0 ]; then echo "1" latest_log=$(ls -t /var/log/program_monitor/*.log | head -1) echo "2" date_str=$(echo $latest_log | cut -d'/' -f5 | grep -oE '[0-9]{8}') echo "3" echo "$date_str" latest_time=$(date +%Y%m%d) echo "4" echo "$latest_time" if [ "date_str" == "latest_time" ]; then echo "5" echo ---------$(date)---------- >> $log_file else touch ${log_file} fi else touch ${log_file} echo "6" fi }为啥不打印5明明获得的字符串都是一样的
时间: 2024-02-10 13:12:46 浏览: 175
根据代码分析,问题可能出在判断语句中。在判断语句中,字符串 "date_str" 和 "latest_time" 被引号包含,被视为文本字符串,而不是变量。所以无论实际的值是什么,它们都是不相等的文本字符串。正确的写法应该是去掉引号,将变量的值直接作为字符串比较,即:
```if [ "$date_str" == "$latest_time" ]; then```
相关问题
log_file="${log_dir}/$(date +%Y%m%d%H%M).log" function monitor(){ #set -x #if [ -e `/var/log/program_monitor/*.log` ];then if [ $(find /var/log/program_monitor/ -maxdepth 1 -name "*.log" | wc -l) -gt 0 ]; then echo "1" latest_log=$(ls -t /var/log/program_monitor/*.log | head -1) echo "2" echo "$latest_log" date_str=$(echo $latest_log | cut -d'/' -f5 | grep -oE '[0-9]{8}') echo "3" echo "$date_str" latest_time=$(date +%Y%m%d) echo "4" echo "$latest_time" if [ "date_str" == "latest_time" ]; then echo "5" echo ---------$(date)---------- >> $log_file else touch ${log_fiile} echo "6" fi else touch ${log_file} echo "7" fi } 出现touch: missing file operand这个是?
这个错误提示说明在touch命令中没有指定文件名,因此touch无法创建文件。在monitor函数中,如果找到了log文件,则会执行touch命令,但是touch命令中的文件名变量$log_file没有被正确设置。这可能是因为$log_file变量在if语句块中定义的,而在else语句块中也使用了这个变量,但是没有被正确设置。建议检查$log_file变量的赋值和使用,确保在touch命令中正确指定了文件名。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![xz](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-dosexec](https://img-home.csdnimg.cn/images/20210720083343.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)