Lab 4 Advanced Scripting and Regular Expressions

This lab greatly improved my scripting skills, and allowed me to become more comfortable with Bash variable scoping, input/output redirection using pipes and selecting and changing text using regular expressions. Increasing my familiarity with Bash will allow me to be faster and more efficient when interacting with the terminal.

Procedure

  1. Using nano, the script fib was written to print the nth Fibonacci number. It is interesting to note that a function defintion in Bash is similar to C, obviously excluding the return type. This is despite the fact that Bash interprets commands while C compiles to assembly language.
  2. pgrep -u $(whoami) was used to identify the processes owned by the current user. This output could be piped into wc -l to determine the number of lines, and therefore the number of processes, owned. The final command was pgrep -u $(whoami) | wc -l.
  3. The command ls -l | grep '-......r' printed all files in world-readable files in the current directory by checking whether the seventh character of the permissions string is 'r'.

Commands

  • wc is used to count the number of words in piped input. The -l flag can be used to count the number of lines.
  • finger retrieves information about the users logged in.
  • whoami returns the current user's username.
  • head returns the first 10 lines of any piped input by default. The -n flag can be used to change the number of lines output.
  • tail returns the last 10 lines of any pipied input by default, although this can also be adjusted using the -n flag.
  • grep prints lines that contain a match for one or more patterns, and the -E flag allows the use of extended regular expressions.
  • sed allows regex transformations on an input stream, and the -E flag allows the use of extended regular expressions.