1

I have created a simple script:

echo "the path of the current directory is `pwd`"

and saved it by the name pathinfo

then i have created a bin directory at my home page with path as /home/vpnsadmin/bin and copied my script(pathinfo) to that bin directory.

Now i want run this script as a command but it is showing error

-bash: /usr/bin/test2: No such file or directory

but if copy my script(pathinfo) to "/usr/bin/" then it runs as a command.

the PATH environment variable is set as-

PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/vpnsadmin/bin

My question is why does the shell not run it as a command when it is present in /home/vpnsadmin/bin. or else why does it only check for the binary at /usr/bin and not at /home/vpnsadmin/bin or at /bin

6
  • Pardon my ignorance. I m new to scripting and linux. Commented Apr 5, 2012 at 12:42
  • Did you chmod +x pathinfo to make the script executable? Commented Apr 5, 2012 at 12:46
  • yes i did that. that is why only it was running when i copied that to /usr/bin Commented Apr 5, 2012 at 12:48
  • Also try type -a pathinfo test2 to see where bash thinks they are Commented Apr 5, 2012 at 13:19
  • -bash: type: pathinfo: not found test2 is /home/vpnsadmin/bin/test2 This was the output when i typed the above command. Commented Apr 5, 2012 at 13:20

3 Answers 3

3

The shell that is to execute your command needs to have the correct PATH variable set at the time of execution and, depending on shell, might need to have created its own internal (hash)map of the available commands.

Assuming you are using bash, try the following with your script saved in /usr/bin:

$ PATH=/ test2

$ PATH=/usr/bin test2

In the first case you should get an expected "not found" error, in the second it should work. The third test to perform is left as an exercise...

And I have to say that the supplied error message looks a bit odd if you actually tried to do

$ test2

and not

$ /usr/bin/test2

before copying the command to /usr/bin.

Edit:

Also, avoid naming your scripts test, in any way shape or form. This causes so much confusion for beginners.

Hint:

man test

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

12 Comments

You were right for the first two cases. But in the third case the problem is exactly what is asked. The supplied error message is "-bash: /usr/bin/test2: No such file or directory" that is what i dont understand.
yeah thanks for that. Actually first i named it test and then edited to test2. But i will take care of that from next time.
Actually my questions is that bash shell should search for the binary at all paths provided in the PATH env variable. But it is not doing that and only restricted to /usr/bin
@jayant Did you perform the third test? Also, is the execute bit set on the '/home/vpnsadmin/bin` directory?
yo...i performed the third test and the execute bit is set there.
|
0

Did you have the path to bash at the top of your script and did you use backticks around pwd?

#!/bin/bash
echo "the path of the current directory is `pwd`"

Did you make the file executable?

chmod +x pathinfo

4 Comments

atually the problem is not with the script. the script is running fine when i am directly running it but it is not running when it present in one of the paths set in PATH environment varialbe other than "/usr/bin"
That should be a comment, not an answer.
What shell do you use? Eg ps $$ or echo $SHELL ? Some shells (like zsh) requires you to run rehash (a shell builtin) to update their table of commands...
/bin/bash this the shell i am using
0

There is another script pathinfo somewhere in your path which contains a call to /usr/bin/test2

Try whereis pathinfo to see how many there are and which pathinfo to see which one your shell currently prefers.

1 Comment

there is no output of the "whereis pathinfo" command or "locate pathinfo" command.

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.