执行Bash脚本遇语法错误:unexpected token `<` 求助排查
问题:Bash脚本触发语法错误(line 30: syntax error near unexpected token
<) 运行脚本时出现如下报错:
line 30: syntax error near unexpected token
<' ./check_health.sh: line 30:done < <(grep -v '^#' /etc/oratab)'
脚本内容:
#!/bin/bash function check_dgmgrl { server_address="$1" dgmgrl_output=$(echo -e "connect /;\nshow configuration;\n" | dgmgrl / | grep "$server_address") echo "$dgmgrl_output" >> dgmgrl_output.txt } function check_srvctl { export ORACLE_SID="$1" echo "Service status for database $ORACLE_SID:" srvctl status service -db "$ORACLE_SID" >> service_status.txt } servers_file="Servers.txt" while IFS= read -r server_address; do check_dgmgrl "$server_address" done < "$servers_file" while IFS=: read -r db_name db_home _; do if [[ "$db_name" =~ ^\#.*$|^$ ]]; then continue fi check_srvctl "$db_name" done < <(grep -v '^#' /etc/oratab)
问题原因
<(command) 是Bash专属的进程替换语法,若脚本未通过Bash执行(比如用sh命令运行,或脚本的#!/bin/bash声明被忽略),POSIX标准Shell不支持该语法,就会触发此错误。
修复方案
方案1:确保用Bash执行脚本
- 给脚本添加可执行权限:
chmod +x check_health.sh - 直接运行脚本:
./check_health.sh - 或明确指定Bash调用:
bash check_health.sh
方案2:改用POSIX兼容的管道写法
如果需要脚本兼容更多Shell环境,可将最后一段的进程替换改为管道,写法如下:
grep -v '^#' /etc/oratab | while IFS=: read -r db_name db_home _; do if [[ "$db_name" =~ ^\#.*$|^$ ]]; then continue fi check_srvctl "$db_name" done
注:此写法中循环会在子Shell中运行,若后续有需要在父Shell保留变量的场景需注意,但当前脚本仅涉及文件写入,完全适用。
内容的提问来源于stack exchange,提问作者Mohammad saeed Madkhourian Far
相关产品推荐
相关产品推荐

