LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 05-13-2009, 08:32 PM   #1
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432

Rep: Reputation: 109Reputation: 109
Can't find executables with 'which'


I have a 'bin' directory in my home that contains executables. For example: scandirs.sh. It's in my PATH, so I can just type 'scan{tab}{tab} to run it. But 'which scandirs.sh' returns nothing. Why?

TIA
 
Old 05-14-2009, 01:52 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Are you using tab completion when passing the filename to which?

Code:
which scan{tab}
 
Old 05-14-2009, 02:25 AM   #3
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Quote:
Are you using tab completion when passing the filename to which?
tab completion only works when you are in the directory containing the file.

make sure the name is correct.
 
Old 05-14-2009, 02:39 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

If which <command> doesn't find anything it will say so and show the path it looked in. Does the path shown include the bin dir?
 
Old 05-14-2009, 02:55 PM   #5
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by repo View Post
tab completion only works when you are in the directory containing the file.
Not for me in Ubuntu..

It does require the file to be executable, and in a folder specified in the PATH environment variable though.

Quote:
Originally Posted by druuna View Post
If which <command> doesn't find anything it will say so and show the path it looked in. Does the path shown include the bin dir?
For me, the which command doesn't give any output if the command specified is not found, although you can check the exit status:
Code:
which scandirs.sh
echo $?

Last edited by Disillusionist; 05-14-2009 at 02:59 PM.
 
Old 05-14-2009, 03:07 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@Disillusionist: I did not know about different which versions, this is what I get (path shortened for readability):
Code:
$ which foobar
which: no foobar in (/usr/qt/bin:/bin:/usr/bin:/usr/local/bin:/opt/ant/bin:)
$ echo $?
1
I just remembered something, some distro's use the actual which program, others use a script that has (roughly) the same behaviour:
Code:
#!/bin/bash
type -pa "$@" | head -n 1 ; exit ${PIPESTATUS[0]}
It seems that I'm using the actual program, not the script.

Last edited by druuna; 05-14-2009 at 03:11 PM.
 
Old 05-14-2009, 03:51 PM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Code:
$ which which
/usr/bin/which
$ bash -version
GNU bash, version 3.2.48(1) - release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
 
Old 05-14-2009, 04:17 PM   #8
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432

Original Poster
Rep: Reputation: 109Reputation: 109
Quote:
Originally Posted by Disillusionist View Post
Are you using tab completion when passing the filename to which?
Code:
which scan{tab}
Yes.


Quote:
Originally Posted by repo View Post
tab completion only works when you are in the directory containing the file.
make sure the name is correct.
Quote:
Originally Posted by Disillusionist View Post
Not for me in Ubuntu..

It does require the file to be executable, and in a folder specified in the PATH environment variable though.

For me, the which command doesn't give any output if the command specified is not found, although you can check the exit status:
Code:
which scandirs.sh
echo $?
I agree completely with Disillusionist.


Quote:
Originally Posted by druuna View Post
I just remembered something, some distro's use the actual which program, others use a script that has (roughly) the same behaviour:
You are right.

Code:
luc$[520]> which which
/usr/bin/which

luc$[521]> ls /usr/bin/which
lrwxrwxrwx 1 root root 10 2009-04-27 02:44 /usr/bin/which -> /bin/which

luc$[522]> ls /bin/which
-rwxr-xr-x 1 root root 946 2008-03-31 18:11 /bin/which

luc$[523]> cat /bin/which
#! /bin/sh
set -ef

if test -n "$KSH_VERSION"; then
        puts() {
                print -r -- "$*"
        }
else
        puts() {
                printf '%s\n' "$*"
        }
fi

ALLMATCHES=0

while getopts a whichopts
do
        case "$whichopts" in
                a) ALLMATCHES=1 ;;
                ?) puts "Usage: $0 [-a] args"; exit 2 ;;
        esac
done
shift $(($OPTIND - 1))

if [ "$#" -eq 0 ]; then
 ALLRET=1
else
 ALLRET=0
fi
case $PATH in
        (*[!:]:) PATH="$PATH:" ;;
esac
for PROGRAM in "$@"; do
 RET=1
 IFS_SAVE="$IFS"
 IFS=:
 case $PROGRAM in
  */*)
   if [ -f "$PROGRAM" ] && [ -x "$PROGRAM" ]; then
    puts "$PROGRAM"
    RET=0
   fi
   ;;
  *)
   for ELEMENT in $PATH; do
    if [ -z "$ELEMENT" ]; then
     ELEMENT=.
    fi
    if [ -f "$ELEMENT/$PROGRAM" ] && [ -x "$ELEMENT/$PROGRAM" ]; then
     puts "$ELEMENT/$PROGRAM"
     RET=0
     [ "$ALLMATCHES" -eq 1 ] || break
    fi
   done
   ;;
 esac
 IFS="$IFS_SAVE"
 if [ "$RET" -ne 0 ]; then
  ALLRET=1
 fi
done

exit "$ALLRET"
Why do they do that?

And it's just simply broken, since scandirs.sh is in my PATH and is executable.

Last edited by lucmove; 05-14-2009 at 04:19 PM.
 
Old 05-14-2009, 04:24 PM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

It seems that 'your' script is different from mine (the 'snippet' in post #6 is the script......).

What happens if you use that on?

@Disillusionist:

$ which --version
GNU which v2.16, Copyright (C) 1999 - 2003 Carlo Wood.
GNU which comes with ABSOLUTELY NO WARRANTY;
This program is free software; your freedom to use, change
and distribute this program is protected by the GPL.

Guess it is a good thing I'm in the process of switching to B/LFS 6.4 But it never needed patching and always works as expected.....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can`t find C-compiler in Debian ,or at least can`t find one that can make executables hemmelig Linux - Software 4 05-26-2008 03:07 AM
How to find executables in a folder? sikandar Linux - Software 6 04-10-2006 03:52 PM
How to find executables? Dadzilla Linux - Newbie 7 04-10-2006 03:46 PM
XFCE4-Synaptic problem, can't find executables. Stimz Linux - Software 3 08-21-2005 02:50 PM
Executables q64 Linux - Newbie 3 03-29-2004 03:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 04:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration