Changes

Jump to: navigation, search

OPS235 Lab 1 - CentOS7 - VMware

25 bytes added, 07:59, 22 May 2016
no edit summary
|- valign="top"
|
{{Admon/note|Bash Shell Reference Guide:|<br>'''<u>She-bang Line</u>'''<ul><li>Forces shell script to run in a specific Shell</li><li>Some shell syntax not backward compatible</li><li>'''#!''' must be at beginning of first line of shell script</li><li>Example: '''#!/bin/bash'''<br><br></li></ul>'''<u>Variables</u>'''<br><br>&nbsp; &nbsp;'''Environment'''<blockquote><ul><li>System-wide or "global" variable (usually UPPERCASE)</li><li>Can view with command: '''set &#124; more'''</li><li>'''$''' in front to expand variable to value<li>Examples: '''USER''', '''PATH''', '''HOME''', '''SHELL'''<br><br></li></ul></blockquote> '''User-defined''' <ul><li>Variable created by user (command line, scripting)</li><li>Examples:<br>''myVar&#61;"my value"; readonly myVar; export myVar''<br>''read -p "enter value: " myVar''<br><br></li></ul>'''Positional parameters'''<ul><li>Assign values with set command or shell script arguments</li><li>These variables are numbered (eg. $1, $2 ... $10}</li><li>Special parameters: $*, $@, $#, $$, $?<br><br></li></ul>'''<u>Command Substitution</u>'''<ul><li>Useful method to expand output from a command to be used as an argument for another command.</li><li>Examples:<br>''file $(ls)''<br>''set $(ls);echo $#;echo $*''<br>''echo "hostname: $(hostname)"''<br><br></li></ul>'''<u>Logic Control Flow Statements</u>'''<ul><li>'''$?''' variable true (0) if command runs; otherwise is false (non-zero)</li>Example:<br><br>''if echo $myVar &#124; grep "match"''<br>''then''<br>''echo "Match"''<br>''fi''<br></li><li>Square 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>}}
|}
13,420
edits

Navigation menu