Professional Documents
Culture Documents
206 Sheet
206 Sheet
&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
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
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
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};
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.
Wk 7 POINTERS
TEXT CHAT
SIERPINSKI
CHAT ENCODE/DECODE