This is a simple shell instance.
Here is a description of its requirements:
Experiment five UNIX Shell program design
Experiment content: Use UNIX shell programming language, write Unix shell application.
Experiment requirements: The following requirements to write a shell script, the learning of the various UNIX commands in tandem.
1. Run the shell script, first displaying a menu similar to the following:
What Would you:
List Directory **********************1
Change directory*********************2
Edit file****************************3
Remove file**************************4
ADD messages in a file***************5
Search keyword in a specified file***6
Exit menu****************************7
Enter Your choice:
2. Enter 1: Lists the contents of the current directory in a long format.
3. Enter 2: Change the working directory to the target directory of your own designation. (You can view the contents of the directory with option 1 after changing the request)
4. Enter 3: Prompt for a filename (the file may not exist), and then call the VI editor for editing.
5. Enter 4: Deletes a specified file.
6. Enter 5: Prompts for a filename and then adds and saves any information entered by the user on the command line on the keyboard to this file until the user enters a blank line to complete the operation.
Note: This operation cannot be stopped by entering only one line, you can continuously enter multiple lines of information, and you cannot call the VI editor to enter.
For example: A file called "Phone" with the original content (or can be a new file without content):
"This is a phone list"
Now you want to add some field information for the phone book later,
Select "5", display: Input The filename that you want to add messages:
Then enter the file name from the keyboard: phone
Next prompt: Enter the information you want to add, such as the user wants to add the following information:
David Brown (7771) 91101111 m@m.com ......
Emma Redd (6970) 22292222 in@o.com .....
Tom Swanson (823) 44474444 ai@e.com .....
Ming Li (0871) 3133333 bb@r.com .....
................ ...... ........ ............ ...........
When the input completes, the user enters a blank line to complete the item's add information and exits the option. This "phone" file is added to: (Can be viewed with option 3)
This is a phone list
David Brown (7771) 91101111 m@m.com ......
Emma Redd (6970) 22292222 in@o.com .....
Tom Swanson (823) 44474444 ai@e.com .....
Ming Li (0871) 3133333 bb@r.com .....
................ ...... ........ ............ ...........
7. Enter 6: Locate a keyword in the specified file (the file, and display the line where the keyword found is located.)
Requirements: Ignore case when looking, and display line number before each output line found, to determine whether the name entered is a file , not a directory, if the file is to find work, if the file does not exist or the directory will display error messages.
8. Enter 7: Exit the script and return to the shell prompt.
9. Add the appropriate comment to the source program.
Note: This menu should be able to repeatedly display, that is, after the selection to return to the menu again.
Experiment Report:
1, the preparation and debugging of their own programs, the use of a variety of documents placed in their own user's home directory.
2, copying their own shell source program (write their own paper)
3, write design ideas.
4, write on the machine experience and summary
5. Answer the question: what are the different ways to execute a script?
The completion report is as follows:
Experiment Five UNIX Shell Program Design
1. The source code is as follows:
#!/bin/bash
c= "0"
#
# Do List directory
#
ListDirectory ()
{
Ls-l./
Return
}
#
# do change the directory
#
Changedirectory ()
{
Read-p "Input the New Directory Path:" CDI
# Check out wether the ' directory is exist
If [-D $CDI]
Then
CD $CDI
Else
printf "The Directory is not exist/n"
Fi
Return
}
#
# do edit a file
#
Editfile ()
{
Nfile= ""
Read-p "Input" File Name to Edit: "NFILE
VI $NFILE
Return
}
#
# do remove a file
#
RemoveFile ()
{
Rfile= ""
Read-p "Input" File Name to Remove: "Rfile
If [f $RFILE]
Then
RM $RFILE
Else
printf "The File is not exist/n"
Fi
Return
}
#
# do input messages
#
Addmessages ()
{
Mfile= ""
Read-p "Input" File Name to Add Messages: "Mfile
# start to add new Messanges
Mmsg= ""
Read-p "Input the Messages:" Mmsg
While ["$MMSG"!= "]
Todo
echo $MMSG >> $MFILE
Mmsg= ""
Read-p "Input the Messages:" Mmsg
Done
printf "End of the file/n"
Return
}