UbuntuThis forum is for the discussion of Ubuntu Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
$ 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.
Are you using tab completion when passing the filename to which?
Code:
which scan{tab}
Yes.
Quote:
Originally Posted by repo
tab completion only works when you are in the directory containing the file.
make sure the name is correct.
Quote:
Originally Posted by Disillusionist
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
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.
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.....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.