13,420
edits
Changes
no edit summary
# Use a text editor like vi or nano to create the text file called '''if-4.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-4.bash</span>)<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold">#!/bin/bash<br>clear<br>read -p "Enter the first number: " num1<br>read -p "Enter the second number: " num2<br>if [ $num1 -gt $num2 ]<br>then<br> echo "The first number is greater than the second number."<br>elif [ $num1 -lt $num2 ]<br>then<br> echo "The first number is less than the second number."<br>else<br> echo "The first number is equal to the second number."<br>fi</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>
# Issue the following linux command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x if-4.bash</span><br><br>
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./if-4.bash</span><br><br>Try running the script several times with numbers different and equal to each other<br>to confirm that the shell script works correctly.<br><br>A <u>classic</u> shell script to demonstrate many different paths or actions to take depending of multiple testing<br>using an '''if-elif-else''' statement would be a mark to letter grade converter.<br><br>
# Use a text editor like vi or nano to create the text file called '''if-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-5.bash</span>)<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>clear<br>read -p "Enter a mark (0-100): " mark<br>if [ $mark -ge 80 ]<br>then<br> echo "You receive an A grade."<br>elif [ $mark -ge 70 ]<br>then<br> echo "The receive a B grade."<br>elif [ $mark -ge 60 ]<br>then<br> echo "The receive a C grade."<br>elif [ $mark -ge 50 ]<br>then<br> echo "The receive a D grade."<br>else<br> echo "You receive an F grade."<br>fi</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>
# Issue the following linux command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x if-5.bash</span><br><br>
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./if-5.bash</span><br><br>What do you notice? Run several times to confirm that the shell script runs correctly for all mark (grade) categories.<br><br>
# Use a text editor like vi or nano to create the text file called '''for-3.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-3.bash</span>)<br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>clear<br>set 10 9 8 7 6 5 4 3 2 1<br>for x<br>do<br> echo $x<br>done<br>echo "blast-off!"</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>
# '''Add execute permissions''' for the owner of this script and '''run this Bash shell script'''.<br><br>What do you notice?<br><br>Let's create another shell script to '''run a loop for each file''' that is contained in your current directory using '''command substitution'''.<br><br>
# Use a text editor like vi or nano to create the text file called '''for-4.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-4.bash</span>)<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>clear<br>set $(ls)<br>echo Here are files in my current directory:"<br>echo<br>for x<br>do<br> echo " $x"<br>done</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>
# '''Add execute permissions''' and '''run''' this Bash shell script.<br><br>What do you notice?<br><br>We can reduce a line in our shell script by using '''command substitution''' in the for loop instead of using the '''set''' command.<br>Let's demonstrate this in another shell script.<br><br>
# Use a text editor like vi or nano to create the text file called '''for-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-5.bash</span>)<br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>clear<br>echo "Here are files in my current directory:"<br>echo<br>for x in $(ls)<br>do<br> echo " $x"<br>done<br></span><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>
# '''Add execute permissions''' for this shell script and '''run Bash shell script'''<br>What do you notice? Does the output for this shell script differ than '''for-4.bash'''? Why?<br><br>We now want to introduce you to the use of '''error-checking'''.<br><br>
# Use the '''more''' command to view the text file called '''for-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more for-5.bash</span>)<br><br>Take a few moments to re-familiarize yourself with this shell script<br><br>
# Use a text editor like vi or nano to edit the text file called '''for-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-5.bash</span>)<br><br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br>
# Add the following lines in your shell script <u>AFTER</u> the read statement to prompt the user for a mark:<br><span style="font-family:courier;font-weight:bold;">while ! echo $mark | egrep "^[0-9]{1,}$" > /dev/null 2> /dev/null<br>do<br> read -p "Not a valid number. Enter a mark (0-100): " mark<br>done</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>
# Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./for-5.bash</span><br><br>
# When prompted, enter a letter instead of a number. What happens?<br>Does the shell script allow you to enter an invalid grade like '''200''' or '''-6'''?<br><br>Let's add an additional error-checking loop to force the user to enter a number between 0 and 100.<br><br>
# Use a text editor like vi or nano to edit the text file called '''for-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-5.bash</span>)<br><br>
# Add the following lines in your shell script <u>AFTER</u> the PREVIOUSLY ADDED<br>error-checking code block to force the user to enter a valid number:<br><span style="font-family:courier;font-weight:bold;">while [ $mark -lt 0 ] || [ $mark -gt 100 ]<br>do<br> read -p "Invalid number range. Enter a mark (0-100): " mark<br>done</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>
# Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./for-5.bash</span><br><br>
# When prompted, enter a letter instead of a number. What happens?<br>Does the shell script allow you to enter an invalid grade like '''200''' or '''-6'''?<br><br>Let's reinforce '''math operations''' in a shell script (that you learned in '''tutorial 10''') and then incorporate math operations within a loop.<br><br>
# Use a text editor like vi or nano to create the text file called '''for-6.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-6.bash</span>)<br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br>
# Enter the following lines in your shell script:<br><span style="font-weight:bold;font-family:courier;"><br>#!/bin/bash<br>value=1<br>while [ $value -le 5 ]<br>do<br> echo "$value"<br> value=value+1<br>done<br></span><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>
# Set execute permissions for this shell script and run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-6.bash</span><br><br>What do you notice? You should have noticed an '''error message'''.<br><br>
# To demonstrate what went wrong, <u>issue</u> the following '''commands''':<br><br><span style="color:blue;font-weight:bold;font-family:courier;">num1=5;num2=10<br>result=$num1+$num2<br>echo $result<br><br></span>Notice that the user-defined variable stores the text "'''10+5'''" which is <u>NOT</u> the expected result of adding the number 10 and 5.<br><br>As you may recall in class, we need to convert a number stored as text into a '''binary number''' for calculations (in this case, advance the value by 1 for each loop). We can accomplish this by using the math construct '''(( ))'''<br><br>
# Use a text editor like vi or nano to edit the text file called '''for-6.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-6.bash</span>)<br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br>
# Edit '''line 6''' and change to the following:<br><span style="font-family:courier;font-weight:bold;">((value=value+1))</span><br><br>'''Note:''' For those familiar with other programming languages, you can achieve the same results by using: '''((value++))'''<br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>
# '''Add execute permissions''' for this file and '''run this Bash shell script'''.<br>What do you notice? <br><br>
# Issue the following to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/myscripts/week12-check-1</span><br><br>
# Use a text editor like vi or nano to create the text file called '''~/.bashrc''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi ~/.bashrc</span>)<br><br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br>
# Enter the following lines in your shell script (the symbol "<span style="font-family:courier;font-weight:bold;">[</span>" is the open square bracket symbol):<br><span style="font-family:courier;font-weight:bold;">clear<br>echo -e -n "\e[0;34m"<br>echo "Last Time Logged in (for security):"<br>echo<br>lastlog -u $USER<br>echo<br>echo -e -n "\e[m"</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>
# You can test run the startup file without exiting and re-entering your Bash shell environment.<br>Issue the following:<br><span style="color:blue;font-weight:bold;font-family:courier;">. ~/.bashrc</span><br><br>What do you notice?<br><br>
# Exit your current Bash shell session.<br><br>