|
|
Note: The archives category content is an automatically generated focus channel and does not neccessarily reflect the opinions of this blog. No responsibility is taken for the external links presented here, follow at your own discretion. The archives content is never scraped from sites - but an abstract obtained from search engines.
Category: 'UNIX Coding Training'
Tuesday, January 22nd, 2008
| Problem |
Solution |
Example |
Reference |
Recommended |
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!
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
Put the code under example tab, in a cron entry. See reference for more detail about cron. Change the minutes, hour, filename and mail address! You may need to use mail instead of mailx and ksh, etc instead of bash. But the basic syntax is the same.
|
|
1 Comment »
Saturday, December 29th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
- 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?
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
md5sum TARBALL | awk '{print $1;}' | cmp - TARBALL.md5 # cmp good for comparing output with file contents, or diffing files. Outputs byte offset and line number. md5sum creates a hash (checksum) of file. dd bs=1 skip=106 if=infile # disk to disk copy, example outputs infile with offset of 106 bytes (block size of 1 byte, skip 106 bytes). - Ever needed to display a line before or after a pattern. Yep sure you can use Perl, but this is a bit simplier to work out.
See the code here See a run through screen shot here Usage - ppgrep #1 #2 pattern file - where #1 is the number of lines prior to the pattern (could be 0) and #2 is number of lines post pattern. - Sending email upon UNIX cron failure
|
No Comments »
Saturday, December 29th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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 the current directory, to filename.tgz in tmp directory. If not - no drama. This will backup and pipe through content to gzip, which then zips it. tar cvf - . | gzip –best > /tmp/filename.tgz Recreate a directory: mkdir new tar -C old -cf - . | tar -C new -xvf Restore over the network: ssh remote_hostname "(tar -cf - remote_directory)" | tar -xvf - For example: ssh bree "(tar -cf - Perl_Bin)" | tar -xvf -
|
No Comments »
Sunday, December 16th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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:
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
ssh -x -g -L 8081:www.example.com:8080 localhost Don't forget, if you need to open LINUX (FC4) firewall, do this (as root): vi /etc/sysconfig/iptables # add following line then save, exit -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8081 -j ACCEPT /etc/init.d/iptables restart
|
No Comments »
Sunday, December 16th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
awk ' { thislen=length($0); printf("%-5s %d\n", NR, thislen); totlen+=thislen} END { printf("average: %d\n", totlen/NR); } ' filename
|
No Comments »
Friday, December 7th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
For example, spin around 100 times and print hello: i=0; while [[ $i -lt 100 ]] ; do echo -n "hello"; ((i++)) ; done; echo "" hellohellohellohellohellohellohellohellohellohello hellohellohellohellohellohellohellohellohellohello hellohellohellohellohellohellohellohellohellohello hellohellohellohellohellohellohellohello… Similarly the for command is a beauty too, spin around 5 times and kick off a loop 5 times - paste the result into 5 columns: for i in 1 2 3 4 5; do for j in 1 2 3 4 5; do echo "$i:$j"; done; done | paste - - - - - 1:1 1:2 1:3 1:4 1:5 2:1 2:2 2:3 2:4 2:5 3:1 3:2 3:3 3:4 3:5 4:1 4:2 4:3 4:4 4:5 5:1 5:2 5:3 5:4 5:5
|
No Comments »
Friday, December 7th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
ls | paste - - - comments.php comments-popup.php fat.js footer.php header.php index.php screenshot.png sidebar.php style.css As you'd expect with UNIX, that is not the end of this commands uses! Create a comma seperated list: ls | paste - - - -d, comments.php,comments-popup.php,fat.js footer.php,header.php,index.php screenshot.png,sidebar.php,style.css Then courtesy of the LINUX info command: cat num2 1 2 $ cat let3 a b c $ paste num2 let3 1 a 2 b c And: $ paste -s num2 let3 1 2 a b c
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
For example, spin around 100 times and print hello: i=0; while [[ $i -lt 100 ]] ; do echo -n 'hello'; ((i++)) ; done; echo '' hellohellohellohellohellohellohellohellohellohello hellohellohellohellohellohellohellohellohellohello hellohellohellohellohellohellohellohellohellohello hellohellohellohellohellohellohellohello… Similarily the for command is a beauty too, spin around 5 times and kick off a loop 5 times - paste the result into 5 columns: for i in 1 2 3 4 5; do for j in 1 2 3 4 5; do echo "$i:$j"; done; done | paste - - - - - 1:1 1:2 1:3 1:4 1:5 2:1 2:2 2:3 2:4 2:5 3:1 3:2 3:3 3:4 3:5 4:1 4:2 4:3 4:4 4:5 5:1 5:2 5:3 5:4 5:5
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
[[ $(echo $var | grep -ic "pattern") -eq 0 ]] & You could also go for "-eq 1" - does contain: [[ $(echo $var | grep -ic "pattern") -eq 1 ]] & Or "-ne 0" for same, etc. [[ $(echo $var | grep -ic "pattern") -ne 0 ]] & Plus you could use || for "or" either in place of "&&" or after this block - same as if .. then "positive" else "negative" fi. [[ $(echo $var | grep -ic "pattern") -eq 0 ]] || { echo "var does contain pattern" }
|
No Comments »
Thursday, December 6th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
If you start a process with nohop (ignore the hup signal), it will not exit (usually) when your shell is exited. nohup stands for no hang up, a throw back to terminal lines.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
nohup command > /tmp/logfile.log 2>&1& If you forget to run the command with nohup, this can now be simulated on solaris. Just background the process (with ctrl Z and bg) then type disown. disown [%1] The UNIX command jobs will show all backgrounded jobs. After running disown, it should show no jobs in the background.
|
No Comments »
|