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%
- Figure out how to take in an argument
on the command line and echo it back.
-
When the above is working:
Use "which"
with that argument to see if a program with that name exists.
-
When the above is working:
Echo the return code
of "which".
-
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%
- 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.
- 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.
-
When the above is working:
If good program name, use "touch" to create the file.
-
When the above is working:
Use "chmod"
to make the file executable.
-
When the above is working:
Launch the editor, detached from the current process.
Testing
- xprog grep - rejected
- xprog newprog - ok, created, is executable, editor launched