Introduction to Scripting - 2
Objective:
The objective of this lab is to introduce scripting with some sample scripts.
What to do
Download the sample scripts from ops105 account on matrix. Replace MYSENECA-USER with your MySeneca username in the example below
scp -r MYSENECA-USER@matrix.senecacollege.ca:~mark.fernandes/ops105/scripts/all-scripting-examples /tmp/scripting
List the contents of the scripts you just downloaded
cd /tmp/scripting ls -F
00-hello* 01-ask_and_hello* 02-if_else* 03-if_else-is_directory* 04-if_else-needs_3_arguments* a* misc/ scripting_symbols.txt
The scripts we are interested in are
00-hello
,01-ask_and_hello
,02-if_else
, and a few more. View the contents of file00-hello
are:cat 00-hello
#!/bin/bash # # OPS145: script 1 - introduction (output only) # Script name: hello # Purpose: display a message # display message to user echo "Hello bash"
Run the
00hello
script../00-hello
Hello bash
Next look at the contents of
01-ask_and_hello
example.cat 01-ask_and_hello
#!/bin/bash # ask user for their name read -p "What is your name? " NAME # show custom message to user echo "Hello $NAME from OPS105 scripting"
Run the
01-ask_and_hello
script../01ask_and_hello
What is your name? mark Hello mark from OPS105 scripting
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