OPJ
OPJ

Reputation: 123

Korn Shell Scripting header error

I just barely landed a job where 80% of my work is in the Unix Environment. I'm learning how to shell script. I'm really having a hard time trying to set the header on my practice scripts. If you take a look at my first script, it accepts the header. I created the same header again on a new script and it give me a bad interpreter. Both were created executables using the command chmod u+x nameOfScript. Both scripts are in the same location. So I really don't understand what's going on. Take a look:

parallels@ubuntu:~$ cat firstApp.ksh

#!usr/bin/ksh

# this prints the first line.

echo "Hi this is my first app, which me luck!"
echo "i hope this works again"    
#this is a comment as well

Now look at my other script which doesn't work, same header

parallels@ubuntu:~$ cat thirdApp.ksh
#!usr/bin/ksh

#print this

echo "I'm gonna try this again"

When I run this script, it runs...

parallels@ubuntu:~$ ./firstApp.ksh
Hi this is my first app, which me luck!
i hope this works again

But when I run this it fails

parallels@ubuntu:~$ ./thirdApp.ksh
bash: ./thirdApp.ksh: usr/bin/ksh: bad interpreter: No such file or directory

I'm copying the header exactly on both scripts, when I look for the Korn shell header I get this

parallels@ubuntu:~$ which ksh
/usr/bin/ksh
parallels@ubuntu:~$ 

Please let me understand what I'm doing wrong. Unix is still very new to me.

Upvotes: 1

Views: 1941

Answers (2)

David W.
David W.

Reputation: 107040

In Unix, the #! located in the first column of the first line of a script is called the shebang. This is the mechanism that allows the Unix executor to figure out the program you want to use to process your script. If you don't have a shebang, your script will execute with your default shell.

If you don't have the shebang in your shell script, but the user's default shell is Kornshell, everything will be okay. If the user's default shell is C-shell, your script won't work. Therefore, you should always use the shebang.

The problem with the shebang is that it doesn't use your $PATH to find the program. Instead, you must have the hard coded path. If you had:

#! usr/bin/ksh

And, you're in the /home/opj directory, it will look for /home/opj/usr/bin/ksh. This is the problem of the non-working script. You need that beginning /:

#! /usr/bin/ksh

That is, of course, assuming that your Kornshell application is under /usr/bin/ksh and not under /bin/ksh. Most shells are under the /bin directory. Check to make sure it's either /bin/ksh or /usr/bin/ksh.

One way around this limit is to depend upon env which is almost always under the /usr/bin. When you use env, env will search for the program along your path, and then use that as the interpreter to your script:

#! /usr/bin/env perl

My script will find the Perl interpreter whether it's under /bin/perl, /usr/bin/perl, or /usr/local/bin/perl. In fact, Perlbrew depends heavily upon this behavior.

The disadvantage is that you can't force someone to use a particular version of your interpreter. If I have #! /bin/ksh, that's what my script will use.

One more thing. Why are you using Kornshell instead of BASH. BASH and Kornshell are 95% identical in syntax, but BASH is just more ubiquitous now. I use Kornshell because that's what I learned way back in the last two decades of the 20th century. I even installed Kornshell on my Mac just because I like its features that aren't in Bash.

However, I recommend new users to learn BASH simply because if they have an issue, there's a lot more resources out there.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753615

The commands will only work when run in the root directory. You need:

#!/usr/bin/ksh

with the full absolute path name (starting with a /), not a relative pathname as you have with:

#!usr/bin/ksh

Note that to be relevant, the shebang (#!) must be the first two characters in the file. Leading blank lines, or blanks on the line before the #, render it irrelevant; the kernel looks to see if the first two characters are #! and if they are not, ignores the shebang line.

Upvotes: 3

Related Questions