<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matt [Danger] &#187; Bash</title>
	<atom:link href="http://mattdanger.net/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://mattdanger.net</link>
	<description>One step closer to world domination</description>
	<lastBuildDate>Fri, 02 Dec 2011 18:20:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Bash Examples</title>
		<link>http://mattdanger.net/2008/11/bash-examples/</link>
		<comments>http://mattdanger.net/2008/11/bash-examples/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 23:35:14 +0000</pubDate>
		<dc:creator>Matt Danger</dc:creator>
				<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://mattdanger.net/?p=8</guid>
		<description><![CDATA[A while ago a friend was taking an entry level Unix programming class and asked me to help him study. I came up with some sample problems for him. Here are my solutions: The first is a program that will allow a user to input as many numbers as the user wants (999 as the&#8230;]]></description>
			<content:encoded><![CDATA[<p>A while ago a friend was taking an entry level Unix programming class and asked me to help him study. I came up with some sample problems for him. Here are my solutions:</p>
<p>The first is a program that will allow a user to input as many numbers as the user wants (999 as the choice that ends the user input). The program will then output the highest number, the lowest number, the sum of all the numbers, and the average number.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #007800;">COUNT</span>=<span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Enter a number (999 to quit): &quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> NUM; <span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #666666; font-style: italic;"># Set initials</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$COUNT</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #007800;">HIGHEST</span>=<span style="color: #007800;">$NUM</span>
    <span style="color: #007800;">LOWEST</span>=<span style="color: #007800;">$NUM</span>
    <span style="color: #007800;">SUM</span>=<span style="color: #007800;">$NUM</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># If 999 is entered, break out</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$NUM</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">999</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">break</span>
  <span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #666666; font-style: italic;"># See if inputted number is the highest number</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$NUM</span> <span style="color: #660033;">-gt</span> <span style="color: #007800;">$HIGHEST</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
      <span style="color: #007800;">HIGHEST</span>=<span style="color: #007800;">$NUM</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># See if inputted number is the lowest number</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$NUM</span> <span style="color: #660033;">-lt</span> <span style="color: #007800;">$LOWEST</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
      <span style="color: #007800;">LOWEST</span>=<span style="color: #007800;">$NUM</span>;
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Calculate sum</span>
    <span style="color: #007800;">SUM</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$SUM</span>+<span style="color: #007800;">$NUM</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Enter another number (999 to quit): &quot;</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
  <span style="color: #007800;">COUNT</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$COUNT</span>+<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #007800;">AVERAGE</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$SUM</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$COUNT</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Highest number: <span style="color: #007800;">$HIGHEST</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Lowest number: <span style="color: #007800;">$LOWEST</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Sum of the numbers: <span style="color: #007800;">$SUM</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Average of the numbers: <span style="color: #007800;">$AVERAGE</span>&quot;</span></pre></div></div>

<p>The second program is a number guessing game. The computer picks a number from 0-60 then allows the user to guess at the number until the user gets it right. The program gives the user a hint on whether they should guess a higher or lower number.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash </span>
<span style="color: #007800;">RAN</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>RANDOM<span style="color: #000000; font-weight: bold;">%</span>60<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">COUNTER</span>=<span style="color: #000000;">1</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;I've guessed a number between 0-60, what is it? &quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> GUESS; <span style="color: #000000; font-weight: bold;">do</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$GUESS</span> <span style="color: #660033;">-eq</span> <span style="color: #007800;">$RAN</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">break</span>;
  <span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$GUESS</span> <span style="color: #660033;">-lt</span> <span style="color: #007800;">$RAN</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
      <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Nope, try a higher number: &quot;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
      <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Nope, try a lower number: &quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>	
  <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #007800;">COUNTER</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>COUNTER+<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Yep, good job! It took you <span style="color: #007800;">$COUNTER</span> tries to guess correctly.&quot;</span></pre></div></div>

<p>The third program allows a user to type in a number, then the program will either count up to the number provided by the user or count down from that number. In addition, a menu lets the user choose which counter they wish to use. In both cases that counter will either count down to 0, or start counting from 0.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash </span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Enter a number: &quot;</span>; <span style="color: #c20cb9; font-weight: bold;">read</span> NUM
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Enter U to count up to <span style="color: #007800;">$NUM</span> and D to count down to 0: &quot;</span>; <span style="color: #c20cb9; font-weight: bold;">read</span> CHOICE
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$CHOICE</span> <span style="color: #000000; font-weight: bold;">in</span>
  <span style="color: #ff0000;">'u'</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #ff0000;">'U'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> 
    <span style="color: #007800;">COUNTER</span>=<span style="color: #000000;">0</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$COUNTER</span> <span style="color: #660033;">-le</span> <span style="color: #007800;">$NUM</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
      <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$COUNTER</span>
      <span style="color: #007800;">COUNTER</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$COUNTER</span>+<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #ff0000;">'d'</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #ff0000;">'D'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$NUM</span> <span style="color: #660033;">-ge</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
      <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$NUM</span>;
      <span style="color: #007800;">NUM</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$NUM</span>-<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Error: You entered an invalid argument.&quot;</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></div></div>

<p>The last program asks the user if they want to add, subtract, multiply or divide two user inputted numbers. Then the program outputs the sum of number 1 and number 2, the difference of number 1 and number 2, the product of number 1 and number 2, and the quotient of number 1 and number 2</p>
<p>This program will run until the user wants to quit. Also, if the user puts in a 0 as the second number, we let the user know that if they choose divide that a division by zero error will occur.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span>; <span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Please enter two numbers (type 'q' to quit): &quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Number 1: &quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">read</span> NUM1
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$NUM1</span> == <span style="color: #ff0000;">&quot;q&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #7a0874; font-weight: bold;">exit</span>; <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Number 2: &quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">read</span> NUM2
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Enter (a) to add, (s) to subtract, (m) to multiply, or (d) to divide these numbers: &quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">read</span> OPERATION
&nbsp;
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$OPERATION</span> <span style="color: #000000; font-weight: bold;">in</span>
    a<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;The sum of <span style="color: #007800;">$NUM1</span> and <span style="color: #007800;">$NUM2</span> is: <span style="color: #007800;">$(($NUM1+$NUM2)</span>)&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
    s<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;The difference of <span style="color: #007800;">$NUM1</span> and <span style="color: #007800;">$NUM2</span> is: <span style="color: #007800;">$(($NUM1-$NUM2)</span>)&quot;</span><span style="color: #000000; font-weight: bold;">;;</span>
    m<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;The product of <span style="color: #007800;">$NUM1</span> and <span style="color: #007800;">$NUM2</span> is: <span style="color: #007800;">$(($NUM1*$NUM2)</span>)&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
    d<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$NUM2</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You can't divide by 0, a division by zero error will occur.&quot;</span>
      <span style="color: #000000; font-weight: bold;">else</span> 			
	    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;The quotient of <span style="color: #007800;">$NUM1</span> and <span style="color: #007800;">$NUM2</span> is: <span style="color: #007800;">$(($NUM1/$NUM2)</span>)&quot;</span>
      <span style="color: #000000; font-weight: bold;">fi</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #000000; font-weight: bold;">esac</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mattdanger.net/2008/11/bash-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

