Scripting 2
Scripting 2
Origins of scripting languages Learning scripting languages A simple example of a shell script A script with a loop and debugging Processing a table of data by selecting rows and columns Doing arithmetic internally within the shell Perl, Python, Ruby etc. Scheduling scripted jobs using cron
Benefits: * greater accuracy of job control * less typing * ability to restart job from initial data when system goes down part way through.
Mainframe features reinvented on smaller/cheaper machines.
Microsoft froze .BAT language early 1990s. Early versions of MS Basic packaged with MS-DOS, later versions sold separately.
Unix opened development and transfer of ideas between shells. * Bourne Shell: /bin/sh * 'C' Shell: /bin/csh * Korn Shell: /bin/ksh * Ash Shell: /bin/ash * GNU Bash Shell: /bin/sh and /bin/bash Unix shell scripts have usual 3rdG features, including loop constructs, variables, arrays, branches and functions.
reboot shutdown usr11361 usr11187 usr11187 usr11187 usr11513 usr11451 usr11456 usr11138 usr12069 usr12069
~ ~ console console console console console console console console console console
Tue May 11 13:14 Tue May 11 13:15 Tue May 11 09:04 - 10:22 (01:18) Mon May 10 18:53 - 20:30 (01:36) Mon May 10 18:50 - 18:53 (00:02) Mon May 10 18:38 - 18:50 (00:12) Mon May 10 15:15 - 16:27 (01:11) Mon May 10 12:11 still logged in Mon May 10 10:53 - 15:14 (04:21) Mon May 10 09:03 - 10:40 (01:36) Sat May 8 11:05 - 09:01 (1+21:55) Sat May 8 11:00 - 11:04 (00:04)
Advantage: Very rapid development of systems/network administration and automated operations. Disadvantages: Slow, not for very large programs, non-portable. Having to load and execute external programs very many times ?
Adding 200 user login accounts to a system: * Shell: 1 hour to run (1992), helped us learn how to do it * Perl: ('93) 2 minutes to run, we already knew how to do it * 'C' ('94) 6 seconds but 10 times as long to write the program.
Advanced scripting: Perl, Python, Ruby 4 This program runs much faster than the shell script, because everything is done inside the same process. There are many syntactic similarities, but Perl borrows array and loop notation from 'C' and some other notation from grep, awk and sed. As in Bash, $ is used to introduce single (scalar) variables and @ is used for arrays. $_ is used for a default scalar variable, so that the substitution operation e.g: s/\)//; which strips a closing round bracket from a string, doesn't state the string it modifies.