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 !!!



1 comment:

  1. Really a great post.I liked it and i will share it with others too.
    1978 Buick Wildcat AC Compressor

    ReplyDelete