Open main menu

CDOT Wiki β

Changes

OPS245 Scripting Exercises dev

587 bytes removed, 21:57, 29 April 2023
no edit summary
== 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 ==
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.
* How to redirect === Piping output from a one command to a fileanother command ===* How to pipe In Bash, you can use the output from one of a command to 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:
<pre>
if [[ $grade -ge 50 ]]; then
echo "Congratulations, you passed!"
else
echo "I'm sorry, you failed."
fi
</pre>
 
=== 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).
 
<pre>
git clone https://github.com/ops245/python
</pre>
 
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'''
* if<pre>* test#!/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 screenecho "Hello" # List the contents of /ls / # Print good bye on the screenecho "Good Bye</pre>
* Sample (see below): Create a python script that does the same thing.
'''Python'''
*Python has conditional statements<pre>#!/usr/bin/env python3# Author: Jason Carman# Date: January 10, we just haven't covered them yet2023# Purpose: Print hello, list the contents of /, print good bye# Usage ./hello.py#
= Exercises =# Import the os moduleimport os # Print helloprint('Hello') # List the contents of the bin directoryos.system("ls /")
You can do these exercises in any order, and change them in any way you like.# Print good byeprint('Good Bye')
* Create a bash script that will print Hello, then list the contents of the </ directory, then print Good Bye.**Create a python script that does the same thing.* Create a bash script that will run your other script twice.** 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.pre>