OPS245 Scripting Exercises dev

From CDOT Wiki
Revision as of 21:56, 29 April 2023 by Jason Michael Carman (talk | contribs) (Exercises)
Jump to: navigation, search

What is scripting?

Why write a script?

Scripting is an essential skill for system administrators. A good system administrator (or sysadmin) is a lazy one. They write scripts to automate repetitive tasks such as creating users or backing up files/systems.

Terminal vs script file

A shell script is nothing more than a sequence of shell commands. Any command you put in a shell script can be executed just as well in a terminal. In fact no matter how complex your script is - you can run the entire thing from a terminal window without executing the script. Some of the earliest scripts people learn to create are the commands in the same sequence they would issue them from the command line, only in a script with the appropriate shebang line at the top. You learned about shebang lines in ULI101. The shebang line is the path to the interpreter, and must be the first line of your script.

The Bash shebang line is:

#!/bin/bash

The Python 3 shebang line is :

#!/usr/bin/env python3

Runnning scripts

Running scripts from your current directory or another directory or a directory in the $PATH

You can run a script from your current directory with ./ followed by the script name (without a space). I.E. ./script.bash or ./script.py Alternatively, if the script is in a directory that is specified in your $PATH environment variable you can execute the script by simply typing the name of the script without the ./. You can view your $PATH variable by issuing the command echo $PATH.

Since the scripts in our course are all located in /home/username/bin (which is part of our $PATH) you can execute them without the preceding ./

Script Permissions

You need to make sure scripts have the execute permission for the user or groups you wish to be able to execute your script. You can add the execute permission with the chmod command, which you learned in ULI101. As a quick refresher, what is the difference between the following commands?

  • chmod +x script.bash
  • chmod u+x script.bash

Both of the above commands will work. However, the first one gives execute permission to user, groups and other (in otherwords everyone). This is not the most secure way of allowing your scripts to be executed. The second one is a much better practice, which will give only your user execute permission.

Variables

$PWD and pwd, what is the difference?

$PWD is an environment variable that contains the path to your present working directory as an absolute path. pwd is a command that lists your present working directory, as an absolute path.

Exit codes and $?

$? is a special variable that contains the exit code of the last executed command. Linux uses an exit code of 0 to represent success. Any non-zero value represents failure. To check the exit status of the last command use echo $?.

Variables in Bash vs Python

Bash Variables

In ULI101 you learned about Bash variables. You can create a variable in bash at any time. You do not have to assign variable types to variables in Bash. Bash variables are character strings by default, but act as other data types based on the context you use them. For example, when you perform a math operation on two variables in bash they are treated as numbers.

This is how to create a variable in Bash and assign it a value:

number=1

There should be no space between the variable and it's value. Additionally, variable names are alpha-numeric and cannot begin with a number.

To refer to the value stored in a bash variable, you simply use the variable name with a $ in front of it. The $ means the value of that variable. The following example will print out the value of the variable number (which we assigned above) using the echo command:

echo $number

Python Variables

Python variables can be used in the same way as Bash variables (ie you do not need to assign a type when creating the variable). However, Python allows you the option to specify a data type when creating a variable. This is called casting. Python data types and casting is something we may look at later in the course.

This is how to create a variable in Python and assign it a value:

number = 1

Note the difference in spacing when compared to Bash variables. Python variables require a space between the equal sign and the content on either of it.

To refer to the value stored in a Python variable, you do not need a $ in front of it. You simply refer to the variable by name. The following example with print out the value of the variable number (which we assigned above) using the print function:

print(number)

Getting input from the user

Bash user input

In ULI101 you learned how to prompt the user for input and store it in a variable using the read command in Bash. There are two ways you can do this in Bash:

Prompt the user to enter their name using the echo command, then use the read command to store it in a variable:

echo "Please enter your name: "
read name

Do both in one line by using read with the -p option:

read -p "Please enter your name: " name

Either are correct. Which one you use is entirely up to your preference.

Python user input

In Python, prompting a user for input works very similarly to the second method above (using read -p). To prompt a user for input we use the input() function.

Prompt the user to input their name and store it in the variable name, using the input function:

name = input("Please enter your name: ")

Note: The space in both examples after the : is not required. It does however, make your prompt easier to read.

Quotes

Bash single vs double quotes

In Bash, single quotes suppress shell expansion; meaning variables and commands do not get expanded to their contents. Double quotes allow shell expansion.

Python single vs double quotes

In Python, there is no difference between single quotes. Both allow variable expansion. You can use whichever you prefer.

ULI101 review

Redirecting and piping output

Redirecting output to a file

In Bash, you can redirect the output of a command to a file with a single > between the command and the file. Note, this will overwrite the file. You can also append the output to a file with two >> between the command and the file. This will add the content to the file, not overwrite the file. In both cases if the file does not exist it will be created.

Piping output from one command to another command

