当命令失败时,我正在寻找一种获取wget
命令所请求域的IP地址的方法。
我无法使用ping
命令获取IP,因为地址可能会在wget
命令终止后发生变化。
我想在shell脚本中执行此操作。
答案 0 :(得分:1)
当wget
失败时,它以非零退出状态终止,并将错误写入标准错误描述符(2)。
因此,您可以检查退出代码($?
变量),并解析写入标准错误的字符串:
url='http://stackoverflow.com/users/edit/1646322'
output=$( wget "$url" 2>&1 )
if [[ $? -ne 0 ]]; then
printf '%s' "$output" | \
perl -ne '/^Connecting to .*\|([^\|]+)\|/ and print $1'
fi
示例输出
151.101.129.69