Archive for the 'unix-coding-training' Category

sqlite 10 sec guide

Problem You want to get up and running with sqlite Solution Install sqlite3 on linux, via yum: yum install sqlite3Then just run sqlite dbname Example % sqlite3 testdata.dbSQLite version 3.5.9Enter “.help” for instructionssqlite> create table memos(text,priority INTEGER);sqlite> insert into memos values(‘deliver project description’,10);sqlite> insert into memos values(‘lunch with Christine’,100);sqlite> select * from memos;deliver project description|10lunch [...]

Stripping out newlines from string

Problem Different versions of UNIX handle the stripping of newlines in a string differently. Obviously if you echo a string, the shell automatically expands newlines “\n”. Solution sed and tr don’t appear to like performing these substitutions. But with Perl its a steal. Example % echo ‘hello world\nhello world\ntest it’ | perl -ane ‘s#\\n# #g; [...]

Resize image command line – mogrify

Problem You want to resize images from the command line. Solution Use image magik mogrify command.mogrify -resize “percentage” image_naAlso you can view current sizing – like this:% identify IMG_3864.JPGIMG_3864.JPG JPEG 563×422 563×422+0+0 DirectClass 8-bit 32.8887kb Example mogrify -resize 20% IMG_3705.JPG Reference Technorati Tags: mogrifymogrify man page – resize an image

receive notification when file arrives

Problem You want to get notified when a file arrives. Maybe it is coming in from somewhere else, or just is the result of some processing.What ever the case, never sit there waiting for a file to arrive again! Solution Put the code under example tab, in a cron entry. See reference for more detail [...]

Shell Utilities

Problem You want to compare 2 streams, with byte offset and line numbers?Pull off content with a byte offset?Display lines before or after a pattern?Send an email – upon job failure? Solution The shell is a powerful beast! Anything is possible … with a little know-how. See the examples. Example md5sum TARBALL | awk ‘{print [...]

tar many uses

Problem You want to make some backups with tar. Solution tar is very functional and has many uses. Example If you are lucky enough to have GNU tar (tar –version will return GNU if so), then you can compress and backup at the same time. tar zcvf /tmp/filename.tgz .This backups and zips the contents of [...]

Port forwarding with SSH

Problem You want to forward all traffic from a port on your box, to another host/port combination. Solution The command in the example will port forward, from the local box – any traffic sent to port 8081, will be sent to port 8080 on www.example.com: Example ssh -x -g -L 8081:www.example.com:8080 localhostDon't forget, if you [...]

awk line length and average

Problem You want to display line lengths – then average line length of file. Solution Snip using awk – see example. Example awk ‘ { thislen=length($0); printf(“%-5s %d\n”, NR, thislen); totlen+=thislen}END { printf(“average: %d\n”, totlen/NR); } ‘ filename Reference Technorati Tags: gawk, Unix Coding SchoolLinux Man Pages – gawk command

While For Loops

Problem You want to loop around a number of times and perform an action. Solution Being able to loop around a given number of times, fully utilizes the power of UNIX. See the example. Example For example, spin around 100 times and print hello:i=0; while [[ $i -lt 100 ]] ; do echo -n “hello”; [...]

Split output over columns

Problem Ever wanted to produce columns of output, rather than your output spilling off the screen. Solution Useful command for this is paste, see example for how it works – to split output into 3 columns. Example ls | paste – - -comments.php comments-popup.php fat.jsfooter.php header.php index.phpscreenshot.png sidebar.php style.cssAs you'd expect with UNIX, that is [...]