Apr 13, 2010

Making a Script Executable

Making a Script Executable

Now we have our script file, we can run it in two ways. The simpler way is to invoke the shell with the name of the script file as a parameter, thus:

#/bin/sh first.sh

This should work, but it would be much better if we could simply invoke the script by typing its name, giving it the respectability of other UNIX commands.

We do this by changing the file mode to make the file executable for all users using the chmod command:

#chmod +x first.sh

Important Of course, this isn't the only way to use chmod to make a file executable. Use man chmod to find out more about octal arguments and other options.
We can then execute it using the command:

#first.sh

This may not work and you may get an error saying the command wasn't found. This is probably because the shell environment variable PATH isn't set to look in the current directory. To fix this, either type PATH=$PATH:. on the command line, or edit your .bash_profile file to add this command to the end of the file, then log out and back in again Alternatively, type ./first.sh in your scripts directory to give the shell the relative path to the file.

No comments: