CNIT 340 Exam 2
What is the character used for comments?
#
What is the first line of a bash script?
#!/bin/bash
What is the best way to enable debugging?
#!/bin/bash -xv
Arithmetic Substitution Syntax
$(( <expression> )) - Solutions will be integers (1.9 -> 1)
Command Substitution Syntax
$(<command>) and `<command>` Ex. DATE=$(date %s)
How do you access the number of elements in an array?
${#<name>[@]}
How do you access all values in an array?
${<name>[*]} - Returns All separated ${<name>[@]} - Returns elements as are for ex. Returns 4 elements: Walter Roger John George Douglas
How do you access a single value in an array?
${<name>[index]} Ex. ${NAME[0]}
What is the X11 Config Location?
/etc/X11
What is the PAM Config Location for X11?
/etc/pam.d/xdm
What is the return code for True?
0
Function Syntax
<function name> () { <code block> } OR function <function name> { <code block> }
Array Variable Format
<name>[index]=<value> Ex. NAME[0]=Walter
What must user defined variables start with?
A letter or underscore
User-defined variables best practice
ALL CAPS
Display Manager
Auth users & initialize environment
Array Variables
Can hold multiple elements at a time
Scalar Variables
Can only hold one element (value) at a time
What is ${<name>:?<message>} used for?
Catching errors if variable is unset.
What is another way to enable debugging?
DEBUG=true ./script.bash
Case syntax
case ITEM in pattern 1) <code block 1> ;; pattern 2) <code block 2> ;; esac
What command must you use to convert a windows text file to a unix text file?
dos2unix
Window Manager
move, resize, minimize, and maximize windows
What is the REGEX Result of: /[^0-9]/
negates - matches non-numbers The caret has a different meaning inside of brackets.
what does debugging option -n stand for?
no execution, useful for checking syntax errors
True or False: \ only disables the next character
True
How do you negate a character set?
With ! Ex. ls [!a-z]* Any filename that does not start with a lowercase letter
How do you access a variable?
With $, ex. $name
What symbols are used for quoting?
\ , ' , "
How do you save matches in registers?
\(regex\) saves matches in registers
What is the REGEX Result of: /^\(.\).*\1$/
^ Matches the beginning of the line \(.\) Matches the first character and stores it in register 1 .* Matches all the characters in between the first and last \1$ Matches the last character with contents of register 1
What is the REGEX Result of: /\.$/
matches "." if it is the last character in a line
What is the REGEX Result of: /^The/
matches "The" if it is the first chars in a line
What is the REGEX Result of: /The/
matches anything that starts with the
What is the REGEX Result of: /r./
matches r followed by any character
What is the REGEX Result of: /[A-Za-z]\{4,7\}/ Hint: \{min,max\}
matches strings of only upper and lower case alpha between 4 and 7 letters
What is the REGEX Result of: /X*/
matches zero or more of the character "X"
BASH Select loop syntax
select VAR in item1 item2 item3 do <Code Block> done OUTPUT A MENU: 1)item1 2)item2 3)item3
BASH Sequence syntax
seq <first> <step> <last> Ex. For VARIABLE in $(seq 1 2 20)
How do you debug certain portions of code?
set -n; <statement>; set +n
How do you tunnel X traffic through SSH?
ssh -X remote host
How do you turn off tunneling X traffic through SSH?
ssh -x remote host
How do you start an X server?
startx ( a wrapper of xinit)
How do you set a local variable in a function?
typeset VAR1=312
How do you delete a variable?
unset <name>
BASH until loop syntax
until <condition>; do <code block>; done
BASH while loop syntax
while <condition>; do <code block>; done
Types of BASH loops
while, until, for, select
X Client Authentication Types
Host Based (xhost) & magic cookie authentication
What is ${<name>:+<value>} used for?
If <name> is set, then the expression is replaced by <value> Ex. echo ${DEBUG:+"Program running in debug mode"} If DEBUG is set, then it will print to the screen
What is Filename substitution?
Expanding a string with wildcards (*, ?, [character set])
What is the difference between: FILES=$(ls) vs. FILES=($(ls))
FILES=$(ls) creates a scalar variable: $FILES = one two FILES=($(ls)) creates an array: $FILES[0] = one $FILES[1] = two
True or False: Compound Tests && (and), || (or), > and < work in Single Bracket tests
False
True or False: Array Variable Indices can be positive and negative.
False, only positive
[ -e file ]
File exists
[ -d file ]
File exists and is a directory
[ -f file ]
File exists and is a regular file
[ -s file ]
File exists and is of size greater than zero
[ file1 -nt file2 ]
File1 is newer than file2
[ file1 -ot file2 ]
File1 is older than file2
What are the types of Substitution?
Filename, Variable, Command, Arithmetic substitution
C Style Construct Example
For ((I=1; I<=10; I++))
X-Client
GUI applications
What does it mean if a variable starts with a number?
It is reserved (can use but not change)
What are the two types of variables?
Scalar & Array
What does ? do?
Matches any character one time
What does * do?
Matches any character zero or more times
What does [character set] do?
Matches the characters stated one time
What is the output of: unset NAME DIFFERENT=${NAME:-Walter} echo NAME $NAME, DIFFERENT $DIFFERENT
NAME , DIFFERENT Walter
What is the output of: unset NAME DIFFERENT=${NAME:=Walter} echo NAME $NAME, DIFFERENT $DIFFERENT
NAME Walter, DIFFERENT Walter
How do you make a variable readonly?
NAME=Walter readonly NAME
What is this equivalent to? NAME=(Walter Roger John "George Douglas")
NAME[0]=Walter NAME[1]=Roger NAME[2]=John NAME[3]="George Douglas"
Can read only variables be deleted?
NO
Can you return Strings in a function?
No
Is the following a valid statement? NAME=Walter Smith
No, assigned variables with spaces must be put in quotes: NAME="Walter Smith"
Are there blanks between 2 to 100 in this statement: NAME[101]=John
No, values are only assigned to the corresponding indices
X features
Not part of OS, Architecture Independent, Network Transparent, Policy Free
What expression matching does BASH support?
POSIX and PERL
X-server
Presents the output of an application to the display
What does debugging option -x stand for?
Shell tracing - display commands and args executed
What type of file is a shell script?
Text File
True or False: In Double Quoting $ and ` are still treated as meta-characters
True
True or False: Single Bracket keywords work on Double Bracket tests
True
BASH for loop syntax
for VARIABLE in <set> do <command block> done
full if elif else statement example
if <statement> then <code block 1> elif <statement> then <code block 2> else <code block 3> fi
if then syntax
if <statement>; then action; fi OR if <statement> then <code> fi