Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Not sure what is happening.

Lets call script a.sh with the following

/users/guru$ cat a.sh
#! /usr/bin/ksh

echo "Hello"
date

Execute with ksh. It gets done.

/users/guru$ ksh a.sh
Hello
Tue Jul 15 15:00:52 EDT 2014

Ensure file permission and ksh path.

/users/guru$ ls -l a.sh
-rwxrwxrwx    1 guru  kpc         35 Jul 15 15:00 a.sh
/users/guru$ a.sh
ksh: a.sh:  not found
/users/guru$ which ksh
/usr/bin/ksh
share|improve this question
    
Sometimes it is possible to have whitespaces which might cause the error. See the answer to this question here. unix.stackexchange.com/questions/134581/… –  Ramesh Jul 15 at 19:22
    
The following command fixed it: add export PATH=$PATH:. to users .profile –  Guru Jul 15 at 22:09
1  
See my other comment about why PATH=$PATH:. is considered harmful. –  richard Jul 15 at 22:12
    
Thanks, Richard. It helps. However, I would ask for more reasons/detials, I do not agree a mere filename-command-conflict warrant banning inclusion of "." to PATH altogether. Simply, I would never name my scripts as a keyword (or ensure "." is at the end of path). –  Guru Jul 15 at 22:40

3 Answers 3

Try "./a.sh" when trying to execute it. It needs to know where the file is at.

The './' tells it to look in the current directory.

share|improve this answer
    
+1 OK. ./ does the trick. But why so? I should be able to execute the script as such calling the file? –  Guru Jul 15 at 19:20
1  
Shell environments have an environment variable called "PATH" (echo $PATH), if the script you are executing is not within the $PATH, then it won't know where to look. If you want to make it executable from anywhere try putting it in a directory in the path, or add a directory to it, where you keep your scripts. –  Tim Jul 15 at 19:22
2  
It used to be common practice to have . in the PATH, but then it was realised that it was a security risk, so it was removed. Imagine . being at start of PATH. Then a local script called ls could intercept calls to ls. If . is but at the end of PATH, then you install package that has a program with conflicting name the this program is run instead of the one you expect. (You may notice that in cmd on ms-windows . is implicitly in the PATH, this could be considered a bug in cmd, as it can not be changed. You may also note that ms-windows has more virus problems. –  richard Jul 15 at 22:11

When you type a command, the shell looks up the command from a list of directories, as specified by the PATH variable.

The current directory is not in PATH by default (for security reason), so the shell can not find your script.

Using ./, meaning the current directory, so the shell knows where is your script.

share|improve this answer
    
So, add "." to path and export path? I can use ./ but want to know why can't we run the script as such. –  Guru Jul 15 at 19:39
    
Yes, add PATH=$PATH:. in your .bashrc, and remember to do it for TESTING only. –  cuonglm Jul 15 at 19:42
    
@Guru You cant run it as such because the directory the script is located in is not in the PATH. Executing a.sh will only work if it can be found in your PATH. –  casey Jul 16 at 0:37

As I have posted in the other question, sometimes it is possible to get the file not found error even when you execute the script as ./scriptname. As I have posted in the other answer, you can test it in your machine.

Testing

cat ksh_experiment.ksh

#!/usr/bin/ksh
echo "Hello"

Now after providing the permissions when I ran the file, it produced the output successfully. Now as discussed over here, I inserted some carriage returns in my file. Now when I ran the script, I was getting the output as,

ksh: ./ksh_experiment.ksh: not found [No such file or directory]

Now, cat -v ksh_experiment.ksh too produced the same output. Also, if I typed vim ksh_experiment.ksh , a new file was getting opened.

As discussed in the answer of the link that I provided, I removed the carriage returns using the command,

 perl -p -i -e "s/\r//g" ksh_experiment.ksh

After fixing when I ran, I got the output as expected.

Now, you can use dos2unix as well to convert the file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.