Skip to main content

Posts

Self terminating code for bash scripts

Self terminating code for bash scripts is very easy. But have to be sure not to kill itself in the process. The procedure is simple. Find all the PID with the same name as itsself $0 and then kill all those instances one by one in a for loop except itself $$ (PID of its own). This is a very general script and could be used anywhere and in any script. =================================== if [ "$1" != "" ] ; then # if no command line parameter supplied then move on. if [ "$1" == "kill" ] ; then # if command line parameter is "kill" then kill all running instances of this script. nnn=`ps aux | grep $0 | tr -s " " | cut -d" " -f2 ` echo -e $$ "\n====" # prints PID of this instance of script. make sure not to kill itself. for ii in `echo $nnn` do if [ "$ii" -ne "$$" ] ; then echo "Killing PID:" $ii kill -9 $ii fi done exit fi fi

Random wallpaper change script for Linux

This script would change wallpaper at regular intervals in a random manner. The script can also be killed at any time from any terminal. just like pulseaudio. #For eg. my-laptop>$ ./wallpaper-changer.sh kill Usage: wallpaper-change.sh [option] [option] = kill : Kill all running instances of the script = change : Change the wallpaper = dir : Change the wallpaper directory to = -h : Display this help. =============================================== #!/bin/bash # Wallpaper randomizer script # written by JOnes v4.1.5 # this script keeps rotating the wallpapers at a fixed interval and does it randomly. # It goes into background. You can either kill it manually or use the "kill" switch as a command line parameter. # location of wallpapers / pics. wallpaperdir="$HOME/Pictures" # wallpaper change interval time in seconds. Cannot changed during runtime. changetime=1800 # do not have to edit be...

find if another instance of the same script is running in background

A bash script snippet to find if another instance of the same script is running in background. This code snippet is very useful for those scripts which have to stay in the background and do some scheduled tasks or just plain monitoring like the laptop battery. It is supposed to be included at the beginning of the code. It would check if another instance of the script is running as a background job. If yes then it issues a warning and terminates the script. A note of warning : For multicore CPUs it is a tricky deal to figure the actual number of instances. If CPU is dualcore then echo ${run_count} outputs 2 for the first run. And for the second time around it outputs 3. The extra number is the suspended instance of the script. For quadcore the number would go as 4 -> 5 -> 6. For for the first time just run without the comment on echo ${run_count} and then set the value of exp_count (2 for dualcore , 4 for quadcore etc.). ================================================ process=...

Dopamine induced learning

While watching the killer whales and dolphins perform at the San Diego waterworld, I wondered what motivates these animals to display such acquired skills. I understand that it is the food that they get which prompts such abnormal behavior. But where did the process of learning triggered in those primitive brains. I believe it is the Dopaminergic neurons that are responsible for the learning behavior. These neurons have a predictive error response, which means they give a good stimulus to the brain in a reward comes along. But if expected rewards are not obtained it causes depression. This is known as motivation to us. If we get good results in a job we get motivated to do it. If not we get depressed. So, logically we always do something which gives maximum reward, that is, we always try do something we are good at. Wikipedia article -- " In nature, we learn to repeat behaviors that lead to maximizing rewards. " This is true even for animals. They repeat the behavior which re...
Nowadays normally Ubuntu mounts external hard drives without the executable bit set as a precaution against dangerous scripts from running automatically. Without the executable bit set it becomes impossible to run scripts from external hard drives. However, setting the executable bit is not as hard but little bit laborious. So I wrote a small script to decrease the load. In order to set the executable bit for the normal user the specific partition has to be unmounted as root and then remounted by the normal user with SSID and setting the executable bit so that he has execution permission. The normal remounting process could be done in the userspace so it is all included in the script but the unmounting requires root privilege. Since, sudden unmount can freeze open files and cause data loss if the mount point is removed forcibly (rm -rf) I have not included it in the script. So the unmounting has to be manually by the user with root privileges. So if using a public computer or a shared...

Gateway Netbook LT2802u tinkering

This Gateway Netbook LT2802u is a very compact machine. You need to take it apart in order to even install RAM. So I opened it up for fun and put it back. In order to open the Gateway netbook there are many guides. First read the instructions on http://dl.dropbox.com/u/26855544/Gateway%20LT28%20Series%20Netbook%20Repair.pdf from (  http://gadgetgeekgrrl.blogspot.com/2011/08/netbook-memory-upgrade.html ) then see the following video http://www.youtube.com/watch?v=uBM9jbhUpkc and then see the tips at http://www.notebookforums.com/t/237671/lt2802u-ram-xp-install-instructions The last link gives crucial tips on how not to scratch your laptop. So use a thin laminated card or a Safeway card (not your credit card) instead of a screwdriver. If you are still using a thin flat screwdriver then cover its tip with some thin paper like a receipt. But do not try to remove those thin T-shaped screws which hold the keyboard in place and visible from the battery compartment...

A script to extract audio from a bunch of video files

This sript extracts the soundtrack from any video format and converts it to mp3. Put all videos in a single folder and run this script on them. This works on all kinds of video. This is specially useful on those bunch of youtube videos. It works like flv to mp3, mp4 to mp3, avi to mp3 etc. ================================== #!/bin/bash # convert-video-to-mp3 v2.0 by JOnes. # This sript extracts the soundtrack from any video format and converts it to mp3. # Put all videos in a single folder and run this script on them. This works on all kinds of video. This is specially useful on those bunch of youtube videos. echo -e "\nThis sript extracts the soundtrack from any video format and converts it to mp3. Put all videos in a single folder and run this script on them. This works on all kinds of video. This is specially useful on those bunch of youtube videos.\n\n" if [ ! `which mplayer` ] ; then echo -e "Install mplayer \nsudo apt-get install mplayer" exit 0 fi if [ ! `wh...

Internal Field Separator (IFS)

Internal Field Separator (IFS): This internal variable in bash shell turns out to be an indispensable tool in these days of spaces and other weird characters in filenames. Anyone who has tried to script in bash which involved accessing lot of filenames have definitely come across the problem of spaces in filenames. The problem is this. It becomes impossible to split a directory listing "ls" into filenames using just the for $i in `ls` command. And the reason is simple. The directory listing separates filenames by spaces but the filenames themselves might have spaces. So the for command cannot figure out where the filename ends and where it begins. That is where this neat trick comes in. We have to tell bash to ignore all separation symbols other than newline while splitting up the directory listing into filenames. This means each line would be a new filename. And we have to make sure that each line has a new filename. This is easily achieved by the "ls -1" command...

mplayer shortcut to change aspect ratio while playing video

The following command tells the available commands for keyboard input. mplayer -input cmdlist The keyboard shortcuts to mplayer are stored in two locations - /etc/mplayer/input.conf and ~/.mplayer I prefer editing the /etc/mplayer/input.conf. Assign shortcuts to aspect aspect ratio switching as follows switch_ratio e.g. a switch_ratio 1.3333 # 4:3 s switch_ratio 0 # switches back to original aspect ratio b switch_ratio 1.7777 # 16:9 ratio put his at the end of /etc/mplayer/input.conf.