CIT050 Index > Assignment: Programming bash

Assignment: Programming bash

The most up to date version of this assignment is at Moodle.

Read everything before doing anything!

Script 1

Write a script that will take a single filename as an argument and adds execute permission to the file for the user, but only if the file is a regular file. Name the script Your script must check to see that there is exactly one argument. If there are no arguments or more than one arguments, your script must produce a "usage" message that tells how to use the script. You write it this way:

echo "Usage: $0 filename"

The $0 (that’s a zero, not the letter “Oh”), is a special variable that contains the name of the script file that is currently running.

Note also: this is a script that you are writing, it is not done via the "script" command, which is just a tool to record all your keystrokes.

I'll get you started. Type this command to create a new file:


Then insert these lines into the file:

echo "Usage: $0 filename"

Then save the file, and make it executable:

chmod 755

Now test it:


Your shell script must check to see if the file exists; if it doesn’t, give an error message. You must check to see if the file is a regular file; if not, give an appropriate message. Here is the result of running the script several times, with extra blank lines to make things more readable.

linux199@linux:~> touch file1
linux199@linux:~> mkdir dir1

linux199@linux:~> ./
Usage: ./ filename

linux199@linux:~> ./ file1 file2
Usage: ./ filename

linux199@linux:~> ./ dir1
dir1 is not a regular file.

linux199@linux:~> ./ nosuchfile
nosuchfile does not exist.

linux199@linux:~> ls -l file1
-rw-r--r--  1 linux199 users 0 2006-11-21 17:38 file1

linux199@linux:~> ./ file1

linux199@linux:~> ls -l file1
-rwxr--r--  1 linux199 users 0 2006-11-21 17:38 file1

Note: You should not presume that all files have permission 0644. You should add execute permission ONLY for the user, and leave all other permissions exactly as they were.

Because most of you are not programmers, here is some “pseudo-code” (half Bash script, half English) to tell you how the program should look. Text in italic is text that you need to put in yourself.

if # of arguments is one
   if file in $1 exists
       if file in $1 is a regular file
       add execute permission
       print message that this is not a regular file
      print message that the file does not exist
   print the "Usage" message

Script 2

Write a script that will do an ls -l command on each of its arguments only if the argument is the name of a regular file. This script will have a for loop like the one on page 431 (411 in old edition), and inside the loop will an if test that uses one of the file test options listed on page 421 (401 in old edition).

This script is independent of the previous script. This script will do an ls -l on regular files. It will not touch their permissions at all.

Name your script

Here’s how it will work. Presume you have files named xyz and abc , and a directory named mydir . If you run the script:

./ xyz mydir abc nosuchfile

You will get output like this, since xyz and abc are the only two regular files:

-rw-r--r-- 1 linux199 users 24490 2006-11-21 15:26 xyz
-rw-r-xrw- 1 linux199 users 5963 2006-11-16 18:47 abc

Your program must work on any list of files from the command line, not just the three files that I gave in the example.

When You Finish

Upload both scripts to Moodle.