Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

3,991 bytes added, 09:29, 29 July 2020
INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS
# Use the '''more''' command to view the contents of the text file called '''for-1.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more for-1.bash</span>)<br><br>As you should have noticed from ''tutorial 10'' that the '''for''' loop can use a list.<br>You can also use the for loop with positional parameters stored as arguments from an executed shell script.<br>We will revisit this now.<br><br>
# Use the more command to view the text file called '''for-2.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more for-2.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>
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-2.bash 10 9 8 7 6 5 4 3 2 1</span><br><br>You should notice the script looped for each argument following the shell script.<br><br>You can also use the for loop with a list using '''command substitution''' - this is an effective technique to loop within a shell script.<br><br># First, you need to learn how to use command substitution to store arguments as positional parameters.<br>Issue the following linux command to set positional parameters in your current shell:<br><span style="color:blue;font-weight:bold;font-family:courier;">set apples oranges bananas pears</span><br><br># Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $#</span><br><br>What do you notice?<br><br># Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $*</span><br><br>What do you notice?<br><br>These positional parameters could be used with a for loop. To simplify things, let's create another shell script that uses command substitution and a for loop.<br><br># Use a text editor like vi or nano to create the text file called '''for-2.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-3.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># Enter the following lines in your shell script:<br><span style="font-family:courier;">#!/bin/bash<br>clear<br>set 10 9 8 7 6 5 4 3 2 1<br>for x<br>do<br>&nbsp;&nbsp;&nbsp;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 ''':wx''' 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 for-23.bash</span><br><br># Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-23.bash 10 9 8 7 6 5 4 3 2 1</span><br><br>What do you notice? How does this the result differ from the shell script called for-2.bash. Why?<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>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;">#!/bin/bash<br>clear<br>set $(ls)<br>echo Here are files in my current directory:"<br>echo<br>for x<br>do<br>&nbsp;&nbsp;&nbsp;echo " &nbsp;&nbsp;&nbsp;$x"<br>done<br></span><br><br># Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' 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 for-4.bash</span><br><br># Run your shell scriptby issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-4.bash</span><br><br>What do you notice?<br><br>You will learn We can save a line in a couple of weeks more examples of our shell script by using command substitution in the for loop statementsusing a list. Let's demonstration this in another shell script.<br><br># After 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><br>If you complete are using the Review Questions sections nano text editor, refer to get additional practicenotes 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;">#!/bin/bash<br>clear<br>echo Here are files in my current directory:"<br>echo<br>for x in $(ls)<br>do<br>&nbsp;&nbsp;&nbsp;echo " &nbsp;&nbsp;&nbsp;$x"<br>done<br></span><br><br># Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then work on type ''':wx''' followed by '''ENTER''').<br><br># Issue the following linux command to add execute permissions for yourshell script:<br>online assignment 3<span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x for-5.bash</span><br><br># Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-5.bash</span><br><br>What do you notice? Does the output for this shell script differ than for-4.bash? Why?<br><br>The last thing in this section is to introduce you to '''error-checking'''.<br><br> # x      
In the next investigation, you will ...
# x<br>
 
 
# After you complete the Review Questions sections to get additional practice, then work on your<br>online assignment 3.
<br><br>
= LINUX PRACTICE QUESTIONS =
13,420
edits

Navigation menu