diff options
author | Jovina | 2012-08-17 12:07:31 +0530 |
---|---|---|
committer | Jovina | 2012-08-17 12:07:31 +0530 |
commit | 7a16de8fc1cf5688cfd949b9a35b24a0b5a54799 (patch) | |
tree | 489e1c5807f5183f4fde6171d394eca4864f6062 | |
parent | a315b094f4bfd2a664e8324dbd9b2f3fe0c548d4 (diff) | |
download | sdes-stscripts-7a16de8fc1cf5688cfd949b9a35b24a0b5a54799.tar.gz sdes-stscripts-7a16de8fc1cf5688cfd949b9a35b24a0b5a54799.tar.bz2 sdes-stscripts-7a16de8fc1cf5688cfd949b9a35b24a0b5a54799.zip |
Added ult_8.
-rw-r--r-- | ult/ult_8/marks1.txt | 5 | ||||
-rwxr-xr-x | ult/ult_8/results.sh | 3 | ||||
-rw-r--r-- | ult/ult_8/script.rst | 303 | ||||
-rw-r--r-- | ult/ult_8/students.txt | 5 | ||||
-rw-r--r-- | ult/ult_8/ult8.tex | 256 |
5 files changed, 572 insertions, 0 deletions
diff --git a/ult/ult_8/marks1.txt b/ult/ult_8/marks1.txt new file mode 100644 index 0000000..9a5299d --- /dev/null +++ b/ult/ult_8/marks1.txt @@ -0,0 +1,5 @@ +5 89 92 85 +4 98 47 67 +1 67 82 76 +2 78 97 60 +3 67 68 69 diff --git a/ult/ult_8/results.sh b/ult/ult_8/results.sh new file mode 100755 index 0000000..acdac84 --- /dev/null +++ b/ult/ult_8/results.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir ~/marks +cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | sort > ~/marks/results.txt diff --git a/ult/ult_8/script.rst b/ult/ult_8/script.rst new file mode 100644 index 0000000..ece800c --- /dev/null +++ b/ult/ult_8/script.rst @@ -0,0 +1,303 @@ +.. Objectives +.. ---------- + + .. At the end of this tutorial, you will be able to: + + .. 1. Prepare a simple shell script. + .. 2. Run a script successfully and print it's result. + .. 3. Understand what an environment variable is. + +.. Prerequisites +.. ------------- + +.. 1. Getting started with Linux +.. 2. Basic file handling +.. 3. Redirection and Piping +.. 4. Text Processing + + + +Script +------ + +.. L1 + +{{{ Show the first slide containing title, name of the production +team along with the logo of MHRD }}} + +.. R1 + +Hello friends and Welcome to the tutorial on +'Shell scripts & Variables'. + +.. L2 + +{{{ Show slide with objectives }}} + +.. R2 + +At the end of this tutorial, you will be able to, + + 1. Prepare a simple shell script. + #. Run a script successfully and print it's result. + #. Understand what an environment variable is. + +.. L3 + +{{{ Switch to the pre-requisite slide }}} + +.. R3 + +Before beginning this tutorial,we would suggest you to complete the +previous tutorials as being displayed currently. + +.. R4 + +Let us start with creating a simple shell script. +A shell script is simply a sequence of commands, that are put into a file, +instead of entering them one by one onto the shell. The script can then be +run, to run the sequence of commands in a single shot instead of manually +running, each of the individual commands. +For instance, let's say we wish to create a directory called ``marks`` in the +home folder and save the results of the students into a file +``results.txt``. + +.. L4 + +.. R5 + +We open an editor and save the following text to ``results.sh`` + +.. L5 + +{{{ Open an editor and type the following }}} +:: + + #!/bin/bash + mkdir ~/marks + cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | sort > ~/marks/ + results.txt + +.. R6 + +We can now run the script as, + +.. L6 + +{{{ Open the terminal }}} +:: + + ./results.sh + +.. R7 + +We get an error saying, Permission denied! Why? Can you think of the +reason? Yes, the file doesn't have execute permissions. +We make the file executable and then run it. + +.. L7 +:: + + chmod u+x results.sh + ./results.sh + +.. R8 + +We get back the prompt. We can check the contents of the file +``results.txt`` to see if the script has run. + +So, here, we have our first shell script. The first line of the script is used +to specify the interpreter or shell which should be used to execute the script. +In this case, we are asking it to use the bash shell. +Once, the script has run, we get back the prompt. Here, we had to manually +check, if the contents of the file are correct. It would be useful to have our +script print out messages. For this, we can use the ``echo`` command. We can +edit ``results.sh`` script, as follows. + +.. L8 + +{{{ Open an editor and type the following }}} +:: + + #!/bin/bash + mkdir ~/marks + cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | sort > ~/marks/ + results.txt + echo "Results generated." + +.. R9 + +Now, on running the script, we get a message on the screen informing us, +when the script has run. + +Let's now say, that we wish to let the user decide the file to which the +results should be written to. The results file, should be specifiable by an +argument in the command line. We can do so, by editing the file, as below. + +.. L9 + +{{{ Make the necessary changes in the previous script }}} + +:: + + #!/bin/bash + mkdir ~/marks + cut -d " " -f 2- marks1.txt|paste -d " " students.txt - | sort > ~/marks/$1 + echo "Results generated." + + +{{{ Highlight the text ``$1`` }}} + +.. R10 + +The ``$1`` above, corresponds to the first command line argument to the +script. So, we can run the script as shown below, to save the results to +``grades.txt``. + +.. L10 +:: + + ./results.sh grades.txt + +.. R11 + +When we run the ``results.sh`` file, we are specifying the location of the +script by using ``./``. But for any of the other commands, +we didn't have to specify their locations. Why? The +shell has a set of locations where it searches, for the command that we are +trying to run. + +.. L11 + +.. L12 + +{{{ Show slide, PATH }}} + +.. R12 + +These set of locations are saved in an "environment" +variable called PATH.let us look at what the value of the PATH variable is. +To view the values of variables, we can use the echo command. + +.. L13 + +{{{ Switch to the terminal }}} +:: + + echo $PATH + +.. R13 + +So, these are all the paths that are searched, when looking to execute a +command. If we put ``results.sh`` script in one of these locations, we +could simply run it, without using the ``./`` at the beginning. + +.. R14 + +As expected, it is possible to define our own variables inside our shell +scripts. For example, + +.. L14 + +{{{ Switch to the terminal }}} +:: + + name="FOSSEE" + +.. R15 + +It 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. + +.. L15 +:: + + ls $name* + + +.. R16 + +It is possible to store the output of a command in a variable, by enclosing +the command in back-quotes. + +.. L16 +:: + + count=`wc -l wonderland.txt` + +.. R17 + +It saves the number of lines in the file ``wonderland.txt`` in the variable +count. + +.. L17 + +.. L18 + +{{{ Show slide, variables & comments }}} + +.. R18 + +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. + +.. L19 + +{{{ Switch to 'Summary' slide }}} + +.. R19 + +This brings us to the end of the end of this tutorial. +In this tutorial, we have learnt to, + + 1. Prepare a shell script. + #. Display the result of a script, using the ``echo`` command. + #. Use the environment variable ``PATH``. + #. Create variables and comment out content using the ``#`` sign. + +.. L20 + +{{{ Show self assessment questions slide }}} + +.. R20 + +Here are some self assessment questions for you to solve + +1. Which sign is used to comment out content from a shell script. + + - $ + - % + - # + - * + +2. How will you add directory ``/data/myscripts`` to the beginning of + the $PATH environment variable ? + +.. L21 + +{{{ Solution of self assessment questions on slide }}} + +.. R21 + +And the answers, + + 1. We use the ``#`` sign to comment out the content from a shell script. + + 2. In order to add a directory to the beginning of the $PATH variable,we + say, +:: + + $PATH=/data/myscripts:$PATH + +.. L22 + +{{{ Show the Thank you slide }}} + +.. R22 + +Hope you have enjoyed this tutorial and found it useful. +Thank you! + + diff --git a/ult/ult_8/students.txt b/ult/ult_8/students.txt new file mode 100644 index 0000000..ddacd6b --- /dev/null +++ b/ult/ult_8/students.txt @@ -0,0 +1,5 @@ +Hussain +Dilbert +Anne +Raul +Sven diff --git a/ult/ult_8/ult8.tex b/ult/ult_8/ult8.tex new file mode 100644 index 0000000..a315ba1 --- /dev/null +++ b/ult/ult_8/ult8.tex @@ -0,0 +1,256 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Using Linux Tools +% +% Author: FOSSEE +% Copyright (c) 2009, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[17pt,compress]{beamer} +\usepackage{beamerthemesplit} +\mode<presentation> +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} + \setbeamertemplate{navigation symbols}{} +} +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage[scaled=.95]{helvet} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +\usepackage[T1]{fontenc} + +% change the alerted colour to LimeGreen +\definecolor{LimeGreen}{RGB}{50,205,50} +\setbeamercolor{structure}{fg=LimeGreen} +\author[FOSSEE]{} +\institute[IIT Bombay]{} +\date[]{} +% \setbeamercovered{transparent} + +% theme split +\usepackage{verbatim} +\newenvironment{colorverbatim}[1][]% +{% +\color{blue} +\verbatim +}% +{% +\endverbatim +}% + +\usepackage{mathpazo,courier,euler} +\usepackage{listings} +\lstset{language=sh, + basicstyle=\ttfamily\bfseries, + showstringspaces=false, + keywordstyle=\color{black}\bfseries} + +% logo +\logo{\includegraphics[height=1.30 cm]{../images/3t-logo.pdf}} +\logo{\includegraphics[height=1.30 cm]{../images/fossee-logo.pdf} + +\hspace{7.5cm} +\includegraphics[scale=0.99]{../images/fossee-logo.pdf}\\ +\hspace{281pt} +\includegraphics[scale=0.80]{../images/3t-logo.pdf}} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\sffamily \bfseries +\title +[Shell scripts \& Variables] +{Shell scripts \& Variables} +\author +[FOSSEE] +{\small Talk to a Teacher\\{\color{blue}\url{http://spoken-tutorial.org}}\\\vspace{0.25cm}National Mission on Education + through ICT\\{\color{blue}\url{ http://sakshat.ac.in}} \\ [1.65cm] + Contributed by FOSSEE Team \\IIT Bombay \\[0.3cm] +} + +% slide 1 +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} +\frametitle{Objectives} +\label{sec-2} + +At the end of this tutorial, you will be able to, +\begin{itemize} +\item Prepare a simple shell script. +\item Run a script successfully and print it's result. +\item Understand what an environment variable is. +\end{itemize} +\end{frame} + +\begin{frame} +\frametitle{Pre-requisites} +\label{sec-3} + +Spoken tutorial on, +\begin{itemize} +\item Getting started with Linux +\item Basic file handling +\item Redirection and Piping +\item Text Processing +\end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{\texttt{PATH}} + \begin{itemize} + \item Shell searches in a set of locations + \item Locations are saved in ``environment'' variable called PATH + \end{itemize} + \hspace{30pt}\texttt{\$ echo \$PATH} + %\hspace{26pt} \verb~$ echo $PATH~ + \begin{itemize} + \item Put results.sh in 1 of these locations + \item It can then be run without \texttt{./} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Variables \& Comments} + \verb~$ name=FOSSEE~ + \verb~$ count=`wc -l wonderland.txt`~ + \verb~$ echo $count # Shows the value~ + \verb~of count~ + \begin{itemize} + \item \alert{NOTE:} No space around the \texttt{=} sign + \item All text following the \texttt{\#} is considered a comment + \end{itemize} +\end{frame} + +\begin{frame} +\frametitle{Summary} +\label{sec-8} + + In this tutorial, we have learnt to, + + +\begin{itemize} +\item Prepare a shell script. +\item Display the result of a script, using the ``echo'' command. +\item Use the environment variable ``PATH''. +\item Create variables and comment out content using the ``\#'' sign. +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Evaluation} +\label{sec-9} + + +\begin{enumerate} +\item Which sign is used to comment out content from a shell script?\\ +%\begin{itemize} +{\color{LimeGreen}$\bullet$} \$ \hspace{80pt}{\color{LimeGreen}$\bullet$} \% \\ +{\color{LimeGreen}$\bullet$} \# \hspace{80pt}{\color{LimeGreen}$\bullet$} * \\ +\vspace{8pt} +\item How will you add directory ``/data/scripts'' to the beginning of + the \$PATH environment variable ? +\end{enumerate} +\end{frame} + +%\begin{frame}[fragile] +%\frametitle{Evaluation} +%\label{sec-9} + + +%\begin{enumerate} +%\item Which sign is used to comment out content from a shell script? +%\begin{itemize} +%\item \$ +%\item \% +%\item \# +%\item * +%\end{itemize} +%\vspace{8pt} +%\item How will you add directory ``/data/myscripts'' to the beginning of +% the \$PATH environment variable ? +%\end{enumerate} +%\end{frame} + +\begin{frame} +\frametitle{Solutions} +\label{sec-10} + + +\begin{enumerate} +\item `` \# '' +\vspace{15pt} +\item \texttt{\$ PATH=/data/scripts:\$PATH} +\end{enumerate} +\end{frame} + +\begin{frame} +\frametitle{SDES \& FOSSEE} +\begin{center} +\begin{itemize} +\item \small{SDES}\\ +\small{\color{LimeGreen}Software Development techniques for Engineers and Scientists} \\ +\scriptsize An initiative by FOSSEE. \\ +\vspace{3pt} +\scriptsize For more information on SDES, please visit {\color{blue}\url{http://fossee.in/sdes}}\\ +\vspace{10pt} +\item \small{FOSSEE}\\ +\small {\color{LimeGreen}Free and Open-source Software for \\Science and Engineering Education} \\ +\scriptsize Based at IIT Bombay, Funded by MHRD.\\ +\vspace{3pt} +\scriptsize Part of National Mission on Education through ICT \\(NME-ICT) \\ +\end{itemize} +\end{center} +\end{frame} + +\begin{frame} +\frametitle{About the Spoken Tutorial Project} +\begin{itemize} +\item Watch the video available at {\color{blue}\url{http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial}} +\item It summarises the Spoken Tutorial project +\item If you do not have good bandwidth, you can download and watch it +\end{itemize} +\end{frame} + +\begin{frame} +\frametitle{Spoken Tutorial Workshops}The Spoken Tutorial Project Team +\begin{itemize} +\item Conducts workshops using spoken tutorials +\item Gives certificates to those who pass an online test +\item For more details, please write to \\ \hspace {0.5cm}{\color{blue}contact@spoken-tutorial.org} +\end{itemize} +\end{frame} + +\begin{frame} +\frametitle{Acknowledgements} +\begin{itemize} +\item Spoken Tutorial Project is a part of the Talk to a Teacher project +\item It is supported by the National Mission on Education through ICT, MHRD, Government of India +\item More information on this Mission is available at: \\{\color{blue}\url{http://spoken-tutorial.org/NMEICT-Intro}} +\end{itemize} +\end{frame} + +\begin{frame} + + \begin{block}{} + \begin{center} + {\Large THANK YOU!} + \end{center} + \end{block} +\begin{block}{} + \begin{center} + For more Information, visit our website\\ + {\color{blue}\url{http://fossee.in/}} + \end{center} + \end{block} +\end{frame} + + +\end{document} + + |