diff options
author | Madhusudan.C.S | 2011-06-10 23:19:59 +0530 |
---|---|---|
committer | Madhusudan.C.S | 2011-06-10 23:19:59 +0530 |
commit | 9f2cb3128d19148fe1318e6122c41f4d98f5ae96 (patch) | |
tree | 71478e43f0b10a5e8a66392707def67c47e9dd01 /ult | |
parent | 3e460d238465690e569610db3257d5a41c05f01c (diff) | |
parent | 8ac5a07c395f151e38ac29dfa51c14124d7ca901 (diff) | |
download | sees-9f2cb3128d19148fe1318e6122c41f4d98f5ae96.tar.gz sees-9f2cb3128d19148fe1318e6122c41f4d98f5ae96.tar.bz2 sees-9f2cb3128d19148fe1318e6122c41f4d98f5ae96.zip |
Merge branch 'master' of https://github.com/FOSSEE/sees
Diffstat (limited to 'ult')
-rw-r--r-- | ult/exercises.rst | 6 | ||||
-rw-r--r-- | ult/handout.rst | 44 | ||||
-rw-r--r-- | ult/ult.tex | 47 |
3 files changed, 82 insertions, 15 deletions
diff --git a/ult/exercises.rst b/ult/exercises.rst index b6be986..9898b39 100644 --- a/ult/exercises.rst +++ b/ult/exercises.rst @@ -57,7 +57,7 @@ Session-1 #. Change ownership of the file ``test`` to some other user (if exists). -#. Count the number of files in a directory. +#. Count the number of files (files, sub-directories, etc.) in a directory. #. Create a new file ``alice.txt`` by concatenating the first 30 lines and the last 40 lines of ``wonderland.txt``. @@ -73,7 +73,9 @@ Session-2 0. Read through the section ``REGULAR EXPRESSIONS`` in ``man grep`` -1. grep the marks of the students who scored above 75 in atleast one +#. Read through in ``man expr`` + +#. grep the marks of the students who scored above 75 in atleast one subject. #. grep the marks of all the students whose names begin with an 's' diff --git a/ult/handout.rst b/ult/handout.rst index b72dbdc..5ef762a 100644 --- a/ult/handout.rst +++ b/ult/handout.rst @@ -1785,6 +1785,41 @@ So, these are all the paths that are searched, when looking to execute a command. If we put the results.sh script in one of these locations, we could simply run it, without using the ``./`` at the beginning. +Variables +--------- + +As expected, it is possible to define our own variables inside our shell +scripts. For example, + +:: + + name="FOSSEE" + +creates a new variable ``name`` whose value is ``FOSSEE``. To refer to this +variable, inside our shell script, we would refer to it, as ``$name``. +**NOTE** that there is no space around the ``=`` sign. + +:: + + ls $name* + +It is possible to store the output of a command in a variable, by enclosing +the command in back-quotes. + +:: + + count=`wc -l wonderland.txt` + +saves the number of lines in the file ``wonderland.txt`` in the variable +count. + +Comments +-------- + +The ``#`` character is used to comment out content from a shell script. +Anything that appears after the ``#`` character in a line, is ignored by +the bash shell. + Control structures and Operators ================================ @@ -1910,6 +1945,15 @@ dummy or a loop variable. It can then be used to refer to the element of the list that is currently being dealt with. We could, obviously, use something as lame as ``i`` in place of ``animal``. +To generate a range of numbers and iterate over them, we do the following. + +:: + + for i in {5..10} + do + echo $i + done + Now, we use a ``for`` loop to list the files that we are interested in. :: diff --git a/ult/ult.tex b/ult/ult.tex index 62117eb..a94dc14 100644 --- a/ult/ult.tex +++ b/ult/ult.tex @@ -679,7 +679,7 @@ \begin{frame}[fragile] \frametitle{Redirection and Piping} \begin{lstlisting} - $ cut -d " " -f 2- marks.txt \ + $ cut -d " " -f 2- marks1.txt \ > /tmp/m_tmp.txt $ paste -d " " students.txt m_tmp.txt \end{lstlisting} % $ @@ -687,7 +687,7 @@ or \begin{lstlisting} - $ cut -d " " -f 2- marks.txt \ + $ cut -d " " -f 2- marks1.txt \ | paste -d " " students.txt - \end{lstlisting} % $ @@ -745,10 +745,10 @@ \item Error message is redirected, in the second case \end{itemize} \begin{lstlisting} - $ cut -d " " -c 2- marks.txt \ + $ cut -d " " -c 2- marks1.txt \ > /tmp/m_tmp.txt - $ cut -d " " -f 2- marks.txt 1> \ + $ cut -d " " -f 2- marks1.txt 1> \ /tmp/m_tmp.txt 2> /tmp/m_err.txt \end{lstlisting} % $ \begin{itemize} @@ -763,7 +763,7 @@ \begin{frame}[fragile] \frametitle{Piping} \begin{lstlisting} - $ cut -d " " -f 2- marks.txt \ + $ cut -d " " -f 2- marks1.txt \ | paste -d " " students.txt - \end{lstlisting} % $ \begin{itemize} @@ -852,7 +852,7 @@ \item Command below prints student marks, sorted by name \end{itemize} \begin{lstlisting} - $ cut -d " " -f 2- marks.txt \ + $ cut -d " " -f 2- marks1.txt \ | paste -d " " students.txt - \ | sort \end{lstlisting} % $ @@ -868,7 +868,7 @@ \item The command below sorts based on marks in first subject \end{itemize} \begin{lstlisting} - $ cut -d " " -f 2- marks.txt \ + $ cut -d " " -f 2- marks1.txt \ | paste -d " " students.txt -\ | sort -t " " -k 2 -rn \end{lstlisting} % $ @@ -887,7 +887,7 @@ \item Command below searches \& shows the marks of Anne alone \end{itemize} \begin{lstlisting} - $ cut -d " " -f 2- marks.txt \ + $ cut -d " " -f 2- marks1.txt \ | paste -d " " students.txt - | grep Anne \end{lstlisting} % $ @@ -902,7 +902,7 @@ \item \texttt{-i} for case-insensitive searches \end{itemize} \begin{lstlisting} - $ cut -d " " -f 2- marks.txt \ + $ cut -d " " -f 2- marks1.txt \ | paste -d " " students.txt - | grep -i Anne \end{lstlisting} % $ @@ -911,7 +911,7 @@ \item To see everyone's marks except Anne's \end{itemize} \begin{lstlisting} - $ cut -d " " -f 2- marks.txt \ + $ cut -d " " -f 2- marks1.txt \ | paste -d " " students.txt - | grep -iv Anne \end{lstlisting} % $ @@ -989,7 +989,7 @@ \begin{lstlisting} #!/bin/bash mkdir ~/marks - cut -d " " -f 2- marks.txt \ + cut -d " " -f 2- marks1.txt \ | paste -d " " students.txt - \ | sort > ~/marks/results.txt \end{lstlisting} % $ @@ -1013,6 +1013,21 @@ \end{frame} \begin{frame}[fragile] + \frametitle{Variables \& Comments} + \begin{lstlisting} + $ name=FOSSEE + $ count=`wc -l wonderland.txt` + $ echo $count # Shows the value of count + \end{lstlisting} % $ + \begin{itemize} + \item It is possible to create variables in shell scripts + \item Variables can be assigned with the output of commands + \item \alert{NOTE:} There is no space around the \texttt{=} sign + \item All text following the \texttt{\#} is considered a comment + \end{itemize} +\end{frame} + +\begin{frame}[fragile] \frametitle{\texttt{echo}} \begin{itemize} \item \texttt{echo} command prints out messages @@ -1020,7 +1035,7 @@ \begin{lstlisting} #!/bin/bash mkdir ~/marks - cut -d " " -f 2- marks.txt \ + cut -d " " -f 2- marks1.txt \ | paste -d " " students.txt - \ | sort > ~/marks/results.txt echo "Results generated." @@ -1036,7 +1051,7 @@ \begin{lstlisting} #!/bin/bash mkdir ~/marks - cut -d " " -f 2- marks.txt \ + cut -d " " -f 2- marks1.txt \ | paste -d " " students.txt - \ | sort > ~/marks/$1 echo "Results generated." @@ -1211,6 +1226,12 @@ \item Loop over the list; \texttt{animal} is a dummy variable \item Echo value of \texttt{animal} --- each name in list \end{itemize} + \begin{lstlisting} + for i in {10..20} + do + echo $i + done + \end{lstlisting} % $ \end{frame} \begin{frame}[fragile] |