You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用read -r读取server.info文件无输出问题求助

解决方案

你的脚本问题在于while循环没有指定输入源,read命令默认从标准输入(终端)读取内容,所以脚本会一直卡住等待你手动输入,不会自动读取server.info文件。

有两种简单的修正方式:

方式一:使用输入重定向

直接将目标文件作为while循环的输入,这是最常用且高效的写法:

#!/bin/bash

file="server.info"
while IFS= read -r server_ip env_type; do
        echo "$server_ip and package type $env_type"    
done < "$file"

方式二:通过管道传递文件内容

用cat命令读取文件内容后,通过管道传给while循环:

#!/bin/bash

file="server.info"
cat "$file" | while IFS= read -r server_ip env_type; do
        echo "$server_ip and package type $env_type"    
done

另外注意脚本里的packege是拼写错误,正确应该是package,不过这个不影响脚本功能,只是建议修正。

内容的提问来源于stack exchange,提问作者Rus Zzzeta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 03:31:05