A Shell Script to do shell scripting faster f aster
Shell Scripting is all about automating a particular task, doing a task a bit faster than what it takes to do manually. However, when we write a shell script, if we notice properly, the the amount of time we take to get it done is little more. In other words, can we think of ways in which we can write and test a shell script faster? Typically, when we write a shell script, we open the editor, write Typically, write something, save and close it. Run Ru n th thee sc scri ript pt.. e epe pendi nding ng on th thee ou outp tput ut or so some me er erro rorr th thro rown, wn, op open en th thee fi file le ag agai ain, n, ed edit it something, save and close it. Run the script, and this goes on and on and on. The focus here is the amount of time you spent on saving the file every time, running it, and again opening the file and repeating this whole process umpteen times. In some cases, while running, we get error, and by the time we re!open the file, we forget what the error is. "ow, again run the script and see the error again and re!open it. The amount of time we spend here is pretty high.
Sometimes Someti mes,, wha whatt people do is, the they y kee keep p # terminal terminalss open, edit and save the fi file le in one terminal, and keep running the script in the other. This is a good time saver, but let us see what can be done to save the ma$imum amount of time to write w rite and test a script. The method which we are going to discuss to make this process faster involves two steps% 1. &ap a key in in vi to save and close a file. 2. 'rite a shell script which will open the file(which is a shell script), and run the script.
*ets discuss those # points in detail% 1. *et us map the function key +# in vi to do a save a close. The following is the line which we should add in the .e$rc or the .vimrc file. nce the below line is added, on pressing +# inside vi, the file will get saved and -uit vi.
map #2 :wq!^M
"ote% & is put using the following se-uence% /trl010& 2# refers to the function key ke y +#. The above setting will work if your 3ni$ flavor is Solaris or *inu$. If it is H4!35, refer this link% &apping function keys in vi 2. 'rite 'rite a small script, with the following contents. I name this script as workon %
$ cat workon #!/usr/bin/bash
if [ ! -f $1 ]; thn cho #!/usr/bin/bash " $1 fi
whi [ 1 ]; o %i $1 chmo &'' $1
(/$1 ra umm)
on
'hat this script does is simple% It accepts a file name as argument, which is a shell script in our case. If the file is empty, adds the she bang line and gets it read y. 6lse simply opens the file. n closing the file, runs the script and the output is displayed. n pressing any key, it takes you back to vi again. 7nd the cycle repeats. "ow, lets write a sample script, test.sh. 1. Invoke the workon script with the test.sh as its argument%
2. n invoking workon , the below screen will be opened. 7s you see, the she bang line gets added automatically since its a new file%
3. *et us edit the file and put some contents in it%
4. 4ress +# to save and run the file. 7s seen below, the output is displayed%
5. 4ress 86nter8 to edit the file again, and the cycle repeats%
In this way, we can write and test a shell script much much faster. Generic workon script: The ne$t -uestion, can we customi9e this workon script even more?
+or e$ample, if you are a developer, at a given point of time, you might be writing a /:/00 program, or a ;ava program or a shell script or a perl script or a python script or a 4*:S<* script and so on.'hen we write a / program, on pressing +#, we would like it to get compiled and the program be run, and the same holds good for ;ava programs as well. *ike the she bang line added automatically for a shell script, we would also like the same to get added for a 4erl or 4ython script. Similarly, for a 4*:S<* script, we would like the S6T statements to get added automatically. The below shown program is the customised workon program which I use. Hope it might be useful for you too. =ou might customi9e it further to suit your needs% #!/usr/bin/bash
fi*+cho $1 , s s/.(0..(.(0./.1/+ t*+cho $1 , s s/(0.(.(0./.1/+
if [ ! -f $1 ]; thn if [ $t * p ]; thn cho #!/usr/bin/pr " $1 if [ $t * p) ]; thn cho #!/usr/bin/p)thon " $1 if [ $t * c ]; thn cho #incu3stio(h" "" $1 cho - int main.n4 .n.n5 "" $1 if [ $t * sq ]; thn cho 678988 <=>8? 8@AB 1 " $1 cho <8B <88?CBDCB ?9 "" $1 cho <8B DEF8
cho <8B 8AIN ?II "" $1 cho <8B 78EJA9F ?II "" $1 cho - .n.nJ8L>E8.n.n.n "" $1 cho - K8FA9.n.n.n "" $1 cho - 89J;.n/ "" $1
s cho #!/usr/bin/bash " $1 fi
fi
whi [ 1 ]; o %i $1 chmo &'' $1 if [ $t * Oa%a ]; thn Oa%ac $1 [ $P -q H ] QQ Oa%a $fi if [ $t * c ]; thn Rcc $1 [ $P -q H ] QQ (/a( if [ $t * sq ]; thn sqpus
it
-s s)s/uni11S
S$1 338?I
8?I
s
(/$1 fi ra umm)
on
6n>oy the world of automation ! See more at% http%::www.theuni$school.com:#@AA:@B:shell!script!to!do!shell! scripting.html2sthash.*=CfaD9E.dpuf