In Bash, you can use the output of a command as the input for another command. This is known as piping.

Basic commands

  • cat: Cat is used to display the content of a file or files. If given multiple filenames it will combine (concatenate) them in the output.
  • grep: Grep is used to search for patterns using regular expressions.
  • cut: Cut is handy for isolating information from command output and storing it in a variable.

Conditional statements

Bash

Conditional statements or if statements are used in scripts to add logic. You can test to see if a condition is met and change the behaviour of the script as a result of that condition. Here is a sample if statement:

if [[ $grade -ge 50 ]]; then
     echo "Congratulations, you passed!"
else
    echo "I'm sorry, you failed."
fi

Python

  • Python has conditional statements, we just haven't covered them yet. We will cover them in a future lecture.

Exercises

Make sure you are in your home directory on c7host, and clone the github repo that contains templates for all the Python scripts you need this semester (including the sample hello.bash and hello.py below).

git clone https://github.com/ops245/python

These are a suggested order. You can do these exercises in any order, and change them in any way you like.

  • Sample (see below): Create a bash script that will print Hello, then list the contents of the / directory, then print Good Bye.

Bash

#!/bin/bash
# Author: Jason Carman
# Date: January 10, 2023
# Purpose: Print hello, list the contents of /, then print goodbye
# Usage: ./hello.bash

# Print hello on the screen
echo "Hello"

# List the contents of /
ls /

# Print good bye on the screen
echo "Good Bye
  • Sample (see below): Create a python script that does the same thing.

Python

#!/usr/bin/env python3
# Author: Jason Carman
# Date: January 10, 2023
# Purpose: Print hello, list the contents of /, print good bye
# Usage ./hello.py
#

# Import the os module
import os

# Print hello
print('Hello')

# List the contents of the bin directory
os.system("ls /")

# Print good bye
print('Good Bye')

  • Create a bash script that will run your other script (hello.bash) twice. Feel free to reuse this prompt for any of the other scripts. You have been provided a template with comments, but missing code. Fill in the missing pieces based on the comments.
#!/bin/bash
# Author:
# Date:
# Purpose: Run hello.bash twice
# Usage:
#

# Run hello.bash (just like you would execute it from the command line)

# Run hello.bash a second time
  • Now do it in python. You have been provided a template with comments, but missing code. Fill in the missing pieces based on the comments.
#!/usr/bin/env python3
# Author:
# Date:
# Purpose: Run hello.py twice
# Usage:
#

# Import the operating system module

# Run hello.py (using the os.system function and passing it the name of the script)

# Run hello.py a second time

  • Run this new script from different locations, and see if it always works. Fix it if it doesn't.
  • Create a bash script to display the contents of /etc/sysconfig/network-scripts/ifcfg-ens33
    • Pipe the output to cat
      • Pipe that output to cat. See if you understand why that doesn't seem to do anything
  • Create a bash script which will use cat and grep to find the line with BOOTPROTO in /etc/sysconfig/network-scripts/ifcfg-ens33
    • Modify that script so that it doesn't need cat anymore.
  • Create a bash script in which you will create a variable called BP.
    • Assign to that variable the value BOOTPROTO="dhcp" (the equal sign and quotes are part of the value).
    • Use the cut command to retrieve the part between the double-quotes (in this case that's: dhcp).
    • Save the result in a variable, and print that variable.
  • Combine the two scripts above into one. The script should tell you what the value of BOOTPROTO from /etc/sysconfig/network-scripts/ifcfg-ens33 is.
  • Create a python script that will prompt the user for the name of the interface they want to search (e.g. ens33), then prompt them for the parameter they wish to see.
    • Store the responses from the user in variables and use them to grep the appropriate file for the parameter the user asked for. Display it's current value.
    • Note: As we have not covered conditional statements or loops in python yet, you can assume the user always provides usable responses.
  • Use the ls and wc commands to find how many log files there are in /var/log/
    • Add a grep command to find how many of a certain type of log file there are (e.g. vmware-network log files)
  • Use the history and grep commands to find any command you ran in the past that contained a certain keyword (like .sh or cat)
  • Write a bash script which will use the whoami, hostname, date, and lvs commands to create a report.txt file containing all that information.
    • Set it up so that the date (in YYYY-MM-DD format) is in the filename of the report, e.g. report-YYYY-MM-DD.txt
  • Write a bash script that will ask the user for a process name, will check whether that process is running, and if it is: it will print "The process is running". If it isn't: it will print "The process is not running".
    • Modify that script to include the number of processes with that name that are running.
  • Write a script that will use a for loop and the cut command to get a list of usernames from the /etc/passwd file and print one username perline.
    • For each user: using an if statement check whether the directory /home/thatusername exists and then each line will look like: "user1: home directory does not exist" or "user2: home directory exists".
    • Instead of checking for /home/thatusername check for the home directory in the passwd file line for that user.