Open main menu

CDOT Wiki β

Changes

OPS235 Lab 2 - CentOS7

2,339 bytes added, 07:59, 2 April 2015
no edit summary
==Part 1: Automating Routine Tasks (Shell Scripting)==
[[Image{{Admon/tip|Bash Shell Scripting Tips:|<br><ul><li>'''She-bang line on first line of shell script:'''<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:lab2_text_1/bin/bash''').png|thumb|right|275px|When executed 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>. </li><li>'''Variables:'''<br><br> There are 3 types of variables that can be used in shell scripting: '''ENVIRONMENT''' (eg. $USER), '''user-defined'''root($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:'''<br><br>A very useful trick to take output from a command to be used as an argument for another command. Examples include:<br>'''file $(ls)''sudo'<br>'''set $(ls);echo $#;echo $*'''<br>'''echo "hostname: $(hostname)"'''<br><br><li>'''Logic Control Flow Statements:'''<br><br>The '''test''' commandcan 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, provides VM status and opportunity 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'start''/ ' VMs in ''if-else''' / '''if-elif-else''' statements with brackets. The '''exit''' command can be used to terminate the shell script with a textfalse 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>'''if [ $age -based interfacegt 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>}}|} 
We continue the discussion of using Bash Shell Scripts to help automate routine tasks for the Linux system administrator. In lab1, you were exposed to downloading and running Bash Shell Scripts for demonstration purposes. Before showing you demos for this lab, you should learn a few more "shell scripting tricks" in case you need to create your own set of tools for task automation.
13,420
edits