You are on page 1of 2

SHELL

&1
STDOUT
&2
STDERR
&>
STDOUT and STDERR to an output file
$variable To reference the variable of choice
PWD, PATH, HOME, MAIL, TERM, HISTFILE
Quoting
$VAR = "Hello, world" VAR: command not found
$ VAR="Hello, world"
$ echo $VAR
Hello, world
$ echo "$VAR"
Hello, world
$ echo '$VAR'
$VAR
$ echo \$VAR
$VAR
$ echo \'$VAR\'
'Hello, world'
$ echo "'$VAR'"
'Hello, world'
$ echo '"$VAR"'
"$VAR"
$ echo \"$VAR\"
"Hello, world"
awk samples
$ date
Tue Sep 22 21:25:49 EDT 2015
$ date | awk '// {print $2, $3, $6;}'
Sep 22 2015
$ date | awk '// {if ($3>10) {print
$2, $3, $6;}
else { print "Early in the month"} }'
Sep 22 2015
$ echo 1 1 1 1 1 1 1 | awk '// {if
($3>10) {print
$2, $3, $6;} else { print "Early in
the
month"} }'
Early in the month
Loop Sample
#!/bin/bash
for i in $( ls ); do
echo item: $i
done
Reading User Input
#!/bin/bash
echo Please, enter your name
read NAME
echo "Hi $NAME!"
Flags
-e = First line
Useful System Commands
fg: Command for putting a job in foreground
bg: Command for putting a job in background
Ctrl+Z: Stop/suspend the current running process
Ctrl+C: Kills the job running in the foreground
Cat: Concatenate: Read files or STDIN, print on STDOUT
Wc: Print count newlines, words or bytes for each file
Grep: Print lines matching a pattern
Tr: Translate or delete characters

Cut: Print out columns from files


Sort: Sort lines of text files
Awk: Pattern scan, processing language, use to print
Sed: Stream editor for filtering and transforming text
Rm = remove
Ls = list directory files
Less: Simple text file viewer ("less is more")
Time: Run programs, summarize system resource usage
Mktemp: Create a temporary file or directory
C
int *p;
int a = 257;
p = &a;
to a)
int b = *p;
p = &b;
(points tob)
*p = 13;
p is updated

Pointers
a pointer to an integer value
an integer variable
p holds the address of a (points
is assigned the value of a
p holds address of b instead
The value of the int pointed to by

De-reference *p, address-of &p,


index p[i] (which is equivalent to *(p+i))
Pointers: Memory locations of a piece of data
int *x;
There is an int at the memory location stored in x
&x Gives memory address of x

Bitwise Operations
bit_arg<<shift_arg
Shifts bits to of bit_arg shift_arg places to the left -equivalent to multiplication by 2^shift_arg
bit_arg>>shift_arg
Shifts bits to of bit_arg shift_arg places to the right -equivalent to integer division by 2^shift_arg
left_arg & right_arg
Takes the bitwise AND of left_arg and right_arg
left_arg | right_arg
Takes the bitwise OR of left_arg and right_arg
left_arg ^ right_arg
Takes the bitwise XOR of left_arg and right_arg
~arg
Takes the bitwise complement of arg

p == q;
p - q;
bytes

p now holds the same address as q. The


value of referenced variables do NOT
change.
Compares the addresses held by p and q,
NOT the underlying data.
Difference in terms of array position, NOT

Char = 8 bits
int = 32 bits

Arrays
Sequences of data of the same type
- Array size is constant, CANNOT be changed (i.e. arrays
cannot be manipulated)
- declaration: TYPE ArrayName[size] = {0,1,2};
- Arrays of strings can be declared as:
char *array[] = {String1, String2};

Structs and Typedef


- struct Tutorial_Session {int numAttending; char
Types
roomName[50]; int time;}
Can be casted (e.g. x=5; y = (double) x;) //y=5.0)
- Create instance: struct Tutorial_Session comp206tut =
Integers:
int, short, long (signed or
{30, McConnell, 1800};
unsigned) printed with %d
- To typedef and declare a struct under a new name,
Floating point:
float, double, printed with %f
use:
Characters:
char, printed with %c
typedef struct Tutorial_Session{int numAttending; char
Pointer:
TYPE*, printed with type%, &var roomName[50], int time;} TUT_SUS;
(memory address of var) printed - Now a Tutorial_Session struct instance can be
with %p
declared as TUT_SES comp206tut;

int i = 100;
printf(%d\n, *x);Print the value at location stored in x char c = 'a';
float f = 3.1415;
double d = 3.1415926535897932385;
Pointers can be manipulated (ptr++, ptr = &newInt)
f = i;
c = f;
Add./sub. on pointers allows iteration over arrays
i = d;
p++; p + 3; p--; p 3;
d = i;
All of these move in units of sizeof() the underlying
Originally, the values are
type.
i: 100, c: a, f: 3.141500, d: 3.14159.
After re-assigning, the values are
Several math operations are not defined for pointers:
i: 3, c: d, f: 100.000000, d: 3.
p + q; p * q; 3 p; p + 3.14;
p = q;

Libraries
#include <library.h>
<stdio.h> - fopen/fclose, getchar, scanf, printf, etc.
stdin, stdout, stderr
<string.h> - functions for strings (strdup, strcpy, strcat
(append), strlen) <stdlib.h> - defines numeric
conversions, memory allocation (malloc, free)
<assert.h> - assert macro detects logical errors (e.g.
assert *ptr != NULL).

File Management
- FILE *fp;
FILE fopen(const char *filename, const char *mode)
e.g. fp = fopen (file.txt, w+); Creates file.txt with
read/write privileges
- Always fclose files that have been opened.

IN PLACE POINTER SORT

FROM SAMPLE MIDTERM (RATIO)

Wk 7 POINTERS

TEXT CHAT
SIERPINSKI

CHAT ENCODE/DECODE

You might also like