170 likes | 287 Views
Red Hat Academy Workbook 11. Supplements. Chapter 1: Advanced Shell Scripting . The read command reads input from stdin . The test keyword allows you to make logical tests for branching, looping, etc. Conditional statements in bash are available as if…then…[else…]fi .
E N D
Red Hat AcademyWorkbook 11 Supplements
Chapter 1: Advanced Shell Scripting • The read command reads input from stdin. • The test keyword allows you to make logical tests for branching, looping, etc. • Conditional statements in bash are available as if…then…[else…]fi. • Loops can be implemented using for…in…do…done. • Command arguments in bash scripts are available as $1, $2, $3, etc.
The read command in bash • The read command reads one line of text from stdin. • Format: read var1 [var2 …]
Testing conditions • The test keyword can be used to test certain conditions in bash: test condition • Appears usually in the context of an if, while, or for statement.
Branches in bash • Branching in bash: If conditionthencommand(s) else command(s)fi
The for loop in bash • The for loop can be expressed like this: for VICTIM in list do statement(s) done • This will loop through every item in list, with $VICTIM representing each one in turn.
for loop example • Takes the list of files in the current directory, and prints them out with a joyful comment. • Note the `ls`, and the reference to the $PWD environment variable.
Another loop: while • For loops are good for when loops are bounded, i.e. you know how many times to loop. • For unbounded loops, use while instead: while condition do statement(s) done
Accessing command line arguments in bash • The command line can be accessed with the $0 shell variable. • The number of arguments included is represented by the $# shell variable. • Each argument is represented by $1, $2, $3, etc. • The shift command forces each argument to shift down by one place. This is useful when the number of arguments can vary, or is otherwise unknown.
Accessing command line arguments in bash • Sample script prints out the entire command line, then each argument in turn: #!/bin/bash #print out command line:echo $0 #print out the number of arguments: echo Number of arguments: $# #print out each argument, one per line: i=0 while test $1 do i=$(($1+1)) echo argument \#i: $1 shift done
Chapter 2: Character Encoding and Internationalization Where is it???
Chapter 3: The RPM Package Manager • The rpm command is used to add or remove software from your system. • You must have root privileges to add and remove software with rpm. • Anyone can query installed packages, or package files. • Installed packages are maintained in the rpm database. • Queries can be made of the database with the –q switch.