Archive for the Shell Scripting Category
Create Patch Files Using Patch And Diff
Using diff to create batch files and subsequently running them against select files is such a convenient way to update, fix or change existing files, but many developers and system administrators simply don’t know much about them.
Here is a quick primer on using diff and apply changes for select files. Use diff –help to check out more options and flags.
NOTE: pay caution when using patching, any mistake or error are executed without warning or undo feature. It’s best to always make a backup of any file or folder that are going to be affected by your patch.
diff
Some useful flags you can specify when comparing files are -b (ignore white space difference), -B (ignore blanc lines), -r (recursive) and -i (ignore case).
Let’s say we need to change the GA analytics code on our pages and that they are unfortunately hard coded. The files are shown below as are the results from diff: (more…)
Track And Parse Twitter Messages Stream
Ever wanted to listen, track and parse tweets from a Twitter stream from an individual user? It’s quite an easy task, if you know the right URL’s to parse. Curl is a great little tool to get sources from almost any web resource and Curl has roots in Linux command like and PHP, just to name a couple.
Let’s look at how this would work by pulling CNN’s breaking news feed as JSON:
curl http://twitter.com/status/user_timeline/cnnbrk.json
or XML:
curl http://twitter.com/status/user_timeline/cnnbrk.xml
That will give you the last 20 tweets in either JSON or XML format.
An even more interesting option is to get messages where you have been mentioned. It’s a bit more complicated, as we need to supply login credentials, but no rocket science either:
curl -u “username:password” http://www.twitter.com/statuses/mentions.json (or .xml if you prefer)
That’ll give you the last few tweets mentioning the user supplied with the curl command. Now all you got to figure out is how to parse the messages and plug them into your application.
Ubuntu run script at startup, shutdown or reboot
What’s the proper method to run a script at startup on Ubuntu?
You can use update-rc.d for start-only or stop-only scripts, following these steps:
Start script called “startup_script” on startup (note the dot at the end of the line) :
# update-rc.d -f startup_script start 99 2 3 4 5 .
- start is the argument given to the command (start, stop).
- 99 is the start priority of the script (1 = first one, 99= last one)
- 2 3 4 5 are the runlevels at which to run the script
The dot at the end of the line has significance, read more here: /etc/rcS.d/README
Start startup_script on shutdown and reboot :
# update-rc.d -f startup_script start 90 0 6 .
Stop startup_script on halt and reboot :
# update-rc.d -f startup_script reboot 90 0 6 .
To run the script as a daemon, use the skeleton file located at “/etc/init.d/skeleton”
To know which runlevel you are running, type:
$ runlevel
Read more about runlevels here.
Nginx and memcached module
Memcache is traditionally used as a module inside server side scripts, such as PHP, ASP, ColdFusion and others. And it’s doing a terrific job, as long as it’s implemented correctly.
But if we look under the hood of the actual Memcache application, and I’m not talking about the PHP or ASP extension, but rather the executable that’s running as a daemon under linux, for example, it is a rather simple database like application running in memory. Now there are two basic actions that need to be performed to use it, one is writing info into memcache, the other is reading it. In a typical scenario, there are many reads for one write, that’s the whole point, isn’t it. But what if we can isolate the reading from the server side scripts, and let a small high speed module do that for us.
Auto Create Thumnails The Easy Way
Ever wondered it there is an easy way to resize your images or quickly create thumbnails from your favorite pictures?
Search no more, the awesome little utility ImageMagick does it for you.
Install it using your package manager, most of them should have it in their repository.
sudo apt-get install imagemagick
Once installed, this command will create tumbnails for all JPG’s in the current directory, 200 pixel wide/high on the longest side. Eg. if your image is in landscape layout, it’ll be 200 pixel wide, in portrait format it’ll be 200 pixel high.
for file in *.jpg ; do convert -resize 200 “$file” t”$file”; done
That’s it. Simply change 200 to the size of your liking, or change the extension if your images are in gif, png or any other image format.
Exchange SSH keys and avoid logins and passwords
Working in a Linux environment, changes are you’re often using ssh, copying or rsyncing files between servers and workstations. I find it very annoying to always have to type in my login credentials. To avoid that, we can exchange ssh keys so that all the authentication is done utilizing those keys.
We can achieve that by following these two simple steps:
1. Generate a new key
$ ssh-keygen
2. Copy the generated ssh key to the target server(s)
$ cat ~/.ssh/id_rsa.pub | ssh user@target_machine cat – “>>” ~/.ssh/authorized_keys
That’s all there is to exchange ssh keys.
Bash Shell Script to Loop Over Set of Files
I’m often asked how to write a shell script that loops over a set of files in the current directory and perform some actions on them. Really, nothing is simpler than that, it’s just that most administrators or developers don’t take the time to get into shell scripting.
The simplest form is this, simply change the wildcard characters to match your requirements:
#!/bin/bash FILES="*.txt" for f in $FILES do echo "Processing $f ..." # perform some action on each file. $f stores the current file name echo "Done processing $f ..." done