Hi ,all :
I'm trying to define an alias with an embeded awk command:
alias checketh0 `ifconfig eth0 |grep 'inet addr:' |awk '{prinrt $2}' |cut -c 6- `
After I edit it in the .cshrc file and run "source .cshrc" , I run the "checketh0" command in the terminal , the screen displays the "192.168.7.24: Command not found "
Why does is it ?Any help will be highly appreciated.
note: I'm using csh shell.
Thanks for you help.
Thanks for your advice ~ But After I edit the command "alias checketh0 "echo `ifconfig eth0 | grep 'inet addr:' |awk '{print $2}' | cut -c 6-`" in my .cshrc file The screen displays this : innet addr:192.168.7.24 Bcast:192.168.7.255 Mask :255.255.255.0
I just want to the "192.168.7.24"
Maybe is it the awk command error?
On Tue, Dec 15, 2009 at 12:54 PM, Andrew Harley andrew@promed.com.auwrote:
On 15/12/09 15:40, Majian wrote:
Hi ,all :
I'm trying to define an alias with an embeded awk command:
alias checketh0 `ifconfig eth0 |grep 'inet addr:' |awk '{prinrt $2}' |cut -c 6- `
After I edit it in the .cshrc file and run "source .cshrc" , I run the "checketh0" command in the terminal , the screen displays the "192.168.7.24: Command not found "
Why does is it ?Any help will be highly appreciated.
note: I'm using csh shell.
Hi Majian,
It's not an issue with awk but with the way the alias is interpreted. It tries to run the result of the commands in between the ` ` as a command itself. To get around it, try:
# alias checketh0="echo `ifconfig eth0 | grep 'inet addr:' |awk '{print $2}' | cut -c 6-`"
Cheers,
Andrew
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
On Mon, Dec 14, 2009 at 7:20 PM, Majian jiannma@gmail.com wrote:
But After I edit the command "alias checketh0 "echo `ifconfig eth0 | grep 'inet addr:' |awk '{print $2}' | cut -c 6-`" in my .cshrc file The screen displays this : innet addr:192.168.7.24 Bcast:192.168.7.255 Mask :255.255.255.0
I think you need to escape either the {} or $ in the awk command, or else not use it. Try:
[root@ ~]# alias checketh0 "ifconfig eth0 | grep 'inet addr:'|cut -d ':' -f 2|cut -f 1 -d ' '" [root@ ~]# checketh0 12.17.152.3
Note that I did not use any '`' marks. The example is on the command line, not in .cshrc, but I hope it will work in that context anyhow.
This is a bit OT, should probably have OT in the subject?
HTH! Dave
On Mon, Dec 14, 2009 at 7:20 PM, Majian jiannma@gmail.com wrote:
But After I edit the command "alias checketh0 "echo `ifconfig eth0 | grep 'inet addr:' |awk '{print $2}' | cut -c 6-`" in my .cshrc file The screen displays this : innet addr:192.168.7.24 Bcast:192.168.7.255 Mask :255.255.255.0
I think you need to escape either the {} or $ in the awk command, or else not use it. Try:
[root@ ~]# alias checketh0 "ifconfig eth0 | grep 'inet addr:'|cut -d ':' -f 2|cut -f 1 -d ' '" [root@ ~]# checketh0 12.17.152.3
<snip> Simpler, and more obvious to read:
/sbin/ifconfig eth0 | awk '{if ( $0 ~ /inet addr:/ ) {print substr($2,6)}}'
mark "awk! awk!"
m.roth@5-cent.us wrote:
Simpler, and more obvious to read:
/sbin/ifconfig eth0 | awk '{if ( $0 ~ /inet addr:/ ) {print substr($2,6)}}'
That's only obvious to the couple of people who speak awk... (Probably about the same number that use the C shell that started this problem).
With more generic regexps it would be:
ifconfig eth0 |sed -n -e 's/(.*inet addr:)([0-9.]*)(.*)/\2/p'
m.roth@5-cent.us wrote:
Simpler, and more obvious to read:
/sbin/ifconfig eth0 | awk '{if ( $0 ~ /inet addr:/ ) {print substr($2,6)}}'
That's only obvious to the couple of people who speak awk... (Probably about the same number that use the C shell that started this problem).
With more generic regexps it would be:
ifconfig eth0 |sed -n -e 's/(.*inet addr:)([0-9.]*)(.*)/\2/p'
Right, and that's obvious to the meanest intelligence, while "if what I read in has this, then print this part of that field" is sooo complicated.
Are you willing to agree to differ, and stop attacking me, when I give one awk script to replace 3-4 commands?
mark
m.roth@5-cent.us wrote:
m.roth@5-cent.us wrote:
Simpler, and more obvious to read:
/sbin/ifconfig eth0 | awk '{if ( $0 ~ /inet addr:/ ) {print substr($2,6)}}'
That's only obvious to the couple of people who speak awk... (Probably about the same number that use the C shell that started this problem).
With more generic regexps it would be:
ifconfig eth0 |sed -n -e 's/(.*inet addr:)([0-9.]*)(.*)/\2/p'
Right, and that's obvious to the meanest intelligence, while "if what I read in has this, then print this part of that field" is sooo complicated.
Are you willing to agree to differ, and stop attacking me, when I give one awk script to replace 3-4 commands?
It wasn't an attack - and the one sed command does the same thing with an even more lightweight program. And perl could do it without running ifconfig.
On Mon, Dec 14, 2009 at 8:54 PM, Andrew Harley andrew@promed.com.au wrote:
It's not an issue with awk but with the way the alias is interpreted. It tries to run the result of the commands in between the ` ` as a command itself. To get around it, try:
# alias checketh0="echo `ifconfig eth0 | grep 'inet addr:' |awk '{print $2}' | cut -c 6-`"
Er, no, the double quotes will mean that the backtick command is expanded at the time the alias is defined rather than at the time it is run. So you'll get the ifconfig output from the time the shell started, always.
You want
alias checketh0 'echo `ifconfig eth0 | grep "inet addr:" |awk '''{print $2}''' | cut -c 6-`'
(Please pardon gmail's stupid line wrapping in that last example. Should be all one line.)