This lab will give you the chance to investigate several different examples of script languages.
To get started, download and unzip the scripts.zip starter file. Since we will be working with a number of different files, the main instructions are included in this web page rather than scattered in the files.
When you are finished you can check your work against my solution.
Shell Scripts
The first scripts we will look at are bash shell scripts. Shell scripts are often used to perform simple tasks with files. The bash scripting language consists mostly of control structures, plus a few conventions around variables. That's enough to accomplish a lot of useful things.
To begin, take a look at script_args.sh. This is a pretty simple script. Lines beginning with # are comments. The command echo prints output to the terminal. Anything beginning with $ is a variable. The variables $0, $1, etc come predefined as the various command line arguments used to run the script. So for example:
$ bash script_args.sh abc def script_args.sh abc def
Note the extra blank line in the output above, which comes from printing the nonexistent argument $3. This language is pretty forgiving about undeclared variables -- they all have empty values!
The script above has some problems. In addition to the blank line problem, if we happened to provide four or more arguments only the first three would be printed. It would be nice to print exactly as many arguments as were present, and no more. The second example script, script_args2.sh, does exactly this, and gives us the chance to look at the syntax of a loop. This is a set-based loop, where the variable $arg takes on each of the values in the expression $@. This happens to be each of the command line arguments; $@ is another predefined variable that holds the array of arguments. If you run this script, it will print exactly as many lines as you provide command line arguments. Try it with a few different options.
Exercise 1: Sometimes when working on a progtamming project, one decides to change the name of some program component. This may necessitate renaming files, often more than one. For example, in C there may be files with .c, .h, and possibly .o extensions all with the same name, all needing to be changed. In Java there will be a .java file and a .class file of the same name.
Write a new shell script that will rename all the files beginning with the specified prefix to use a new prefix. For example:
$ ls Tamp.class Tamp.java $ bash rename_base.sh Tamp Temp $ ls Temp.class Temp.java
This seems complicated but I will give you all the pieces. Begin with the jpg2png.sh file we saw in lecture. We'll be doing something very similar but with a few changes.
- We will need the length of the portion of the file name to be replaced. This can be computed as: baselen=${#1} (note the lack of any space around the = sign; this is important).
- To get the list of files we want to loop over, we put the .* wildcard after the file stem we are searching for (stored in the first command line argument variable, $1. Think about how to do this, and then click here to verify the full line of code.
- To create the new name, we concatenate the new base name with the suffix of the existing file name f: target=$2${f:$baselen}
- We want to rename the file so long as there isn't already an existing file with the new name (otherwise it would get overwritten). We can test this with the line if [ ! -f $target ].
- The actual renaming is just a call to the mv, providing the old name and new name as arguments.
You can create some dummy files to test on if you want.
Javascript
The second scripting language we are going to look at is Javascript. This one is a good choice because of the ubiquity of web browsers. We will use scripting to add a small bit of functionality to a page.
Begin by looking at button.html. Open it with a web browser and observe what happens when you click the button. This is a scripted effect achieved using Javascript. It's mildly interesting, but not very informative.
Now look at the file in your code editor. Most of the text here is HTML boilerplate that we're not really concerned with. The only part of the file you need to look at is lines 4-7, which make up a Javascript function that handles the response when the button is clicked. As mentioned in the lecture, this small bit of code manages to both extract information and execute changes to the web page via the interface provided by the web browser (called the document object model). That happens on line 6. The previous line sets up the text that we wish to add.
Exercise 2: Augment the script so that instead of just saying "Button clicked" the page includes the time at which this event occurred. Since the Javascript syntax is similar to other languages we have studied, you should be able to figure out how to do this. Some hints:
- String concatenation in Javascript uses the + operator.
- You can create a Date object with the current time as demonstrated in the script sample file from lecture. (Choose View Source or CTRL-U to see the Javascript source.) Once you have the object, you can call its toLocaleTimeString() method to get the current time as a string.