Starting from:

$25

COMP9044 - Week 05 - Laboratory Exercises - Solved

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

 

 

Create a new directory for this lab called lab05, change to this directory, and fetch the provided code for this week by running these commands:

$ mkdir lab05

$ cd lab05



 

 

Write a Shell program, backup.sh which takes 1 argument, the name of a file.

Your program should create a backup copy of this file.

If the file is named example.txt the backup should be called .example.txt.0 but you should not overwrite any previous backup copies.

So if .example.txt.0 exists, the backup copy should be called .example.txt.1 and if .example.txt.1 also exists it should be called .example.txt.2 and so on.

For example:

$ seq 1 3 >n.txt

$ cat n.txt 







$ backup.sh n.txt 

Backup of 'n.txt' saved as '.n.txt.0' 

$ cat .n.txt.0 







$ backup.sh n.txt 

Backup of 'n.txt' saved as '.n.txt.1' $ backup.sh n.txt 

Backup of 'n.txt' saved as '.n.txt.2' $ backup.sh n.txt 

Backup of 'n.txt' saved as '.n.txt.3' 

$ ls .n.txt.* 

.n.txt.0 

.n.txt.1 

.n.txt.2 
Your answer must be Shell. You can not use other languages such as Perl, Python or C.

When you think your program is working, you can use autotest to run some simple automated tests:


Rewrite your shell script from the last exercise as a Perl program, backup.pl which takes 1 argument, the name of a file.

Your program should create a backup copy of this file.

If the file is named example.txt the backup should be called .example.txt.0 but you should not overwrite any previous backup copies.

So if .example.txt.0 exists, the backup copy should be called .example.txt.1 and if .example.txt.1 also exists it should be called .example.txt.2 and so on.

For example:

$ seq 1 3 >n.txt

$ cat n.txt 







$ backup.pl n.txt 

Backup of 'n.txt' saved as '.n.txt.0' 

$ cat .n.txt.0 







$ backup.pl n.txt 

Backup of 'n.txt' saved as '.n.txt.1' $ backup.pl n.txt 

Backup of 'n.txt' saved as '.n.txt.2' $ backup.pl n.txt 

Backup of 'n.txt' saved as '.n.txt.3' 

$ ls .n.txt.* 

.n.txt.0 

.n.txt.1 

.n.txt.2 
Your answer must be Perl only. You can not use other languages such as Shell, Python or C.

You may not run external programs, e.g. via system or backquotes. for example, you can't run cp.

No error checking is necessary.

When you think your program is working, you can use autotest to run some simple automated tests:

$ 2041 autotest perl_backup
When you are finished working on this exercise, you must submit your work by running give:


Write Shell scripts snapshot-save.sh & snapshot-load.sh which saves & restore backups of all the files in the current directory.

These scripts should be in Posix-compatible Shell, use:

#!/bin/dash 
snapshot-save.sh
If snapshot-save.sh should save copies of all files in the current directory.

snapshot-save.sh should first create a directory named .snapshot.0 to store the backup copies of the files.

But if .snapshot.0 already exists, the backup directory should be called .snapshot.1 and if .snapshot.1 also exists it should be called .snapshot.2 and so on. snapshot-save.sh should ignore files with names starting with .

snapshot-save.sh should also ignore itself and snapshot-load.sh (not backup snapshot-save.sh and snapshot-load.sh). snapshot-load.sh n

If snapshot-load.sh is called with a first argument of n it should restore (copy back) the files from snapshot .snapshot.n.

Before doing this it should copy the current version of all files in a new .snapshot directory, (hint run snapshot-save.sh)

This is to make sure the user doesn't accidentally lose some work when restoring files. It is always done even if the user wouldn't lose work.

Examples
$ ls .snapshot.*/* ls: cannot access .snapshot.*/*: No such file or directory 

$ echo hello >a.txt

$ snapshot-save.sh 

Creating snapshot 0 

