Tuesday, June 26, 2012

SHell saved the day!

I was having trouble letting my students start the processing IDE in our somewhat "super lag" computer lab. All options failed. Then I came to realization that I have to do the dirty job myself. This would include the following task applied repeatedly to student's individual directory:
  1. Accessing the student direcotry
  2. Access the Directory where I put the "zipped" IDE
  3. Extract the File
  4. And proceed to the next student directory.
This could go on forever should I choose to manually do the tasks one folder at a time. Then "light bulb", why not automate those tasks using a shell script. So after googling for some shell commands and syntax I finally arrived to the code below:
#!/bin/sh
#This is a simple shell script program to traverse all the directories containing
#the file  processing-1.5.1-linux.tgz stored inside csc 100 directory
#Programmed by: Orven E. Llantos

for file in *
do
 if test -d $file
 then cd $file;cd csc\ 100/; tar zxvf processing-1.5.1-linux.tgz; cd ../..;
 fi
done
The for-loop will access all the files in the current directory, then the if statement tests whether the current item is a directory, if it is then do the associated tasks otherwise do nothing. And done. Running this script saved me, to generalize the code above for it to do whatever you wanted to do repeatedly given a set of directories we can re-write it as:
#!/bin/sh
#This is a simple shell script program to traverse all the directories containing
#the file  processing-1.5.1-linux.tgz stored inside csc 100 directory
#Programmed by: Orven E. Llantos

for file in *
do
 if test -d $file
 then ;
 fi
done
Hope this could help you too. Fin.

No comments: