Bash URI parser using SED

Warning! This version is now obsolete! Check out the new and improved version (using only Bash built-ins) here! Here is a command-line (bash) script that uses sed to split the segments of an URI into usable variables. It also validates the given URI since malformed strings produce the text “ERROR” which can be handled accordingly: # Assembling a sample URI (including an injection attack) uri_1=’http://user:pass@www.example.com:19741/dir1/dir2/file.php’ uri_2=’?param=some_value&array[0]=123&param2=\`cat /etc/passwd\`’ uri_3=’#bottom-left’ uri=”$uri_1$uri_2$uri_3″ #

Continue reading

Recursive file/directory change-detection

Disclaimer This article explores a way in which an approximate “fingerprint” of a file tree can be created! If all you want is to detect file changes a much more appropriate method would be to use inotify/incron. Version 2 (update) Another, much faster method would be to use ls -lR to browse over the filesystem. On a newly installed Debian virtual machine (on Xen) hashing the entire filesystem (the root

Continue reading

Detect number of CPUs on a machine

UPDATE: Steven pointed out (very nicely) that there’s no need for cat in this picture, grep would do just fine on its own. So, thanks Steven! Detect how many CPU cores are present on the running machine: grep -c processor /proc/cpuinfo This can be very useful when writing multi-threaded programs to properly match the number of threads with the number of CPU cores.

Continue reading