$ ls .snapshot.*/* 

.snapshot.0/a.txt $ echo word >a.txt

$ snapshot-load.sh 0 Creating snapshot 1 

Restoring snapshot 0 

$ ls .snapshot.*/* 

.snapshot.0/a.txt 

.snapshot.1/a.txt $ cat a.txt hello 
and

$ echo hello0 >a.txt

$ echo world0 >b.txt $ snapshot-save.sh 

Creating snapshot 0 $ echo hello1 >a.txt

$ echo world1 >b.txt $ snapshot-save.sh 

Creating snapshot 1 $ echo hello2 >a.txt

$ echo world2 >b.txt

$ ls .snapshot.*/* 

.snapshot.0/a.txt 

.snapshot.0/b.txt 

.snapshot.1/a.txt 

.snapshot.1/b.txt 

$ snapshot-load.sh 0 Creating snapshot 2 

Restoring snapshot 0 $ grep . ?.txt 

a.txt:hello0 

b.txt:world0 
Your answer must be Posix-compatible Shell only. You can not use Bash ,Perl, Python or C.

Autotest and automarking will run your scripts with a current working directory different to the directory containing the script. The directory containing your submission will be in $PATH.

This means ./snapshot-save.sh run from snapshot-load.sh, but running snapshot-save.sh will succeed.

No error checking is necessary.

When you think your program is working, you can use autotest to run some simple automated tests:

$ 2041 autotest shell_snapshot
When you are finished working on this exercise, you must submit your work by running give:

$ give cs2041 lab05_shell_snapshot snapshot-save.sh snapshot-load.sh
before Tuesday 14 July 18 00 to obtain the marks for this lab exercise.

 

 

Write a Perl program, snapshot.pl which saves or restores backups of all the files in the current directory.

snapshot.pl will be called with a first argument of either load or save. snapshot.pl save

If snapshot.pl is called with a first argument of save it should save copies of all files in the current directory. snapshot.pl should first create a directory named .snapshot.0 to store the backup copies of the files.

But if .snapshot.0 already exists, the backup directory should be called .snapshot.1 and if .snapshot.1 also exists it should be called .snapshot.2 and so on. snapshot.pl should ignore files with names starting with .

snapshot.pl should also ignore itself (not backup snapshot.pl). Hint: Perl's glob and mkdir functions are useful. snapshot.pl load n

If snapshot.pl is called with a first argument of load and a second argument of n it should restore (copy back) the files from snapshot .snapshot.n.

Before doing this it should copy the current version of all files in a new .snapshot directory, in other words do the same as a save operation.

This is to make sure the user doesn't accidentally lose some work when restoring files. It is always done even if the user wouldn't lose work.

Examples
$ ls .snapshot.*/* ls: cannot access .snapshot.*/*: No such file or directory 

$ echo hello >a.txt

$ snapshot.pl save 

Creating snapshot 0 

$ ls .snapshot.*/* 

.snapshot.0/a.txt $ echo word >a.txt

$ snapshot.pl load 0 Creating snapshot 1 

Restoring snapshot 0 

$ ls .snapshot.*/* 

.snapshot.0/a.txt 

.snapshot.1/a.txt $ cat a.txt hello 
and

$ echo hello0 >a.txt

$ echo world0 >b.txt $ snapshot.pl save 

Creating snapshot 0 $ echo hello1 >a.txt

$ echo world1 >b.txt $ snapshot.pl save 

Creating snapshot 1 $ echo hello2 >a.txt

$ echo world2 >b.txt

$ ls .snapshot.*/* 

.snapshot.0/a.txt 

.snapshot.0/b.txt 

.snapshot.1/a.txt 

.snapshot.1/b.txt 

$ snapshot.pl load 0 Creating snapshot 2 

Restoring snapshot 0 $ grep . ?.txt 

a.txt:hello0 

b.txt:world0 
Your answer must be Perl only. You can not use other languages such as Shell, Python or C.

You may not run external programs, e.g. via system or backquotes. for example, you can't run cp.

No error checking is necessary.

More products