<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.ccn.ucla.edu/wiki/index.php?action=history&amp;feed=atom&amp;title=Bash_Scripting_Class</id>
	<title>Bash Scripting Class - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.ccn.ucla.edu/wiki/index.php?action=history&amp;feed=atom&amp;title=Bash_Scripting_Class"/>
	<link rel="alternate" type="text/html" href="https://www.ccn.ucla.edu/wiki/index.php?title=Bash_Scripting_Class&amp;action=history"/>
	<updated>2026-05-06T06:55:37Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://www.ccn.ucla.edu/wiki/index.php?title=Bash_Scripting_Class&amp;diff=724&amp;oldid=prev</id>
		<title>Ccn admin: 4 revisions</title>
		<link rel="alternate" type="text/html" href="https://www.ccn.ucla.edu/wiki/index.php?title=Bash_Scripting_Class&amp;diff=724&amp;oldid=prev"/>
		<updated>2014-01-16T03:14:25Z</updated>

		<summary type="html">&lt;p&gt;4 revisions&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Class One=&lt;br /&gt;
==Key Terms==&lt;br /&gt;
* &amp;lt;b&amp;gt;Shell&amp;lt;/b&amp;gt;      - a &amp;lt;i&amp;gt;command interpreter&amp;lt;/i&amp;gt;. Specifically, a program which takes user input and translates it into instructions the computer can understand&lt;br /&gt;
* &amp;lt;b&amp;gt;Session&amp;lt;/b&amp;gt;  - a particular instance of a shell. Practically, this is a single Terminal or Xterm window.&lt;br /&gt;
* &amp;lt;b&amp;gt;Variable&amp;lt;/b&amp;gt; - A string used to represent some other information. This is very similiar to the connotation of the term as used in mathematics.&lt;br /&gt;
*&amp;lt;b&amp;gt;Scope&amp;lt;/b&amp;gt;     - A variable&amp;#039;s visibility to other parts of a program, script, or shell. I understand that &amp;quot;visibility&amp;quot; is a loaded term, essentially scope determines whether a variable exists within a given context.&lt;br /&gt;
* &amp;lt;b&amp;gt;Environment&amp;lt;/b&amp;gt; - variables, paths, etc particular to a shell session.&lt;br /&gt;
*&amp;lt;b&amp;gt;Environment Variable&amp;lt;/b&amp;gt;  - An environment variable is a particular variable in a shell which holds a value significant to your working environment. Examples include, $PATH, $SHELL, $FSLDIR, etc.&lt;br /&gt;
*&amp;lt;b&amp;gt;Piping&amp;lt;/b&amp;gt;  - Sending the output of one program as the input to another. Within shell scripting, this is denoted by the | symbol.&lt;br /&gt;
*&amp;lt;b&amp;gt;Special Character&amp;lt;/b&amp;gt;  - A character which holds a special meaning in the shell environment. Common examples include |, \, /, $, ;&lt;br /&gt;
&lt;br /&gt;
==Key points about variables and their use in shells==&lt;br /&gt;
A variable only retains its value within the scope it is set. Within BASH, a variable is set with the = operator&lt;br /&gt;
 $ myFirstVar=&amp;quot;somevalue&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The contents of a variable is accessed by prepending a $ to the variable name&lt;br /&gt;
 $ echo $myFirstVar&lt;br /&gt;
&lt;br /&gt;
The following example will demonstrate both variable assignment and scope. &lt;br /&gt;
&lt;br /&gt;
copy/paste the following into a text file named myTemp.sh:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
&lt;br /&gt;
if [ -e ~/.bashrc ];then&lt;br /&gt;
  temp=&amp;quot;wut&amp;quot;&lt;br /&gt;
  echo $temp&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
echo $temp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in your shell type:&lt;br /&gt;
 &lt;br /&gt;
 $ temp=&amp;quot;no wai&amp;quot;&lt;br /&gt;
 $ echo $temp&lt;br /&gt;
 $ sh myTemp.sh&lt;br /&gt;
 $ echo $temp&lt;br /&gt;
