Monday, November 22, 2010

Unix IF

File not exist

if [ ! -f filename ]
echo file does not exit

Test variable
if [ $a == 1 ]
echo true
echo false

Thursday, November 18, 2010

vi cursor movement

Type: To Move To:
h one space to the left (also try left arrow)
j one line down (also try down arrow)
k one line up (also try up arrow)
l one space to the right (also try right arrow)
$ end of current line
^ beginning of current line
Enter beginning first word on the next line
G end of file
:n line n; use :0 to move the beginning of the file
w beginning of next word; 5w moves to the beginning of the 5th word to the right
e end of next word
b beginning of previous word
Ctrl-b one page up
Ctrl-f one page down
% the matching (, ), [, ], {, or }
(Press % with your cursor on one of these characters to move your cursor its mate.)

Wednesday, November 17, 2010

SSH escape sequences

~. - terminate connection
~B - send a BREAK to the remote system
~C - open a command line
~R - request rekey (SSH protocol 2 only)
~^Z - suspend ssh
~# - list forwarded connections
~& - background ssh (when waiting for connections to terminate)
~? - this message
~~ - send the escape character by typing it twice

Note that escapes are only recognized immediately after newline

Unix substring

${#string} - Length of String
${string:position} - extract substring from $string at $position
-- position argument starts at 0, not 1.
${string/substring/replacement} - replace first match of $substring with $replacement
${string//substring/replacement} - replace all matches of $substring with $replacement
${string/#substring/replacement} - If $substring matches front end of $string, substitute $replacement for $substring
${string/%substring/replacement} - If $substring matches back end of $string, substitute $replacement for $substring
${string#substring} - Strip shortest match of $substring from front of $string
${string##substring} - Strip longest match of $substring from front of $string
${string%substring} - Strip shortest match of $substring from back of $string
${string%%substring} - Strip longest match of $substring from back of string

echo ${#file}

echo ${file:12}

echo ${file:5:2}

echo ${file/of/OF}

echo ${file//a/A}

echo ${file:0:4}

echo ${file/#j/A}

echo ${file/%s/A}

echo ${file#aaa}

echo ${file%aaa}

Job commands

ps : report processes and pid numbers
jobs : current jobs and job id numbers
ctrl-S : stop screen scrolling
ctrl-Q : resume screen scrolling
ctrl-Z : suspend a job
stop %n : suspend background job n

bg : push the job to background
fg : bring a job to foreground
fg % job-id : foreground by job-id

ctrl-C : kill foreground process
kill -KILL pid#
kill -KILL %job-id

If job is running in foreground hit ctrl-z to suspend, then bg %n to run in background.