Skip to content
Snippets Groups Projects
Commit 18ded812 authored by Daniel Shchur's avatar Daniel Shchur
Browse files

Assignment 2 and scripts

parent 80cd5a51
No related branches found
No related tags found
No related merge requests found
cse251-hw1.pdf 100644 → 100755
File mode changed from 100644 to 100755
File added
dshchur-hw1.pdf 100644 → 100755
File mode changed from 100644 to 100755
File added
\documentclass[12pt,letterpaper]{article}
\usepackage[letterpaper, margin=.75in]{geometry}
\usepackage[table]{xcolor}
\newcommand{\problem[1]}{\vspace{.25cm}\textbf{Problem {#1}:~}}
\newcommand{\answer}{\emph{Answer:~~}}
\newcommand{\ansindent}{\indent\hspace{0.5cm}}
\newcommand{\ansend}{\vspace{0.25cm}\\}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0cm}
\setlength{\intextsep}{.25cm}
\renewcommand{\labelenumi}{\alph{enumi})}
\renewcommand\familydefault{\sfdefault}
\newcommand{\shell}[1]{\indent\indent\colorbox{lightgray!20}{\texttt{\footnotesize #1}}}
\newcommand{\shellcmd}[1]{\indent\indent\colorbox{lightgray!20}{\texttt{\footnotesize\# #1}}}
\newcounter{p3}
\newcounter{p4}
\newcounter{p6}
\title{CSCE 251 Homework 02}
\author{Daniel Shchur}
\begin{document}
\maketitle
\section{PIPES AND FILTERS}
\problem[1] Give a single command (any commands seperated by pipes are considered to be a single command) of the following:
\begin{enumerate}
\item Read the last 26 lines from the file ``essay.txt'', and append them to the end of the file ``paragraph.txt''.
\begin{enumerate}
\item[] \shellcmd{tail -26 essay.txt >> paragraph.txt}
\end{enumerate}
\item List all of the files/directories in the directory \shell{/source/} that end in ``.cpp'', determine the number of files in this result, and save that value to the file ``cppFiles'' (overwriting the file if it exists, \underline{not} appending to it).
\begin{enumerate}
\item[] \shellcmd{ls -a /source/ | grep .cpp | wc -l > cppFiles}
\end{enumerate}
\item List all files in the current directory in order of least-recently used to most-recently accessed, then append the list of files to the file \shell{/results/ordered}.
\begin{enumerate}
\item[] \shellcmd{ls -pur | grep -v / >> /results/ordered}
\end{enumerate}
\item Read the first 50 lines from the file \shell{/usr/logins.txt}, sort them in lexicographical (dictionary) order, remove any duplicate lines (while displaying the number of duplicates next to each), sort them again so they are in numerical order from largest to smallest, then append the top 5 lines of the result to the file \shell{/tmp/biggestLogins}.
\begin{enumerate}
\item[] \shellcmd{head -50 /usr/logins.txt | sort -d | uniq -c | sort -nr | head -5 >> /tmp/biggestLogins}
\end{enumerate}
\end{enumerate}
\section{WILDCARDS AND REGULAR EXPRESSIONS}
\problem[2] Answer the following questions about wildcards:
\begin{enumerate}
\item How would you use the \shellcmd{ls} command to display all of the files/directories in the directory \shell{/sbin} that start with ``bl''?
\begin{enumerate}
\item[] \shellcmd{ls -a bl* /sbin}
\end{enumerate}
\newpage
\item How would you use the \shellcmd{ls} command to display all of the files/directories in your current directory that contain the word ``grade'' somewhere in the name and end with ``.pdf''?
\begin{enumerate}
\item[] \shellcmd{ls -a *grade*.pdf}
\end{enumerate}
\item How would you use the \shellcmd{ls} command to display all of the files/directories in your current directory that are any two letters, followed by the file extension ``.py''?
\begin{enumerate}
\item[] \shellcmd{ls -a ??.py}
\end{enumerate}
\item Which wildcard character can match any number of characters?
\begin{enumerate}
\item[] \answer `*'
\end{enumerate}
\item Which wildcard character matches only one character?
\begin{enumerate}
\item[] \answer `?'
\end{enumerate}
\end{enumerate}
\problem[3] Answer the following questions about regular expressions:
\begin{enumerate}
\item Which characters are used in \shellcmd{egrep} regular expression patterns to group pieces of an expression together?
\begin{enumerate}
\item[] \answer `(' and `)'
\end{enumerate}
\item Give the command to search the file ``idioms.txt'' for all lines beginning with ``When'' or ``Why'' or ``How''.
\begin{enumerate}
\item[] \shellcmd{grep -E `(\^{}When|\^{}Why|\^{}How)' idioms.txt}
\end{enumerate}
\setcounter{p3}{\value{enumi}}
\end{enumerate}
Give regular expressions (such that could be used with \shellcmd{grep} and \shellcmd{egrep}) for each of the following:
\begin{enumerate}
\setcounter{enumi}{\value{p3}}
\item All lines ending with the phrase ``here below''
\begin{enumerate}
\item[] \shell{`here below'}
\end{enumerate}
\item All lines that end in either `y' or `z'
\begin{enumerate}
\item[] \shell{`(y\$|z\$)'}
\end{enumerate}
\item All lines that contain the word ``index'' followed by any odd number/digit followed by any even number/digit (ignore 0s)
\begin{enumerate}
\item[] \shell{`index[13579][2468]'}
\end{enumerate}
\item All lines that contain either 0 or 1 instances of the character `R', but no more
\begin{enumerate}
\item[] \shellcmd{grep -Ev `R.*R'}
\item[]{\footnotesize\emph{Using the inverse flag because, unlike the slides say, `?' only works for repeated characters in succession.}}
\end{enumerate}
\item All lines that contain a word with 1 or more of the letter `a', followed by 0 or 1 of the letter `b', followed by the letter `c'
\begin{enumerate}
\item[] \shell{'a+b?c'}
\end{enumerate}
\end{enumerate}
\newpage
\section{THE AWK PROGRAMMING SYSTEM}
\problem[4] Answer the following questions about AWK:
\begin{enumerate}
\item The AWK programming language focuses on what type of data processing?
\begin{enumerate}
\item[] AWK focuses on text data processing
\end{enumerate}
\item Assume you have a file called ``calculate.awk'' containing AWK commands. Give the command for running all of the commands in this file.
\begin{enumerate}
\item[] \shellcmd{awk -f calculate.awk}
\end{enumerate}
\setcounter{p4}{\value{enumi}}
\end{enumerate}
Give AWK commands for accomplishing each of the following:
\begin{enumerate}
\setcounter{enumi}{\value{p4}}
\item Print the 2nd last field (the field directly before the last field) of each line from a file named ``last.txt''
\begin{enumerate}
\item[] \shellcmd{awk `\{if(NF>1)\{last=NF-1\} else\{last=NF\} print \$last\}' test.txt}
\item[]{\footnotesize\emph{NOTE: Prints out second to last only if there are more than 2 columns. If there is only 1 or less, it prints out the last column.}}
\end{enumerate}
\item Assume you have a file called ``names'' that contains a list of people, one person per line. Also assume that on each line, the 3rd field on that line contains the age of the person. Some of the people do not have an age listed, and those lines only have 1 field. Print the total age for all the people that have an age (the sum of all of their ages).
\begin{enumerate}
\item[] \shellcmd{awk `\{if(NF==3)\{sum+=\$3\}\} END \{print sum\}' names}
\end{enumerate}
\item Print the total number of lines from a file named ``lines''
\begin{enumerate}
\item[] \shellcmd{awk `END \{print NR\}' lines}
\end{enumerate}
\item Exchange the second and the third fields of every line, and then print the lines (you can print each line directly after the exchange). After printing all of the lines, print out the message ``Done!''. Assume the file name is ``switch.txt''.
\begin{enumerate}
\item[] \shellcmd{awk `\{if(NF>2)\{temp=\$2;\$2=\$3;\$3=temp;print\}\}' switch.txt}
\end{enumerate}
\end{enumerate}
\section{ADVANCED PIPELINE COMMANDS}
\problem[5] Answer the following questions about the \shellcmd{cut} and \shellcmd{tr} commands:
\begin{enumerate}
\item If you have a file called ``saved\_data.txt'' with many lines of data, each line having values separated by a `\$' character, how would you obtain the 7th value of each line using the \shellcmd{cut} command?
\begin{enumerate}
\item[] \shellcmd{cut -d \$ -f 7 saved\_data.txt}
\end{enumerate}
\item Assume the same things from part (a), except now you want to obtain the 3rd character of the 5th value on each line. How would you do this using the \shellcmd{cut} command? \underline{HINT:} You may use piping here.
\begin{enumerate}
\item[] \shellcmd{cut -d \$ -f 5 saved\_data.txt | cut -c 3}
\end{enumerate}
\item If you have a file called ``records.csv'' with several lines of data, each line containing comma-separated values, give the AWK command to print off the 5th value on each line (you have to tell AWK to use a comma as the separator using the \shell{-F} flag, so look up this flag to see how to use it properly).
\begin{enumerate}
\item[] \shellcmd{awk -F , `{print \$5}' records.csv}
\end{enumerate}
\item Give the \shellcmd{cut} command for accomplishing the same thing that you did with AWK in part (c).
\begin{enumerate}
\item[] \shellcmd{cut -d , -f 5 records.csv}
\end{enumerate}
\item Assume you want to read as input the file ``passwords.txt'', change every lowercase letter to a uppercase letter and every `\$' to a `\#', then save the result back to that same file (overwriting it). How would you do this with the \shellcmd{tr} command? \underline{HINT:} Use piping and file redirection.
\begin{enumerate}
\item[] \shellcmd{tr `[:lower:]' `[:upper:]' < passwords.txt | tr `\$' `\#' > passwords.txt}
\end{enumerate}
\end{enumerate}
\problem[6] Answer the following questions about using the vi editor:
\begin{enumerate}
\item Assume you are using \shellcmd{vi} and are in insert mode. Describe what you need to do to save the changes you've made to a new file (name it ``save.txt'') and quit \shellcmd{vi} (assuming that the file hasn't been saved before). Give specific commands/keystrokes.
\begin{enumerate}
\item[] Hit escape then type \shell{:w save.txt} then hit enter. After, type \shell{:q} and hit enter.
\end{enumerate}
\setcounter{p6}{\value{enumi}}
\end{enumerate}
What does each of the following \shellcmd{vi} commands do (if you see a command we haven't talked about, just do a quick internet search to find out what the command does):
\begin{enumerate}
\setcounter{enumi}{\value{p6}}
\item 9dw
\begin{enumerate}
\item[] Deletes the current word under the cursor 9 times, so deletes 9 words in a row.
\end{enumerate}
\item 234G
\begin{enumerate}
\item[] Jumps to line 234.
\end{enumerate}
\item 12dd
\begin{enumerate}
\item[] Deletes 12 lines after the cursor (including the current line).
\end{enumerate}
\item 15h
\begin{enumerate}
\item[] Go back 15 characters (cursor movement).
\end{enumerate}
\item $\sim$
\begin{enumerate}
\item[] Changes the character from lowercase to uppercase or vice-versa.
\end{enumerate}
\item 4yy
\begin{enumerate}
\item[] Copy (yank) the next 4 lines (starting at the line of the cursor).
\end{enumerate}
\item How do you tell \shellcmd{vi} to open the file ``bubblesort.java'' with the cursor starting on line 46?
\begin{enumerate}
\item[] \shellcmd{vi +46 bubblesort.java}
\end{enumerate}
\item How do you tell \shellcmd{vi} to open the file \shell{/etc/passfile} as a read-only file?
\begin{enumerate}
\item[] \shellcmd{vi -R /etc/passfile}
\end{enumerate}
\item Other than the arrow keys on your keyboard, how do you move the cursor left/right/up/down with \shellcmd{vi}?
\begin{enumerate}
\item[] `h' moves left
\item[] `j' moves down
\item[] `k' moves up
\item[] `l' moves right
\end{enumerate}
\end{enumerate}
\section{COMMAND SHELLS}
\problem[7] Answer the following regarding shells:
\begin{enumerate}
\item List three command line shells.
\begin{enumerate}
\item[1.] Bourne Again shell (bash)
\item[2.] zsh
\item[3.] tcsh
\end{enumerate}
\item Do an Internet search to find and list 3 graphical shells (desktop environments) that exist for Unix-based operating systems.
\begin{enumerate}
\item[1.] GNOME
\item[2.] LXDE
\item[3.] i3
\end{enumerate}
\item When a shell first loads it reads setup and initialization settings from special hidden files called a \rule{1cm}{0.15mm} file.
\begin{enumerate}
\item[] configuration
\end{enumerate}
\item Give the command to create a new command called \shellcmd{ls2} that runs the longer command \shellcmd{ls -aF1}
\begin{enumerate}
\item[] \shellcmd{alias ls2=`ls -aF1'}
\end{enumerate}
\item What would you type to have your bash prompt change to the phrase ``Hi, \textless username\textgreater. You are currently in \textless current\_directory\textgreater. What should I do?'', where \textless current\_directory\textgreater\ is the current directory that you are in at any given time and \textless username\textgreater\ is the username of the current user of the shell? (Hint look up how to set the bash shell prompt)
\begin{enumerate}
\item[] \shellcmd{PS1=``Hi, \textbackslash u. You are currently in \textbackslash w. What should I do?''}
\end{enumerate}
\end{enumerate}
\end{document}
\ No newline at end of file
#!/bin/bash
for (( i=0; i<${#1}; i++ )); do
echo $1
done
if [ $2 -lt 0 ]; then
echo "Negative"
else
echo "Positive"
fi
#!/bin/bash
n=0
m=1
for (( i=1; i<=$1; i++ )); do
if [ $i -eq 1 ]; then
echo $n
elif [ $i -eq 2 ]; then
echo $m
elif [ $(( $i % 2 )) -ne 0 ]; then
echo $(( $m + $n ))
n=$(( $m + $n ))
elif [ $(( $i % 2 )) -eq 0 ]; then
echo $(( $m + $n ))
m=$(( $m + $n ))
fi
done
#!/bin/bash
if [ -z $1 ]; then
dir=$(pwd)
else
dir=$1
fi
echo
echo "Please enter two numbers, first one smaller than the next."
echo
echo "Enter the first:"
read num1
if [[ -z $num1 || $num1 -lt 0 ]]; then
echo "Error: number is NULL or less than 0"
exit 1
fi
echo
echo "Enter the second:"
read num2
if [[ -z $num2 || $num2 -lt 0 ]]; then
echo "Error: number is NULL or less than 0"
exit 1
fi
if [ $num1 -gt $num2 ]; then
echo "Error: The first number must be SMALLER than the second"
exit 1
fi
echo
echo "Listing all files between the sizes of $num1 and $num2 bytes in $dir"
echo
IFS=$'\n'
for i in $(ls -l $dir); do
size=$(echo $i | awk 'NF>2 {print $5}')
file=$(echo $i | awk 'NF>2 {print $9}')
if [ ! -z $size ]; then
if [[ ( $size -ge $num1 ) && ( $size -le $num2 ) ]]; then
echo "$file is $size bytes"
fi
fi
done
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment