.

Labels

.
Showing posts with label miscellaneous. Show all posts
Showing posts with label miscellaneous. Show all posts

Auto shutdown after copying, downloading , converting etc

Many times it has happened that i had to sit in front of the computer late into the night or early morning to get some file to complete copying  or downloading. Before learning scripting i really searched and wished there was some program which could shutdown my system for me after some process is completed. This script works well for copying, downloading and when converting videos or other files.

First before using the script change the property of your shutdown command , so that you don't need to type your password for executing shutdown. Run the following command in terminal :
sudo chmod u+s /sbin/shutdown

There is also one more method to use shutdown without entering password by editing /etc/sodoers . But
this never worked for me ( Ubuntu 11.10 ) . If you want to use this method check this out  http://askubuntu.com/questions/168879/shutdown-from-terminal-without-entering-password   


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
This script continuously checks if there is any change in the  size of directory in which the process is taking place and initiates shutdown if no change is observed for a specific time.

du is a command-line tool for estimating file space usage
shutdown is command-line tool to bring the system down
bc is command-line tool to do mathematical calculation. Better if using huge values in calculation

-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#for shutting the computer after completing a process
#please avoid using script in the home folder and similar folder which contains files having access permissions denied

#change interval time to your need
interval=5s

echo ".. started checking every $interval" 

then=0;

for (( i=1; i>0; i++ ));do
   sleep $interval
   now=$(du -c "$PWD" | grep total | sed 's/total//g' )
   check=$(echo "$now-$then" | bc )

   if [[ $check == "0" ]];then
        echo -e "No change in data size of current directory noticed.\nInitiating shutdown in 1 minute .." 
        shutdown -h 1
   else
        then=$now
   fi
done

-------------------------------------------------------------------------------------------


To use the script inside terminal move to the directory where the copying or downloading is carried out and just run the script. Change the value of interval  to the time you find suiting. With a little bit of imagination you can change the script for many purposes. If you dont want to shutdown after completing the process , change it to something else like playing a song through mplayer or something said through espeak !!

 Now no need to waste your precious time waiting !!

Enjoy Linux !!
Dont know what to do with these codes ??  click here

Create desktop wallpaper with Imagemagick


Imagemagick is a really awesome tool which few people really utilise mainly because it is a command line application. Creating Random Noise Images is one of the many cool features of Imagemagick.  Imagemagick recognises around 680 colors and we can make beautiful background images with individual or dual combinations of these colors . Check http://www.imagemagick.org/script/color.php to see list of colors or if Imagemagick is installed through the command line convert -list color in terminal.

If Imagemagick is not installed install it via terminal sudo apt-get install imagemagick ( Ubuntu ) or through  Software Centre ( Ubuntu ) or Add/Remove Software ( Fedora ).



-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
Colours are randomly selected from the list in color.txt file and wallpapers are made with convert command using these colors.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#to create wallpaper with Imagemagick

#to get the list of colors and save it in a folder
if [ -e /home/$USER/color.txt ];then
   null=$null1
else
   convert -list color | cut -d "(" -f 1 | sed 's/rgb//g'| sed -n '6,$p' > /home/$USER/color.txt
fi

#for selecting colors randomly 

select=$[ ( $RANDOM % 3 ) + 1 ]
num1=$[ ( $RANDOM % 680 ) + 1 ]
num2=$[ ( $RANDOM % 680 ) + 1 ]

color1=$(sed -n "${num1}p" /home/$USER/color.txt)

color2=$(sed -n "${num2}p" /home/$USER/color.txt)


if [ $select == 1 ];then
   convert -size 2000x1250  plasma:$color1 random.jpg
elif [ $select == 2 ];then
   convert -size 2000x1250  plasma:"$color1-$color2" random.jpg
else
   convert -size 2000x1250 plasma:fractal random.jpg
fi


------------------------------------------------------------------------------------------- 

Running the script ones will create color.txt file ( where the list of colours  recognised my Imagemagick is stored for random selection) and image file random.jpg ( 2000x1250 pixels ) in your home directory.
Make random.jpg your desktop background. If not satisfied with background run the script again till you find background of your taste. As random.jpg file is replaced by the script the background will also change automatically. 

You can also try looping the script through a for loop . The script is named back in my system .So in terminal execute 

for (( i=0; i<35; i++ ));do
back
sleep 3
done

See 35 times your background change with really cool colour combinations !! Try it , it is really cool !!



If you want your background to be new everytime you loggin  place the script in Startup Applications  or by executing  the script through  /etc/rc.local 


