Open main menu

CDOT Wiki β

OPS235 Scripting Exercises

Revision as of 13:10, 10 January 2020 by Andrew (talk | contribs)

Things on this page

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.

Runnning a command

  • How to run a command in the current directory or another directory or a directory in the $PATH
  • That programs you run need to have execute permission
  • What your $PWD is, pwd command
  • Check the return code from a command by examining $?

Variables

  • How to create a variable and set a value in it
  • How to get the value from a variable

Quotes

  • Why use single or double quotes
  • The difference between single and double quotes
  • Backquotes

Redirecting output

  • How to redirect output from a command to a file
  • How to pipe output from one command to another command

Basic commands

  • cat
  • grep
  • cut

Conditional statements

  • if
  • test, [

Exercises

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

  • Create a bash script that will print Hello, then list the contents of the / directory, then print Good Bye.
  • 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.
  • reate 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
  • 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.