Introduction to Scripting
Objective:
The objective of this lab is to introduce scripting
What to do
A simple script, but first run the following command
echo "OPS105: Hello Bash"
OPS105: Hello Bash
Make a file, using your favourite editor, called
hello
and put the following information in it:#!/bin/bash echo "OPS105: Hello Bash"
So the contents of file
hello
are:cat hello
#!/bin/bash echo "OPS105: Hello Bash"
The properties of a file and a script are slightly different, but it is significant. Let us check the initial properties of
hello
before we make it a script, and we check whether or not we can run the file simply by using its namels -l hello hello
-rw-r--r-- 1 mfernand_stu mfernand_stu 39 Nov 12 16:27 hello -bash: ./hello: Permission denied
Change the permissions of
hello
to make it a script, and attempt to run it again. Observe the result.chmod a+x hello ls -l hello hello
-rwxr-xr-x 1 mfernand_stu mfernand_stu 39 Nov 12 16:27 hello OPS105: Hello Bash
Now make a directory called
rough_work
and puthello
in it, then attempt to run it. Notice the error message. Why?mkdir -p rough-work mv hello rough-work hello
-bash: ./hello: command not found
That error happened because the script
hello
was no longer in the search path ($PATH
). To run it properly we can do either one of the following:- call it with the correct path to
hello
rough-work/hello
OPS105: Hello Bash
- change your
pwd
to the location where thehello
script lies and call it from there
pwd cd rough-work pwd hello
/tmp /tmp/rough-work OPS105: Hello Bash
- call it with the correct path to