Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use hostname completion in Bash UNIX
Thanks to Kirk Bauer's tutorials in Automating UNIX and Linux administration, I was able to set up a small function for bash that provide user with hostname completion in the command line. Use your favorite editor to open your ~/.bashrc and paste this:

shopt -s progcomp
_hostname()
{
  local cur
  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  # COMPREPLY is a 'one-liner'
  COMPREPLY=($(compgen -W '$( nigrep "name.*" / /machines | cut -d"
    " -f 5 | egrep -v "localhost|broadcasthost|machines")' -- $cur))
  # end of 'one-liner'
  return 0
}
# feel free to add commands after "ssh ping"
complete -F _hostname -o dirnames ssh ping
This code allows you to use hostname completion from NetInfo (bash has built-in hostname completion from /etc/hosts, but that's not really useful on Mac OS X).
    •    
  • Currently 2.14 / 5
  You rated: 2 / 5 (7 votes cast)
 
[10,091 views]  

Use hostname completion in Bash | 7 comments | Create New Account
Click here to return to the 'Use hostname completion in Bash' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use hostname completion in Bash
Authored by: patpro on Jan 28, '04 11:52:18AM
well, since I've posted this hint few days ago, I've found a better command : replace the line "COMPREPLY..." with this one : (all on one line)

COMPREPLY=($(compgen -W '$( nireport . /machines name | egrep -v "localhost|broadcasthost")' -- $cur))


[ Reply to This | # ]
Use hostname completion in Bash or ZSH
Authored by: adrianm on Jan 28, '04 02:50:53PM
And if you use the way better shell :), zsh, just add this to your .zshrc file:


autoload -U compinit
compinit

This completes all sorts of things, including hostnames.

[ Reply to This | # ]

Use hostname completion in Bash or ZSH
Authored by: patpro on Jan 28, '04 05:21:56PM

it still won't do hostname completion from Netinfo ;)



[ Reply to This | # ]
Use hostname completion in Bash or ZSH
Authored by: Thom on Feb 01, '04 02:10:13PM

Okay, then how CAN this hint be translated for zsh?



[ Reply to This | # ]
Use hostname completion in Bash
Authored by: ua on Jan 29, '04 11:41:32PM

http://sourceforge.net/projects/bash-completion/ will tab-complete pretty much everything. This has also been ported to FINK.



[ Reply to This | # ]
Use hostname completion in Bash
Authored by: Titanium Man on Jan 30, '04 10:11:05AM
This code allows you to use hostname completion from NetInfo (bash has built-in hostname completion from /etc/hosts, but that's not really useful on Mac OS X)
Add an entry to /etc/hosts for your computer's current IP address and a unique hostname (i.e. foobar.foo), then ping foobar.foo. You ping yourself. If you happen to be using zsh, open a new Terminal window and typ 'ssh f' then hit the TAB key. It autocompletes to foobar. Add machines records to NetInfo if you want, but for what you're trying to do, /etc/hosts should work.

[ Reply to This | # ]
/etc/hosts instead of NetInfo!
Authored by: EatingPie on Jan 30, '04 04:31:30PM

Might be a good idea to use /etc/hosts rather than NetInfo for adding your own hosts. I've heard... and so this is hearsay!... that NetInfo is going away. Don't know how true it is, but...

Note that in Jaguar you had to disable NetInfo hosts and turn on /etc/hosts. Now in Panther you can use both without worries.

Oh, and the sourceforge link above is an empty project, with no files. However, I did a "fink list" and see "bash-completion 20030209-1."


---
-Pie
<http://www.storybytes.com>



[ Reply to This | # ]