简体   繁体   中英

redirect command error output to /dev/null

i run ( host ) command on a list of subdomain's to get the ip for each domain... i want to ignore the not found: 3(NXDOMAIN) result i try with

for i in $(cat no-http.txt) ; do host $i 2>/dev/null; done

output:

e1792.dscx.akamaiedge.net has address 104.79.236.53
e1792.dscx.akamaiedge.net has IPv6 address 2a02:26f0:fa00:597::700
e1792.dscx.akamaiedge.net has IPv6 address 2a02:26f0:fa00:5a3::700
akamai-apigateway-warp3pl.tesla.com is an alias for akamai-apigateway-warp3pl.tesla.com.edgekey.net.
akamai-apigateway-warp3pl.tesla.com.edgekey.net is an alias for e1792.dscx.akamaiedge.net.
e1792.dscx.akamaiedge.net has address 104.79.236.53
e1792.dscx.akamaiedge.net has IPv6 address 2a02:26f0:fa00:5a3::700
e1792.dscx.akamaiedge.net has IPv6 address 2a02:26f0:fa00:597::700
Host olt.tesla.com not found: 3(NXDOMAIN)
^CHost kamai-apigateway-warp3pl.tesla.com not found: 3(NXDOMAIN)
Host kamai-apigateway-warpdashboardapi.tesla.com not found: 3(NXDOMAIN)

how i can redirect not found: replay to /dev/null

The NOT found causes host to return 1 so test its return code.

#!/usr/bin/env sh

while read -r name; do
  if reply=$(host "$name"); then
    printf '%s\n' "$reply"
  fi
done <no-http.txt

The "not found" message isn't being written to stderr, so you can't filter it by redirecting.

You can use grep -v to filter it out.

for i in $(cat no-http.txt) ; do host "$i" ; done | grep -v 'not found'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM