13,420
edits
Changes
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>'''Environment'''<ul><li>System-wide or "global" variable (usually UPPERCASE)</li><li>Can view with command: '''set | more'''</li><li>'''$ ''' in front of to expand variable expands to a value<li>Examples: '''USER''', '''PATH''', '''HOME''', '''SHELL'''<br><br></li></ul> '''userUser-defined''' <ul></li>Variable created by user ($varNamecommand line, scripting), and </li><li>Examples:<br>'''myVar="my value"'''; '''readonly myVar'''<br>'''read -p "enter value: myVar'''</li></ul>'''positional parameters''' <ul><li> (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)'''<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 = "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 = "root" ]''<br>''then''<br> ''echo "You must be root" >&2''<br> ''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> ''echo "retire"''<br>''else''<br> ''echo "don't retire"''<br>''fi''<br><br>''if [ $grade -gt 79 ]''<br>''then''<br> ''echo "You get Good Mark"''<br>''elif [ $grade -gt 49 ]''<br>''then''<br> ''echo "You pass"''<br>''else''<br> ''echo "You fail"''<br>''fi''<br></li></ul>}}
|}