2016-02-26 28 views
0

我嘗試使用下面的別名,在我的.bashrc文件:ASN查找別名不能正常工作

alias ip2asn="IP=$(dig $1 a +short);whois -h v4.whois.cymru.com " -v $IP"" 

本身工作沒有問題的命令,但是當我創建的.bashrc別名,並嘗試源〜 ./bashrc我得到以下輸出:

$ source .bashrc 
bash: alias: -v: not found 
bash: alias: k.root-servers.net.: not found 
bash: alias: d.root-servers.net.: not found 
bash: alias: c.root-servers.net.: not found 
bash: alias: e.root-servers.net.: not found 
bash: alias: f.root-servers.net.: not found 
bash: alias: m.root-servers.net.: not found 
bash: alias: g.root-servers.net.: not found 
bash: alias: b.root-servers.net.: not found 
bash: alias: j.root-servers.net.: not found 
bash: alias: i.root-servers.net.: not found 
bash: alias: h.root-servers.net.: not found 
bash: alias: a.root-servers.net.: not found 
bash: alias: l.root-servers.net.: not found 

我注意到,bash shell中甚至出現別名執行後要保留$ IP的變量。我不確定如何迴應這個問題。

有沒有建議嗎? 謝謝, --techno-shaman

回答

1

您的聲明中有一些問題。首先,你需要避開內部雙引號",否則你會關閉並重新打開它們。第二個問題是您的$IP正在擴展別名聲明而不是您使用它時。

關於$IP倖存的別名執行:別名只不過是爲您鍵入的命令。你不應該使用變量或者創建一個子shell - 用()來包含所有的東西。

alias ip2asn='(IP=$(dig $1 a +short);whois -h v4.whois.cymru.com -v "$IP")' 

反正我覺得像您期望的,別不接受這樣的參數,這是不行的,這$1將無法​​正常工作。這聽起來你需要一個功能:

ip2asn() { 
    whois -h v4.whois.cymru.com -v "$(dig "$1" a +short)" 
}