2016-02-26 58 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)" 
}