加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash脚本使用cut命令在变量和存储结果在另一个变量

发布时间:2020-12-16 01:30:31 所属栏目:安全 来源:网络整理
导读:我有一个config.txt文件,IP地址为这样的内容 10.10.10.1:8010.10.10.13:808010.10.10.11:44310.10.10.12:80 我想ping该文件中的每个ip地址 #!/bin/bashfile=config.txtfor line in `cat $file`do ##this line is not correct,should strip :port and store t
我有一个config.txt文件,IP地址为这样的内容
10.10.10.1:80
10.10.10.13:8080
10.10.10.11:443
10.10.10.12:80

我想ping该文件中的每个ip地址

#!/bin/bash
file=config.txt

for line in `cat $file`
do
  ##this line is not correct,should strip :port and store to ip var
  ip=$line|cut -d: -f1
  ping $ip
done

我是一个初学者,对不起这样的问题,但我自己找不到.

awk解决方案是我会使用的,但是如果你想了解你的bash的问题,这里是你的脚本的修订版本.
##config file with ip addresses like 10.10.10.1:80
#!/bin/bash -vx
file=config.txt

while read line ; do
  ##this line is not correct,should strip :port and store to ip var
  ip=$( echo "$line" |cut -d: -f1 )
  ping $ip
done < ${file}

你可以写你的顶行

for line in $(cat $file) ; do ...

您需要使用命令替换$(…)来获取分配给$ip的值

从文件读取行通常被认为是更有效的同时读取行…完成< ${file}模式. 我希望这有帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读