0% found this document useful (0 votes)
7 views5 pages

Unix Review

This document outlines key areas for practice before Quiz 2 in a Unix programming course. It covers topics such as script structure, debugging, exit codes, permissions, command redirection, conditional statements, control structures, and the use of grep. Each section provides specific commands and examples to reinforce understanding of Unix scripting concepts.

Uploaded by

bobbymarley1893
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Unix Review

This document outlines key areas for practice before Quiz 2 in a Unix programming course. It covers topics such as script structure, debugging, exit codes, permissions, command redirection, conditional statements, control structures, and the use of grep. Each section provides specific commands and examples to reinforce understanding of Unix scripting concepts.

Uploaded by

bobbymarley1893
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Review 2

Here are some areas for practice before Quiz 2

Scripts
First line

 What the first line in a script must be


 What that line means
#! /bin/bash
Called shebang and it specifies what interpreter to use for the script
Debug

 The debug command to display each line as it executes


Bash -x script.sh
Exit codes

 How to see the success/failure exit code from the previous line in a script.
Exit ??
 What exit code is returned from a script by default
Status of last command
 How to return a specific exit code from a script
Exit number
 How to check the exit code immediately after a script has run
Permissions and execution

 Setting permissions to allow a script to be executed


Chmod 757
 Two different ways of executing a script from the command line
Bash script.sh
Bash -x script.sh
Script arguments

 How to see the command line arguments


$?
 How the command line arguments are numbered
Starts at 0 which is the path and then goes up by 1
 What command would you use to print the number of arguments sent to a script?
$#
 What command would you use to list all the script arguments? - $@
 What command would you use to discard the first script argument? What happens to
the other arguments when you do this?
Shift, moves 3  2  1

CS2080-002 Programming with Unix Page 1


Review 2

Variables
 How to assign the output of a command to a variable
Name=$()
 How to append the value of a variable to an existing variable (PATH example in
homework 4)
trade PATH=$PATH:/home/joe:/home/joe/container
path=$secondpath

Variable expansion (parameter expansion)

 Variable expansion, when getting the value of a variable how to:


- return a default value if the variable is not set
- chop a pattern from the front of the variable’s value
- chop a pattern match from the front of back of the variable’s value
 How to make a variable visible in child processes

Command redirection
 How would you use a redirection operator to pass the contents of a file into a
command?
>>
 How would you redirect the error output of a command so that it goes to a file?
 How would your redirect the error and standard outputs of a command to different
files?
 Err.txt
 How would your redirect the error and standard outputs of a command to different
files?
 How do you throw away everything output by a command?
 How do you redirect the output of a command to several files?
 How would you use process redirection (process substitution) to redirect the output of
a series of commands to a while loop?

Conditional statements

file1=filename

string1=hello

string2=

string3=goodbye

CS2080-002 Programming with Unix Page 2


Review 2

num1=1

num2=200

if condition ; then

echo “ condition true”

fi

Condition expressions:
Given the pseudo-code above, write the condition expressions to test for these states:
(Include the appropriate brackets or parentheses.)
 file1 is a directory
if[[ -d file1 ]]
 file1 exists
if [[ -e file1 ]]
 file1 is not empty
if [[ -I file1 ]]
 string1 equals string2
if [[ string1 -eq string2 ]]
 string1 comes before string1
if [[
 string2 is empty
if [[ -z strigng2 ]]
 string3 is not empty
string [[ -n string3 ]]
 num1 is greater than num2
if (( num1 > num2 ))
 num1 is not zero
if (( num1 != 0 ))

if-free conditionals.
 Repeat the tests above using if-free conditionals using && or ||

Control structures.
Give the keywords, punctation, and layout of these structures
 while-loop

CS2080-002 Programming with Unix Page 3


Review 2

while statement; do; commands; done


 case statements
case variable in
pattern1) command ;;
 if statements
if statement; do
 for-loops
for variable in element; do commands;done

For-loops.
Write for-loops that cycle through:
 all the files in a directory
 all the numbers between 10 and 15
 all the even numbers between 8 and 20
 the list of names Alfie Brando Charlie Della Eka
 all the lines in a file

C-like for loops


 Write a for-loop that uses the Java- or C-like structure: for( initialize; test; increment
)
For (( I = 0; I < 5; i++))

While-loops
 Write a while loop that reads its input line by line from a file
 Write a while loop that reads its input field by field from a file
 Write a while loop that reads its input line by line from a here-document
 Write a while loop that redirects its output to a file
 Write a for loop that re-directs its output to a file
 Write a while loop reads its input from stdin

ls and file globs


 How do you get ls to search for multiple file name patterns using globs?

grep
 What should you never do with ls and grep?
Put them together
CS2080-002 Programming with Unix Page 4
Review 2

 How to use a file glob/wildcard pattern when telling grep what files to search.
 How to put multiple glob patterns in the same grep command line
 What are the grep options for:

- Count -- return the number of matches found


- Search for multiple patterns
- Do not show file names in output
- Ignore case when matching search terms.
- Caution, matches ALL combinations of case.
- List the files that include the search term, not the actual lines of text.
- Only matching.
- Show only the part of the matching line that matches the pattern
- Quiet, silent (used with pipes), outputs:
- 0 if match found
- 1 if no match
- Recursive, search the current directory and its children
- Search for lines that do not contain the search term.
- Match whole words only
-
 How would you count the total number of times the word “hello” appears in files in
the /home/joe directory whose name does not start with the number 9 and ends
with .txt?

ACL's
 How do you read and set access control lists?

Num_matches =$( grep -w $str_1 $dir_2/*.txt | wc -w

CS2080-002 Programming with Unix Page 5

You might also like