How to Run Shell Scripts ?
For creating a shell script, just open your vi editor or any other editor you are using to create files. In the editor type in the following two statements.
echo "Hello World!!"
echo "This is a shell script"
After typing in the statements, save the file with some filename say "abc".
There are two methods of running this shell script :
1. Use sh command on the shell prompt, like
$ sh abc
sh <shellscriptname> is used to execute the shell script passed to it as argument.
2. Other way is to change your shell script properties to executable. You can use chmod command on the shell prompt to change the properties of your shell script file to executable, such as :
$ chmod 755 abc
Where, chmod is the command which changes the properties of a file. 7 means you are giving read+write+executable permissions to the owner of the file i.e. you. 5 means you are giving read+write permissions to the group owners of the file. Next 5 means you are giving read+write permissions to other users. After using this command, your file i.e. shell script is now executable i.e. we can just type in its name on the shell prompt and it will gets executed, like :
$ abc
and , just after giving this command, the statements stored in the abc file will gets executed on your shell prompt.
Re: How to Run Shell Scripts ?
Once a shell script is created, there are several ways to execute it. However, before any Korn shell script be can executed, it must be assigned the proper permissions. The chmod command changes permissions on individual files, in this case giving execute permission to the simple file:
$ chmod +x simple
After that, you can execute the script by specifying the filename as an argument to the ksh command:
$ ksh simple
You can also execute scripts by just typing its name alone. However, for that method to work, the directory containing the script must be defined in your PATH variable. When looking at your .profile earlier in the course, you may have noticed that the PATH=$PATH:$HOME definition was already in place. This enables you to run scripts located in your home directory ($HOME) without using the ksh command. For instance, because of that pre-defined PATH variable, the simple script can be run from the command line like this:
$ simple