Franske ITC-2480 Lab 13

From ITCwiki
Revision as of 20:30, 15 April 2014 by BenFranske (talk | contribs) (Created page with "=Introduction= In this lab you will create and modify some basic BASH scripts. =Lab Procedure= == Prerequisites == # Have completed the reading on BASH scripting. If you haven...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

In this lab you will create and modify some basic BASH scripts.

Lab Procedure

Prerequisites

  1. Have completed the reading on BASH scripting. If you haven't done this you will almost certainly have difficulty completing the lab!
  2. Open an SSH console to your Linux system using the PuTTY software, login with your standard user account

Creating Simple BASH Scripts

  1. At their core BASH scripts are really just lists of commands which could have been entered directly into the command line but have been pre-entered into a file to be run later instead. We can see how this works by creating a couple of very simple BASH scripts which each just run a single command. Your ability to write BASH scripts that do something useful is really only limited by how well you know Linux commands and utilities which could be included in the script.
  2. The echo command can be used on your system to print information out to the standard output. Try running the command "echo Hello World!" on your computer. You should see the text "Hello World!" printed out on the screen below your command.
  3. In order to turn this into a script we just need to put the command into a text file and add one more line, called a sha-bang because it is made up of a hash (pound) sign and a bang (exclamation) sign. This line tells the system what program to use to interpret the commands in the script. In this case we're going to be writing BASH scripts so we'll set it for the BASH program. Create a new text file called "hello.sh" with the following text:
    #!/bin/bash
    echo Hello World!
    
  4. Set the hello.sh file to be executable by all users on the system so that we will be able to run it.
  5. As a safety precaution against mistakenly executing something you don't really want to the shell will not allow you to run a file by just typing the filename, even if you are in the correct directory. The location of the file must either be included in your system's PATH, you must specify the full path to the file (such as /home/jsmith/hello.sh), or if you are in the correct directory already you can put in a relative path like ./hello.sh. Remember, a single period is a shortcut which means the current directory so ./ means a file in the current working directory. Try running your hello.sh script now. If your system outputs Hello World! just like before when you ran the echo command from the command line you have successfully written your first BASH script!
  6. Now try using what you have learned to create and test another script called simple-backup.sh which runs the command "tar -czf /var/jsmith-backup.tar.gz /home/jsmith/"

Using Variables and Conditionals in BASH Scripts

  1. Variables are a key part of turning a simple list of instructions you could type into a command line into something much more powerful by allowing you to reuse code and do something very similar (remember programmers are lazy!) but with a little tweak each time without re-writing the whole thing. Variables can be defined in the script itself but there are also special variables which are part of the BASH environment we can use. Variables can be identified in bash scripts as starting with a $. One type of these variables allows us to access arguments which have been supplied as part of the command line calling the script. Sometimes we only want to do a part of the script or we want to modify the script depending on the outcome of some test, called a conditional. This is often used in combination with variables as we'll see in the example below.
  2. In BASH the script name is assigned to the $0 variable, the first argument to $1 and so on. Let's see how we can modify our hello.sh script to make it a bit more personable by including a name. Try adjusting your hello.sh script to look like this:
    #!/bin/bash
    echo Hello $1!
    
  3. What do you think will happen if you run the script like this "./hello yourfirstname"? Try it and find out if you were correct in your hypothesis!
  4. Wait, what if someone doesn't know they are supposed to include their name as an argument though? Try running "./hello.sh" and see what happens. It might be the case that we want to go back to the simple "Hello World!" and maybe add some instructions if they don't specify an argument. Try updating your hello.sh script again and adding a conditional like this:
    #!/bin/bash
    if [ -n "$1" ]; then
        echo Hello $1!
    else
        echo Hello World!
        echo Try running $0 yourfirstnamehere to get a personal greeting!
    fi
    
  5. See if you can predict what these changes will do and then try it out to see if you are correct.
  6. It's also possible to capture the standard output of a command and put that into a variable for use. For example try running the "date +%Y%m%d" command. We can use the output of this command in a variable to make our backup script a little bit nicer. Save this script as better-backup.sh with the appropriate permissions to run it as a script.
    #!/bin/bash
    USER="jsmith"
    TODAY=$(date +%Y%m%d)
    BACKUPFILE="/var/"$USER"-backup-"$TODAY".tar.gz"
    echo Beginning backup for $USER on $TODAY
    tar -czf $BACKUPFILE /home/$USER/
    echo Backup completed for $USER saved to $BACKUPFILE
    
  7. See if you can predict what this script will do and then try it out to see if you are correct. Once you have figured out how this script works see if you can modify it so you can specify a username as an argument instead of as a fixed variable. Can you do this with a change to only one line?
  8. After successfully doing that try modifying the script so that it gives instructions about how to use the script if you forget to give a username as an argument.
  9. It is important to note that while this script works, it's just a simple example and not well written because it is easily broken by giving a username which doesn't actually exist on the system, or trying to backup files you don't have permission to access, etc. A better script could be written to check for all of these things as well as other errors. If the script is to be used by more people than the original author (and in the best cases, even then) it is important for usability and security purposes to do these sorts of checks, particularly if the script accepts user input such as an argument.