click on image to see enlarged


 click on image to see enlarged


 click on image to see enlarged


 click on image to see enlarged


 click on image to see enlarged


To read more on backgrounds with Imagemagick please visit http://www.imagemagick.org/Usage/backgrounds/  and  http://www.imagemagick.org/Usage/canvas/#random


Enjoy Linux !!

Dont know what to do with these codes ?? click here

Solve Crossword Puzzle


Script to solve Crossword Puzzle


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
There are two files named british-english and american-english which contains all words in the dictionary without their meaning i.e just the word only , in /usr/share/dict.
The puzzle  provided is compared with the words in any of the above files. 
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#for solving crossword puzzle
# put '?' in place of missing letter
# if you want british spelling replace 'american-english' with 'british-english'

read -p "please enter the puzzle:"

word=$(echo $REPLY | sed 's/?/./g')
cat /usr/share/dict/american-english | grep --color=always "^$word$"


-------------------------------------------------------------------------------------------

Suppose you want solve  puzzle "_ros__ord" , you should enter as ?ros??ord ( put question mark instead of blank spaces ). The script in this case is named crossword .OK lets solve this puzzle together ..





Ohh!! It is crossword !!

Enjoy Linux !!

Dont know what to do with these codes?? click here

Check for Palindrome

Script to check whether given string is a Palindrome.

A palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, with general allowances for adjustments topunctuation and word dividers.
                                             -Wikipedia ( http://en.wikipedia.org/wiki/Palindrome )


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
The script divides the string to be checked into two parts of equal number of characters i.e it will divide anna into an and na.
'rev' is command line tool which will reverse the string provided.The second part of the string is reversed and then compared with the first part.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
#!/bin/bash

#to check whether the given string is palindrome.String can contain white spaces

#finding the lenght of the given string
length=$(echo $* | wc -c)


#cutting string into two with one in reversed order
if [[ $((length%2)) == 0 ]];then
   length=$((length/2))   
   l=$(echo $* | cut -c 1-$length)
   r=$(echo $* | rev | cut -c 1-$length)
else
   length=$(((length-1)/2))
   l=$(echo $* | cut -c 1-$length)
   r=$(echo $* | rev | cut -c 1-$length)
fi

#checking both cut parts of the string
if [[ "$l" == "$r" ]];then
   echo "Yes.It is a palindrome"
else
   echo "Sorry.It is not a palindrome"
fi


-------------------------------------------------------------------------------------------


In my computer this script is named palindrome . If I need to check palindrome of a string say malayalam, I will type palindrome malayalam in terminal and press Enter ..





..DONE SIR !!!

Enjoy Linux!!

Dont know what to do with these codes ?? click here


Dont know what to do with these codes ??

Shell script is a series of commands written and stored in text file which when executed run as typing each command in the terminal and executing it individually . Shell script can do simple task like doing  mathematical calculations and even to make great applications for your own needs. In this case scripts are for Bash shell and always has #!/bin/bash as its first line.

To make more clarity,  suppose you have the habit of downloading a lot of music (.mp3)  files . After downloading you will move the file to the folder in which your collection of mp3s are located from the Downloads folder , which may be on another disk partition.
Cutting and copying with  mouse right-clicking is the worst way of doing that !!

Instead you can use a command-line :
find /home/$USER/Downloads -name "*.mp3" -exec mv {} /media/mymusic/collecton/ \;  

but the command is too lengthy and typing it each time is laborious. Save the command in a text file and name it, say movemp3. Make it executable and call the script in the terminal by typing movemp3 and press Enter and it is done! This is the fastest and best way to do the work !!  

HOW TO USE THE CODES PROVIDED ?
For using any script given in this blog, copy the entire script and paste in a text file using any text editor like gedit, emacs , vim etc. Save the file with any name and with or without .sh extention.Best thing is to place the script somewhere were the shell will automatically find it. Or else you will have to type entire path name of the script.
For example our earlier script movemp3 placed in Desktop can only be called by /home/$USER/Desktop/movemp3 in terminal instead of just movemp3. It is advisable to place all your scripts inside a separate folder and include that folder in the PATH variable. To include in PATH variable , go to home directory and press Ctrl h



click on the image to view it larger

Open the .bashrc file in any text editor and add this at the end of the file -


export PATH=/home/$USER/BASH:"$PATH"


OK.I want to place all my scripts in newly created folder BASH. So this line includes BASH in my home directory to PATH variable . Instead of /home/$USER/BASH    you should provide your folder's correct path name. Save and exit the text editor.





Now all scripts placed in the folder which was added to PATH variable can be executed just by its name . No need to type entire path !!



For the scripts to work make  them executable  by right clicking and ticking Allow executing file as program in Permissions or using terminal by navigating to the folder in which the script is placed and running line command chmod 755 name-of-script


If there is any more doubts please feel free to ask.




Enjoy Linux !!!



find and play music - quickest way !!

So you are the one who loves music and has more than thousand of songs saved in your system.This script is going to make your life a hell lot easier!! Too much song in your drive makes it hard to find and play the song you want.You can avoid a lot of double-clicks with this really simple script.

Make sure you've got mplayer installed. If not run sudo apt-get install mplayer ( Ubuntu ) in terminal or install it through Software Center ( Ubuntu ) or Add/Remove Software ( Fedora ).



-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
Nothing much to explain.The script is pretty self explaining.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

# for finding and executing music in mplayer
# replace '/my/Music/Collection/'with real path of the folder your music is stored. '/*/*/*' are given to look into sub folders

song=$*

#for playing all songs in shuffled manner
if [[ "$song" == "all" ]];then
{
mplayer -shuffle /my/Music/Collection/* /my/Music/Collection/*/* /my/Music/Collection/*/*/* /my/Music/Collection/*/*/*/* /my/Music/Collection/*/*/*/*/* /my/Music/Collection/*/*/*/*/*/*
}
else  
#for finding and playing the song name given
{
find /my/Music/Collection/ -type f -iname "*$song*" -exec mplayer {} \;
}
fi

-------------------------------------------------------------------------------------------


In my case the script is just named 'm' ( please keep that name so you don't have to type much ) . If I want to play , say Robbie Williams - Feel ,  I'll just type  m feel or m robbie or m 'anypart of the name of the song you remember ' and press enter . I've more than 2000 thousand songs in my drive and finding and playing it doesn't even take a second !!! 


    click on the image get larger view


If you have more than one song having the same name you given to m script , it will play all those songs one by one. If first song played is not what you intended then press Enter to go to next song or press Ctrl C to close the script and again execute with more details.

If you want to play all songs in your collection in shuffled manner execute m all.


Try it. You will fall in love with this script !!

Enjoy Linux !!


Don't know what to do with the codes ?? click here

Check a word's spelling


script to check a word's spelling . 
The script uses aspell . A short simple one liner in terminal   echo [word ] | aspell -a   will do the work , but the script makes it more convenient and the output is screened to give only what is needed. 



-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
Nothing to explain.The script just edits the output of 'aspell' with 'sed'
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#to check or find the correct spelling of the given word

found=$(echo "$*" | aspell -a | sed "s/@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.7-20110707)//g" | cut -d : -f 2 | sed 's/,/\n/g')
echo -e "\e[1;34mdid you mean \e[0m:$found"


-------------------------------------------------------------------------------------------

In my case the script is named spell . To check whether definitly is the correct spelling , type spell definitly in terminal and just press enter..







OK. It is definitely 'definitely' and not definitly :)

Enjoy Linux !!


Don't know what to do with these codes ?? click here

Playing .dat video file from a cd or dvd

Sometimes .dat video files wont play by double clicking on it neither in vlc nor in totem player. This script play .dat video files from a cd or dvd one after the another in mplayer.

For installing mplayer run in terminal sudo apt-get install mplayer  or install it through Software Center ( Ubuntu ) or Add/Remove Software ( Fedora ).


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
'mount' command will display all the drives loaded.
If disk drive is loaded , mount command will display it as /dev/sr0.
When the disk is loaded script will check number of .dat files , dat files are placed in  'mpegav' folder in the cd.
Then the dat files are played one after the other with mplayer
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
#!/bin/bash

#for running .dat file

#getting path of the mpegav file in cd
name=$(mount | grep "sr0" |cut -d / -f 4,5 | cut -d " " -f 1)

#check to find whether cd is loaded
if [[ "$name" != "$null" ]];then
     count=$(find /$name/mpegav/ -nowarn -type f | wc -l)
fi

#playing .dat file or sending warning
if [[ $count != $null ]];then
     i=2
     count=$((count + 1))
     while [ $i -le $count ];do
          mplayer -cdrom-device /dev/cdrom vcd://$i

          i=$((i + 1))

           clear
     done
else
     clear
           echo "vcd not loaded or not present!!!"
     sleep 1
fi

-------------------------------------------------------------------------------------------


Now, who said we need Windows to play .dat file ..??

Enjoy Linux


Don't know what to do with these codes ??  click here