jayant
jayant

Reputation: 125

Creating a command in linux

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

Upvotes: 1

Views: 264

Answers (3)

Aaron Digulla
Aaron Digulla

Reputation: 328770

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.

Upvotes: 0

HonkyTonk
HonkyTonk

Reputation: 2039

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

Upvotes: 3

Ed Manet
Ed Manet

Reputation: 3178

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

Upvotes: 0

Related Questions