Open main menu

CDOT Wiki β

Changes

Shell Scripting

52 bytes added, 12:12, 19 January 2016
Tips to Speed up Scripting
:'''/home/murray.saul/vi-tutorial'''
:'''/home/murray.saul/scripting-1'''
 
== Lab 1 Scripting Content ==
<br><ul><li>'''She-bang line: #!/bin/bash'''<br><br>Shell scripts have evolved of the past 40 years. To avoid running a newer shell script on an older shell, it is recommended to force running the shell script in the correct shell. In order to do this, on the first line at the very beginning of the shell script, you add the '''#!''' ('''# as in "shhhh" - a comment''', and''' ! is referred to as "bang" run a commmand''': in this case, '''run the command: /bin/bash'''). You can issue the Linux command '''which bash''' to get the correct location. If there is no bash shell on that machine, the shell script will not run (as a precaution - the Linux admin will know how to make a fix to the shell script if required).<br><br> </li><li>'''Variables:'''<br><br> There are 3 types of variables that can be used in shell scripting: '''ENVIRONMENT''' (eg. $USER), '''user-defined''' ($varName), and '''positional parameters''' (eg. $1, $2... containing arguments after shell script or by using set command (eg. '''set $(ls)''' ). Using dollar sign ('''$''') in front of variable expands the value assigned.<br><br></li><li>'''Command Substitution:<u>'''<br><br>'''<u>Examples</u>'''<br><br>'''file $(ls)'''<br>'''set $(ls);echo $#;echo $*'''<br>'''echo "hostname: $(hostname)"'''<br><br><li>'''Logic Control Flow Statements:'''<br><br>The '''test''' command can be used to see if a condition is true or false<br>(i.e. test $USER &#61; "root") . The '''$?''' special shell variable stores the result (zero if true, non-zero if false). Square brackets '''[ ]''' can be used to represent the test command with the condition <u>inside</u> the brackets (spaces separating brackets).Can use '''if''' / '''if-else''' / '''if-elif-else''' statements with brackets. The '''exit''' command can be used to terminate the shell script with a false value.<br><br>'''<u>Examples</u>'''<br><br>''if [ $USER &#61; "root" ]''<br>''then''<br>&nbsp;''echo "You must be root" >&amp;2''<br>&nbsp;''exit1''<br>''fi''<br><br># For number comparison: use:<br># -gt,-ge, -lt, -le, -eq, -ne<br><br>''if [ $age -gt 65 ]''<br>''then''<br>&nbsp;''echo "retire"''<br>''else''<br>&nbsp;''echo "don't retire"''<br>''fi''<br><br>''if [ $grade -gt 79 ]''<br>''then''<br>&nbsp;''echo "You get Good Mark"''<br>''elif [ $grade -gt 49 ]''<br>''then''<br>&nbsp;''echo "You pass"''<br>''else''<br>&nbsp;''echo "You fail"''<br>''fi''<br></li></ul>
== Tips to Speed up Scripting ==
* Labs are designed so students can copy and paste scripting contents to their c6host c7host or virtual machines (except for centos3 which is text-based OS)
* You are required to add execute permissions (at least for owner) in order to run the created shell script
* When retrieving other shell scripts via the wget command, set execute permissions and then run the shell script from the current directory
 
 
[[Category:OPS235]]
[[Category:OPS235 Labs]]
221
edits