You are not logged in.
Pages: 1
Hi,
If I type a command, and bash responds with "command not found", how can I get it to automatically suggest a package to get that command?
Thanks!
Offline
You need to install pkgtools
[karol@black ~]$ pkgfile startx
extra/xorg-xinit
It's not automatic though.
Offline
To get some automagic, you could use bash's command_not_found_handle, e. g. in your bashrc:
command_not_found_handle() {
pkgfile $(echo $1 | cut -d ' ' -f1) || echo "Command not found!"
}
There are some special cases where you may not want this to happen, some of them may be covered in this article: http://www.linuxjournal.com/content/bas … -not-found
Offline
To get some automagic, you could use bash's command_not_found_handle, e. g. in your bashrc:
command_not_found_handle() { pkgfile $(echo $1 | cut -d ' ' -f1) || echo "Command not found!" }
There are some special cases where you may not want this to happen, some of them may be covered in this article: http://www.linuxjournal.com/content/bas … -not-found
What to I have to do to get 'Command not found'?
Offline
My bad, i could have sworn i tested it. Just forget about this part.
command_not_found_handle() {
pkgfile $1
}
I assumed pkgfile would give a non zero return value when nothing is found.
$ pkgfile
Error: No target specified
$ echo $?
1
$ pkgfile fail
$ echo $?
0
$ pkgfile || echo 'Command not found!'
Error: No target specified
Command not found!
$ pkgfile fail || echo 'Command not found!'
$
edit: and of course the cut part is unnecessary too.
Last edited by Wey (2011-07-22 18:08:38)
Offline
I think pkgfile may already have an option to do what we want
[karol@black ~]$ cat /usr/share/pkgtools/pkgfile-hook.bash
#!/bin/bash
command_not_found_handle () {
local command="$1"
local pkgs="$(pkgfile -b -v "$command")"
if [ ! -z "$pkgs" ]; then
echo -e "\n$command may be found in the following packages:\n$pkgs"
return 0
fi
printf "bash: $(gettext bash "%s: command not found")\n" $command
}
you just need to configure it. I'll have a look after dinner.
Offline
OK, the configuration is straightforward [1] but by default it works only for binaries. You need to edit /usr/share/pkgtools/pkgfile-hook.bash (if you're using bash shell) and change
local pkgs="$(pkgfile -b -v "$command")"
to
local pkgs="$(pkgfile -v "$command")"
to make it work for every file.
By default it works like this:
[karol@black ~]$ les foo
les may be found in the following packages:
core/linux-atm (2.5.1-2) : /usr/sbin/les
[karol@black ~]$ foo
bash: foo: command not found
[karol@black ~]$ vboxdrv
bash: vboxdrv: command not found
[karol@black ~]$ pkgfile les
core/linux-atm
[karol@black ~]$ pkgfile vboxdrv
community/virtualbox
[1] Set 'CMD_SEARCH_ENABLED=1' in /etc/pkgtools/pkgfile.conf and relogin or run
. /usr/share/pkgtools/pkgfile-hook.bash
(if you're using bash)
Last edited by karol (2011-07-22 19:03:18)
Offline
Pages: 1