Introduction to Scripting

Objective:

The objective of this lab is to introduce scripting

What to do

  1. A simple script, but first run the following command

    echo "OPS105: Hello Bash"
    
    OPS105: Hello Bash
    
  2. Make a file, using your favourite editor, called hello and put the following information in it:

    #!/bin/bash
    
    echo "OPS105: Hello Bash"
    
  3. So the contents of file hello are:

    cat hello
    
    #!/bin/bash
    
    echo "OPS105: Hello Bash"
    
  4. 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 name

    ls -l hello
    hello
    
    -rw-r--r-- 1 mfernand_stu mfernand_stu 39 Nov 12 16:27 hello
    -bash: ./hello: Permission denied
    
  5. 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
    
  6. Now make a directory called rough_work and put hello 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
    
  7. 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 the hello script lies and call it from there
    pwd
    cd rough-work
    pwd
    hello
    
    /tmp
    /tmp/rough-work
    OPS105: Hello Bash
    

Practice Questions

Created: 2020-09-22 Tue 19:04