&lt;br /&gt;
Your output should look something like&lt;br /&gt;
&lt;br /&gt;
 james$ temp=&amp;#039;no wai&amp;#039;&lt;br /&gt;
 james$ echo $temp &lt;br /&gt;
 no wai&lt;br /&gt;
 james$ sh myTemp.sh &lt;br /&gt;
 wut&lt;br /&gt;
 wut&lt;br /&gt;
 james$ echo $temp&lt;br /&gt;
 no wai&lt;br /&gt;
&lt;br /&gt;
Now your probably not named james, but the key point is that although the myTemp.sh script acts on a variable called $temp, it only takes on the value of &amp;#039;wut&amp;#039; for the duration of the script, it then goes back to our original value of &amp;#039;no wai&amp;#039;. In scripting parlance, we would say &amp;quot;the variable $temp only retained the &amp;#039;wut&amp;#039; value while in the scope of the temp.sh script&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==Piping &amp;amp; redirects==&lt;br /&gt;
===Piping===&lt;br /&gt;
One of the very powerful features of a unix system is the ability to chain commands together. In this way, a series of very simple programs or operations can build upon each others results to accomplish complex tasks.&lt;br /&gt;
&lt;br /&gt;
Piping is denoted by the | character. A simple example of piping is sorting the output of the ls command like so&lt;br /&gt;
&lt;br /&gt;
 $ ls -l | sort&lt;br /&gt;
&lt;br /&gt;
There is no limit to the number of times you can pipe output. For example, this will only display the results of ls which have the word &amp;quot;ninja&amp;quot; in it...in alphabetical order.&lt;br /&gt;
&lt;br /&gt;
 $ ls -l | grep &amp;quot;ninja&amp;quot; | sort&lt;br /&gt;
&lt;br /&gt;
And this only prints the 3 and 4th columns of the output of ls which has the word &amp;quot;ninja&amp;quot; in it and it&amp;#039;s still in alphabetical order (Oh My!)&lt;br /&gt;
&lt;br /&gt;
 $ ls -l | grep &amp;quot;ninja&amp;quot; | awk &amp;#039;{print $3 &amp;quot;   &amp;quot; $4 }&amp;#039; | sort&lt;br /&gt;
&lt;br /&gt;
On each subsequent iteration of the |, it treats the output from the previous executions as its input.&lt;br /&gt;
&lt;br /&gt;
A second useful technique is to send your output to somewhere else, normally a file. This is done using two special symbols&lt;br /&gt;
&lt;br /&gt;
 &amp;gt; &amp;amp; &amp;gt;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The basic syntax is: &lt;br /&gt;
&lt;br /&gt;
 $ [some commands and stuff] &amp;gt; [where I want the output to go]&lt;br /&gt;
&lt;br /&gt;
So to save the ouput of ls to a text file named &amp;quot;textfile.txt&amp;quot; you would&lt;br /&gt;
&lt;br /&gt;
 $ ls -l &amp;gt; textfile.txt&lt;br /&gt;
