Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

2,114 bytes added, 2 March
Using Variables in Shell Scripts
* [ Environment]
* [,like%20a%20real%20computer%20program. User Defined]
* [,up%20to%20nine%20positional%20parameters. Positional Parameters]Commands/ Techniques
* [ read]
* [ readonly]
* [ Command Substitution]
| style="padding-left:15px;"|Control Flow Statements
* [ Purpose]
* [ test command]
* [,conditions%20that%20we%20may%20set. if statement]
* [ if-else statement]
* ['for%20loop'%20is%20a,files%20using%20a%20for%20loop. for loop]
''<b>User-defined variables</b> are variables which can be '''created by the user''' and exist in the session. This means that no one can access user-defined variables that have been set by another user,<br>and when the session is closed these variables expire.''<br>Reference:
Data can be stored and removed within a variable using an '''equal sign'''.<br><br>The '''read''' command can be used to prompt the user to enter data into a variable.<br>Refer to the diagram on the right-side to see how user-defined variables are assigned data.
'''Positional Parameters and Special Parameters'''
Refer to the diagram to the right for examples using positional and special parameters.
'''Command Substitution:'''
[[Image:for-command-substitution.png|thumb|right|300px|Example of how a '''for loop with command substitution''' works.]]
<i>'''Command substitution''' is a facility that allows a command<br>to be run and its output to be pasted back on the command line as arguments to another command.</i><br>Reference:<br><br>
<span style="font-family:courier"><b>command1 $(command2)</b><br>or<br><b>command1 `command2`</b></span><br><br>
<span style="font-family:courier;font-weight:bold">file $(ls)<br>mail -s "message" $(cat email-list.txt) < message.txt<br>echo "The current directory is $(pwd)"<br>echo "The current hostname is $(hostname)"<br>echo "The date is: $(date +'%A %B %d, %Y')"<br>
===Using Control Flow Statements in Shell Scripts===
Refer to the diagram immediately to the right for using the '''if logic statement''' with the '''test''' command.
'''if-else statement:'''
[[Image:if-else.png|thumb|right|300px|Example of how an '''if-else''' statement works.<br>(Image licensed under [ cc])]]
Unlike using only an ''if'' statement, an '''if-else''' statement take '''two different sets of actions'''<br>based on the results of the test condition.<br><br>''How it Works:''<br>When the test condition returns a '''TRUE''' value, then the Linux Commands between<br>'''then''' and '''else''' statements are executed.<br>If the test returns a '''FALSE''' value, then the the Linux Commands between<br>the '''else''' and '''fi''' statements are executed.<br><br>
<span style="font-family:courier;font-weight:bold;">num1=5<br>num2=10<br>if test $num1 –lt $num2<br>then<br> &nbsp;&nbsp;&nbsp;echo “Less Than”<br>else<br>echo &nbsp;&nbsp;&nbsp;“Greater Than or Equal to”<br>fi</span><br><br>
'''Loop Statements'''

Navigation menu