You are not logged in.

#1 2011-07-22 15:11:01

ggs
Member
Registered: 2011-06-19
Posts: 3

command not found

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

#2 2011-07-22 15:14:39

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: command not found

You need to install pkgtools

[karol@black ~]$ pkgfile startx
extra/xorg-xinit

It's not automatic though.

Offline

#3 2011-07-22 17:16:17

Wey
Member
Registered: 2011-04-22
Posts: 217

Re: command not found

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

#4 2011-07-22 17:34:43

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: command not found

Wey wrote:

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

#5 2011-07-22 17:47:51

Wey
Member
Registered: 2011-04-22
Posts: 217

Re: command not found

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

#6 2011-07-22 18:32:01

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: command not found

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

#7 2011-07-22 18:57:22

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: command not found

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

Board footer

Powered by FluxBB