&lt;br /&gt;
===What&amp;#039;s the difference between &amp;gt; and &amp;gt;&amp;gt; then?===&lt;br /&gt;
&amp;gt; tells the program to save the output to the target and to &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;overwrite any contents the target might contain&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;. &lt;br /&gt;
&amp;gt;&amp;gt; tells the program to save the output to the target and to &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;add the output to the end of the target preserving previous contents&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To make this clear, say you have a text file named nobelPrizeWinningStudy.txt (why on earth you&amp;#039;d save your nobel prize winning report in a text file is beyond me, but hey why not?). If you do this:&lt;br /&gt;
&lt;br /&gt;
 $ ls -l &amp;gt; nobelPrizeWinningStudy.txt&lt;br /&gt;
&lt;br /&gt;
nobelPrizeWinningStudy.txt will more appropriately be named &amp;quot;list of files from some random place&amp;quot;. Because none of the original nobel prize winning data is left.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Scripting Constructs==&lt;br /&gt;
Normally you don&amp;#039;t just want to list directories and save contents to files, rather you want to take some data and do something &amp;lt;i&amp;gt;useful&amp;lt;/i&amp;gt; with it. To do so, it&amp;#039;s fairly common to want to perform certain tests, such as &amp;quot;if bank account empty, deposit checks. if bank account not empty.....WIDESCREEN TV&amp;quot;&lt;br /&gt;
===The basic form of a scripting===&lt;br /&gt;
This, and virtually all logic constructs consist of one or more of these basic parts: &lt;br /&gt;
*start&lt;br /&gt;
*sufficient condition&lt;br /&gt;
*code (do stuff)&lt;br /&gt;
*close&lt;br /&gt;
==The &amp;#039;if&amp;#039; conditional==&lt;br /&gt;
The if conditional within shell scripting works nicely for taking actions contingent on a particular condition. The basic construct of the if conditional is&lt;br /&gt;
&lt;br /&gt;
 if [ test condition ]; then&lt;br /&gt;
  do some stuff&lt;br /&gt;
 fi&lt;br /&gt;
&lt;br /&gt;
In the above example, the corresponding components are&lt;br /&gt;
*start -&amp;gt; if&lt;br /&gt;
*sufficient condition -&amp;gt; [ some test condition ]&lt;br /&gt;
*code -&amp;gt; do stuff&lt;br /&gt;
*close -&amp;gt; fi&lt;br /&gt;
&lt;br /&gt;
We can test multiple conditions or include a default action by including the elif and else condtions like so&lt;br /&gt;
  if [ test condition ]; then&lt;br /&gt;
    do some stuff&lt;br /&gt;
  elif [ test codition2 ];then&lt;br /&gt;
    do something different&lt;br /&gt;
  else &lt;br /&gt;
    perform default action &lt;br /&gt;
  fi&lt;br /&gt;
&lt;br /&gt;
==The for loop==&lt;br /&gt;
Another very common task is to take perform an action multiple times on multiple inputs. An example would be running several design.fsf files in succession. The for loop provides a perfect mechanism for doing just this.&lt;br /&gt;
&lt;br /&gt;
The basic for loop takes the form&lt;br /&gt;
&lt;br /&gt;
  for i in &amp;lt;sequence of variables&amp;gt;;do&lt;br /&gt;
    perform an action on $i&lt;br /&gt;
  done&lt;br /&gt;
&lt;br /&gt;
The corresponding components are&lt;br /&gt;
*start -&amp;gt; for&lt;br /&gt;
*sufficient condition -&amp;gt; i is assigned subsequent values of &amp;lt;sequence of variables&amp;gt;&lt;br /&gt;
*code -&amp;gt; perform an action on $i&lt;br /&gt;
*close -&amp;gt; done&lt;br /&gt;
&lt;br /&gt;
A specific implementation might look like&lt;br /&gt;
  for i in design1.fsf design2.fsf design3.fsf;do&lt;br /&gt;
    echo &amp;quot;Do something with $i&amp;quot;&lt;br /&gt;
  done&lt;br /&gt;
&lt;br /&gt;
This would produce an output of&lt;br /&gt;
  $ Do something with design1.fsf&lt;br /&gt;
  $ Do something with design2.fsf&lt;br /&gt;
  $ Do something with design3.fsf&lt;br /&gt;
&lt;br /&gt;
You may have noticed that we declare the variable as i within the for loop, though this could be just about any ascii character or sequence of characters (e.g. myVar). We then act on the value of that variable by placing a dollar sign ($) in front.&lt;/div&gt;</summary>
		<author><name>Ccn admin</name></author>
	</entry>
</feed>