Question: Creating A Script In Vi Now we re ready to use vi to actually create our script. 1 Create an empty file using vi called
Creating A Script In Vi
Now were ready to use vi to actually create our script.
Create an empty file using vi called "opsscript.sh
Enter Insert Mode and type the following line:
#binbash
This is called a HashBang or shebang. It is optional for a script using the default interpreter, but is
considered good practice to always include it It tells the shell which interpreter to use to run the
script. If left out it will use the default interpreter set up on the system. This can cause the script to
run differently if it is different than the machine it was created on There are many interpreters available.
In the case we are calling the bash interpreter. Scripts can be also be used for other interpreters
like python, perl, ruby, php etc.. For example, you might see #binperl A shebang
is not optional for these interpreters.
A# by itself begins a comment. Place a comment with your name on the second line of the script.
The echo command, as we have already seen, sends what follows to the screen when the script runs.
Add this line:
echo "Script is running"
Save and exit your script and vi
The default permissions on the script do not include execute. You can read and write but not execute.
If you tried to run it now it would give you a permissions error.
To change the permissions you use the chmod command.
At the shell prompt, enter the command:
chmod opsscript.sh
The sets the permissions for your user to read, write, and execute. It sets permissions for all
other users to read and execute only. If you want your script to be private ie only you can read,
write, and execute use instead. A complete list of permissions options can be found in the
manual man page for chmod.
When running scripts you need to specify a path. In this case we want it to run from the current directory
so we will use Enter the following at your shell prompt to run the script:
opsscript.sh
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
