0

I am encountering a really strange problem, any help is appreciated.

I have a executable file compiled and cp to a specific location. The name of this executable is "qact" There is a newly added cout statement at the first line of the main function. But when I execute the binary file in that directory, i can not see it. After a long while I accidentally find out that if I am not in that directory, I can see the outputed string when i execute it.

And later I find out that it's only when I am in that directory, the executed binary file is wrong and I will not see the string.

when I use which on that executable, no mater what directory I am in, I always get the same result and it is the correct location.

Really confused..

2
  • What's the name of the executable? Assuming it's a.out, what's the output of pwd && ./a.out. (./ is important to make sure it's not a PATH issue) Commented Nov 26, 2010 at 3:59
  • the name of this executable is "qact" Commented Nov 26, 2010 at 4:27

1 Answer 1

2

Do you have another executable file of the same name elsewhere in your $PATH? If so, it's possible that bash is executing the wrong executable because it uses a hash table to avoid extra $PATH lookups (see Command Search and Execution).

For example, suppose your $PATH is /opt/local/bin:/usr/bin, and you have grep installed in only /usr/bin. When you execute grep, you get the obvious result:

$ echo $PATH
/opt/local/bin:/usr/bin
$ which grep
/usr/bin/grep
$ grep --version
grep (GNU grep) 2.5.1

Now suppose that you install a newer version of grep into /opt/local/bin, which is earlier in your $PATH than /usr/bin. Because which always does the full $PATH lookup each time, but bash keeps a hash table, bash still thinks that the command grep maps to the one in /usr/bin:

$ which grep
/opt/local/bin/grep
$ grep --version
grep (GNU grep) 2.5.1
$ /opt/local/bin/grep --version
GNU grep 2.6.3

You can use the type builtin to diagnose this problem. type will tell you if a command is a shell builtin, an alias, a function, a keyword, or an executable file. If the latter, it tells you the full path to the executable:

$ type grep
grep is hashed (/usr/bin/grep)

So how do you fix this? You can use the hash builtin to manipulate the hash table (type help hash for more information). If you just want to fix one entry (grep in this case), you can do hash -d grep to say "delete the hash table entry for grep", in which case the next time you execute grep, it will search the full $PATH as expected. If you want to clear out the entire hash table (say, if you just installed a large amount of new software or you changed your $PATH), then use hash -r to empty it.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the information. When I tried "hash", it says hash table is empty. When I "type" it, it shows the correct location. Yes there are many other version of this executable. I just tried something else. PATH="<the correct path>", so that no other path exist. And it is still doing the same thing to me.
I just understand that it is after i type the command into the shell when the shell remembers the command and puts them into the hash. But it's the right path.
Just tried this. 1. fresh env setup 2. run qact outside /apps/qfactor/server/bin --> good result, hash now has an entry 3. run qact inside /apps/qfactor/server/bin --> bad result, hash still has that entry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.