Dr. Mark Humphrys

School of Computing. Dublin City University.

Online coding site: Ancient Brain

coders   JavaScript worlds

Search:

Free AI exercises


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.

Usage:
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: Echo the return code of "which".
  4. 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 
    


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.


Testing



ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.      New 250 G VPS server.

Note: Links on this site to user-generated content like Wikipedia are highlighted in red as possibly unreliable. My view is that such links are highly useful but flawed.