Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain


CA114      CA170

CA668      CA669      Projects

Lab - xprog

The problem

When we write a new script on Linux, we need to give it a name.
We need to check: Is this name already in use? (You should not call it "ls", for example.)
We also need to make the script executable.

The assignment

Write a program "xprog" to do all these checks for us.

When we make a new program, we type:

xprog (program name)
This checks if this is a suitable name for a program, and if so, launches the editor and makes sure the file is executable.

Recipe - For 40%

  1. Figure out how to take in an argument on the command line and echo it back.
  2. When the above is working: Use "which" with that argument to see if a program with that name exists.
  3. When the above is working: Make "which" silent by re-directing both output streams to null. The code to do this is to put this at the end of the line:
       > /dev/null   2> /dev/null 
  4. When the above is working: Echo the return code of "which".

For 100%

  1. When the above is working: Write an if statement based on this return code. The if statement echos whether the program name is taken or not.
  2. Warning: The return code variable shows the return code of the last command we executed, which may now be "echo" not "which". So you can now delete the echo statement.
  3. When the above is working: If good program name, use "touch" to create the file.
  4. When the above is working: Use "chmod" to make the file executable.
  5. When the above is working: Launch the editor, detached from the current process.


ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.