Unix Operating System

Note: This lecture is summarized from the "Unix Operating System and Shell Programming Class" by Dr. Chavalit SRISATHAPORNPHAT, Department of Computer Science, Faculty of Science, Kasetsart University, Thailand.

Basic Shell Commands

Redirection

> (redirection) | 1>... (redirection stdout) 
| 2>... (redirection stderr) 
wc < file1.txt (redirection input)
ls -l file1 noFile 1>out.txt 2>&1 (Redirecting to one file)

TEE

ls -l file99 1>/dev/null 2>&1 // ส่งstdin stderr 
เข้าใน trash /dev/null

Prevent overwriting

set -o noclobber

Shell variables

Stored data -> variable=value
Reference -> $variable
Unset -> unset variable
Display -> echo $variable, set

File System & Permissions

Unix File Types

- : Regular file
d : Directory
l : Link
c : Special file
s : Socket
p : Named pipe
b : Block device

Path

/etc/file1
../usr/staff/joan/file3

Directory Contents

ln -s <existingFile> <newLink>

ln <existingFile> <newLink>

File and Directory Permission

chmod [-R] <modes> <file/directories>

umask 022 (default on bash)

// Calculation 
		Directory permission : 777 - <umask - 222> = 755 
		File permission : 666 - <umask - 222> = 644

Advanced bash Commands (Filters)

cat

head

head filename 
// display first 10 lines to stdout
head -n filename 
// display first n lines of filenames to stdout
head file1 file2 
// display first 10 lines of all files to stdout

tail

tail filename
 // display last 10 lines of filename to stdout
 
 tail -n filename or tail --lines=n filename
 // Display last n lines of filename to stdout
 
 tail --lines=+20 filename
 // Display all lines starting to line no 20 to stdout
 
 tail -cn filename
 // Display the last n characters of filename to stdout
 
 tail -f filename
 // Output append data as filename grow

cut

cut -c<column list> filename
// 1-2 or 1-2, 6, 8

paste

paste file1 file2 
// append at the end of line file2 to file1

paste -d '#' file1 file2
// use '#' as delimiter before appending on each line from
2 file
** Default delimiter is TAB

tr

tr -s ' ' < file1
// Squeeze multiple of space into 1 space

tr 'abc' 'xyz' < file1
// Replace 'a' with 'x', 'b' with 'y' and 'c' with 'z'

tr -d ',' < file1
// Delete all ',' from file1

sort

sort filename
// -n : numeric sort

uniq

Comparing Files

tar

gzip

find

Regular Expression and Grep

Grep

Atom

Class : Back References

Operators

Group

Save

sed - Stream Editor

sed - introduction

sed - Operation

Addresses

Addresses : Single-Line

Addresses : Set-of-Line

Addresses : Range

Addresses : Nested

Commands : Modify

Commands : Insert

Commands : Append

Commands : Change and Delete

Commands : Substitute

Commands : Transform “y”

Commands : Input / Output

Input / Output : Next “n”

Input / Output : Append Next “N”

Input / Output : Print & Print First Line

Commands : Files

Commands : Branch

Commands : Hold Space

Commands : Quit

AWK

awk Variables

Patterns

Expressions

Expression : Regular Expression

Expression : Arithmetic Expressions

Expression : Relational Expressions

Logical Expressions and Nothing

Range Patterns

Actions

Basic awk Statements

Decision Statements

Control Actions : next

Control Actions : getline

Control Actions : exit

Loops : while

Loops : for

Loops : do-while

Associative Arrays

Array Loops

Array Sorting (data)

Array Sorting (indexes)

Array : Display by their sorted indexes

String Functions

Math functions

User-defined Functions

Awk Command-line Arguments

Awk Command-line Variables

Shell Programming 1

Listing Running Processes

Parent and Child Processes

A Simple Shell Script

#!/bin/bash
# My first shell script
date
echo "Users currently loggedd in"
who

Arguments and Positional Parameters

#!/bin/bash
# My first shell script
head $1
echo ...
tail $1

Expression

Mathematical Expression

Relational Expression

File Expressions

Logical Expressions

Control Flow : if-then-else

Script Termination

Shell Programming 2

case

while loop

until loop

for … in loop

select

select ITEM in [LIST]
do
	[COMMANDS]
done
PS3="Enter a number: "

select character in Sheldon Leonard Penny Howard Raj
do
    echo "Selected character: $character"
    echo "Selected number: $REPLY"
done

Loop Control Statements

Special Parameters and Variables

Changing Positional Parameters

Shift Command

Shell Programming 3

Functions (1)

Functions (2)

Functions (3)

Functions (4)

Argument Validation

Debugging Scripts

Variable Evaluation

Variable Substitution

Substring Expansion

Pattern Matching in Bash

Miscellanueous Operators

Arrays

Arrays (2)

Arrays (3)

Shell Script I/O

echo

printf

read

Process ID and Job Numbers

Signals

Processs ID variables

trap

trap : Clean up temporary files

wait

Subshells