我正在尝试使用类似文本的文件:
1.1.1.1-Text1
2.2.2.2-Text2
3.3.3.3-Text3
并将每个grep结果作为不同的变量来实现:
ip=$(cat nodes.txt | grep -o '^[^-]*')
curl -k -sL -w 'Response Code: %{http_code} Time: %{time_total}' $ip -o /dev/null # variable IP with value of 1.1.1.1
curl -k -sL -w 'Response Code: %{http_code} Time: %{time_total}' $ip -o /dev/null # variable IP with value of 2.2.2.2
curl -k -sL -w 'Response Code: %{http_code} Time: %{time_total}' $ip -o /dev/null # variable IP with value of 3.3.3.3
所以我的测试结果如下:
Test from 1.1.1.1-Text1 Response Code: 200 Time: 0.000
Test from 2.2.2.2-Text2 Response Code: 200 Time: 0.000
Test from 3.3.3.3-Text3 Response Code: 200 Time: 0.000
答案 0 :(得分:0)
试试这样:
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
ip=`echo $line | grep -o '^[^-]*'`
echo $ip
# now you can use $ip as variable with proper value from file
# ...
done < nodes.txt