Changes

Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

2,114 bytes added, 14:17, 2 March 2021
Using Variables in Shell Scripts
* [https://opensource.com/article/19/8/what-are-environment-variables Environment]
* [https://www.linuxtechi.com/variables-in-shell-scripting/#:~:text=User%20Defined%20Variables%3A,like%20a%20real%20computer%20program. User Defined]
* [http://osr600doc.xinuos.com/en/SDK_tools/_Positional_Parameters.html#:~:text=A%20positional%20parameter%20is%20a,up%20to%20nine%20positional%20parameters. Positional Parameters]Commands/ Techniques
* [http://linuxcommand.org/lc3_man_pages/readh.html read]
* [https://man7.org/linux/man-pages/man1/readonly.1p.html readonly]
* [https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html Command Substitution]
| style="padding-left:15px;"|Control Flow Statements
* [https://en.wikipedia.org/wiki/Control_flow Purpose]
* [https://www.computerhope.com/unix/test.htm test command]
* [https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php#:~:text=If%20statements%20(and%2C%20closely%20related,conditions%20that%20we%20may%20set. if statement]
* [https://www.tutorialspoint.com/unix/if-else-statement.htm if-else statement]
* [https://www.cyberciti.biz/faq/bash-for-loop/#:~:text=A%20'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: https://mariadb.com/kb/en/user-defined-variables/
<br><br>
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.
<br><br>
'''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: https://en.wikipedia.org/wiki/Command_substitution<br><br>
 
''Usage:''
 
<span style="font-family:courier"><b>command1 $(command2)</b><br>or<br><b>command1 `command2`</b></span><br><br>
 
''Examples:''
 
<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.
 
<br><br><br><br><br>
'''if-else statement:'''
<br>
[[Image:if-else.png|thumb|right|300px|Example of how an '''if-else''' statement works.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ 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>
 
''Example:''
 
<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'''
13,420
edits

Navigation menu