Wednesday, December 1, 2010

New Lines in Echo

happymsg='Files Delivered\nFile1.txt\nFile2.txt\n'

echo $happymsg
Files Delivered\nFile1.txt\nFile2.txt\n'

echo -e $happymsg
Files Delivered
File1.txt
File2.txt


Unix Control Key Commands

ctrl-s - freeze scrolling screen
ctrl-q - unfreeze the screen, display continues
ctrl-c - interrupt a running program
ctrl-\ - same as ctrl-c but stronger when terminal doesn't respond
ctrl-z - suspends a running program
ctrl-h - deletes last char typed
ctrl-w - delete last word typed
ctrl-u - deletes last line typed
ctrl-r - redraws last line typed
ctrl-d - ends text for many Unix programs

Monday, November 22, 2010

Unix IF

File not exist

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

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


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

EXAMPLES
file=jack_of_all_trades
echo ${#file}
18

echo ${file:12}
trades

echo ${file:5:2}
of

echo ${file/of/OF}
jack_OF_all_trades

echo ${file//a/A}
jAck_of_All_trAdes

echo ${file:0:4}
jack

echo ${file/#j/A}
Aack_of_all_trades

echo ${file/%s/A}
jack_of_all_tradeA

file=aaa_bbb_ccc_aaa
echo ${file#aaa}
_bbb_ccc_aaa

echo ${file%aaa}
aaa_bbb_ccc_


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.