Search
Topics
Krishnendu\'s Blog

  Create an account  · 


Navigation
Main
 _HOME
General
 Feedback
 Recommend Us
Community
 Downloads
 Forums
 Members List
Members
 Account
 Private Messages
Information
 Content
 Encyclopedia
 Resources
 Linux Kernel
 GNU
 Linux Security


Sponsors
_BLOCKPROBLEM2

www.GLUG-Howrah.org :: View topic - Shell script helped me out
 Forum FAQForum FAQ   SearchSearch   UsergroupsUsergroups   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Shell script helped me out

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.GLUG-Howrah.org Forum Index -> Programming
View previous topic :: View next topic  
Author Message
neo
Pingu
Pingu


Joined: Apr 13, 2004
Posts: 75
Location: kolkata/calcutta

PostPosted: Tue May 20, 2008 2:40 am    Post subject: Shell script helped me out Reply with quote

Those who know and tell difference from a rookie to a veteran need not be told that there is a much gap between what can be gotten from a “professional” technical book available in the market and real hand on experience. Some people prefer Internet which poses to be the panacea for every problem got in the way in one's professional life. You don't know the “code” ! Well run a search in the Internet and see if somebody has written that code snippet for the same or similar problem. So who cares for basics! Why beat your brain? Our presumptuous ability to forget and to disdain for the basics is so widespread that we, more often than not, forget that we once had to learn to walk. Here is an experience i have gained which shows that why basics are so important and lies at the heart of virtually everything.

Few days back I carelessly copied a directory of java program files on a disk with K3B and forgot to take care of the file extensions while coping. So when i copied the same directory from the disk back i found that all the files now have a queer file extension “.jav”. Understandably one cannot expected to change the file names or extensions one by one. Yes! that would be foolish if you are a programmer. Now there is a wonderful way to solve this silly problem. Get a program to do the job. “Read all the files with that wrong extension and change the same”. Is there any on Unix or Linux environment? Answer is “No”. At least I didn't find any. What to do then? Why bother? If you call yourself a programmer then do the job yourself! Write a program to get the job done! And more so if you are a “open source” programmer or “open source” evangelist you call yourself boasting off. They all had written their programs whenever they faced any computing problems. Plenty of application born out of programmer's personal itch! [Read the Cathedral and the Bazaar by Eric S. Raymond]. So charged up by this I set out to write my filename extension changing program. But a bell rang in my mind “don't try to reinvent the (metaphorical or proverbial) wheel” and I started to find if there is A program or a similar program. One thing i knew for sure that i needed a shell script. I know shell programming. I did it in the past and even i taught some people too but i have lost touch with it for long. Well! Well! hadn't I bought books before and don't I have some tutorial on the subject too? Bingo! Yes! then Why on earth am I scratching my head? I jumped for the books and first book i hit was “Unix Shell Programming by Yashavant Kenetkar. Yeah! that legendary man who wrote so many books that outsold any other on the same subject. I flipped through the pages to find what i was looking for. “This is good book no doubt “ i told myself and a a a a a yes! here it is! On page 303 (three not three eh!) I found at last a script – a small one which can solve my problem I had the gut feeling(?) . I just copied it from the page to the editor, saved it and GO! Now! list the directory! Oh! What ? What the hell! Er . . . .r.r ..r.r. no! It can't Be! Is there something i forgot?
The program reads
Code:
for file in *.jav
         do
           mv $file $file.java
        done


After i ran the script i saw that yes file name changed but all the files now read [b]<anyname>.jav.java. Certainly I never desired that. Originally the program was written for changing all the filename with .c extension to .cpp extensions.

In the book the description was given is like this : “Take filenames from a directory as values for the control variable as shown below:
Code:
for file in *.c
                 do
                     mv $file $file.cpp
                 done

This loop would pick up all C program files from the current directory and add the extension '.cpp' at the end of each such file.”[/b]


Though it was not clearly stated but the intention was to change the filename extension to a different extension and not keeping the old extension intact. What do YOU make of it and the statement above from the book? Clearly the problem was not solved. If you are wandering why the program did not solve the problem read the Unix/Linux (mine is a Linux Box) file naming system/convention you'll get it. Now, I wrote mine shell script which read

Code:
for fvar in *
do
if [ -f $fvar ]
 then
  str=$( echo $fvar | cut -f 2 -d"." )

   if [ $str = "jav" ]
    then
        st=$( echo $fvar | cut -f1 -d"." )
        st=${st}".java"
        mv $fvar $st
   else
       echo "nothing to do"
   fi

fi
done


I saved the shell script with file name flnmchange.sh gave it execution permission and ran. Bliss! All the filename with the “.jav” extension now changed to “<filename>.java”. Needless to say that i experimented the programs in a temporary directory with the copy of the .jav files. But now in the original directory i have corrected the file extensions in one go. I am not taking the trouble to explain step by step how shell script works. I'll do that only if you really need that (I hope that the program is self explanatory so you won't need that) and post a reply and have a question regarding the script.

Anyone can change the script to behave as a general program to similar problems. So sometimes lessons learned the costly way. Hope you won't go through the similar situation if you remember the basics and don't take things for granted. And ready to shatter myths no matter how they surround a person or what stature that myth has achieved! Try your own things and get a solid grounding on fundamentals. As for me I keep learning! Happy scripting /programming folks!

Code:
Back to top
View user's profile Send private message Visit poster's website
neo
Pingu
Pingu


Joined: Apr 13, 2004
Posts: 75
Location: kolkata/calcutta

PostPosted: Mon Jun 09, 2008 9:48 pm    Post subject: hi ! Bidhata Reply with quote

YES Ofcourse I would very much like to. In fact i am really eager to meet you guys and talk. I have so much to tell you about next steps we might consider to take and other plans. I would also like to say that the originality of the posting(writing) is somehow missing. For an example the cryptology posting. It's not good enough. You may scold me for being outspoken on this but it is necessary to keep the standard higher. And moreover our friends were discussing crypto and how QUANTUM COMPUTATION (on this very topic i and one of my friend who is doing research on this can plan and write. he is also very interested to post at this site. Tell me what you think!)can end it at length the other day and i thought about writing on it for every one - for new people and for tehies too. I'll do that for other topic also. Now i would like to draw your attention to other new sections to be included - like Databases. There are people who would like very much about the latest databases technology and comparisons among several products and their usages at a streach. Can't we start a new section on that? More plans i'll talk later. If you don't mind my new phone no is 98303 48984. You can contact me. Oh! before i sign oput i should express one of my wishes i treasure to my heart. I wanted to make a short film( even video format would do) on open source movement in india. See if i can arrange in future! Wishes to all of you. And keep thinking engine running.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.GLUG-Howrah.org Forum Index -> Programming All times are GMT + 5.5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



Powered by phpBB © 2001, 2005 phpBB Group





plastic card printers  
tablet pcs  wall fountain printer ink
Kartendrucker 
Free Trial for Anonymous Surf Software 
Cheap Term Life Insurance
Books

NorthWestPharmacy.com is a premier and licensed supplier of Canada Drugs Online