تم الحل ✓
categoryهندسة الحاسبات
schoolبكالوريوس
event_available2026-07-15
السؤال
Transcribed Image Text:
UNIX Shell Scripts
All problems have the same weight.
Problem 1
Write a script named isearch to allow users to input a file name and a word/pattern.
Search the file for the word/pattern and display the lines that contain the word/pattern on
the standard output. If the word/pattern is not found, display a message.
indigo 20% isearch
Enter input file name: phone_book.txt
Enter the word to search for: from
Case-sensitive (y/n)? n
Nikola Sandic
ROB Oxley
(647) 209-7932 friend from work
(416) 723-8759 friend from university
If the word/pattern is not found, display a message.
Word 'from' not found.
Problem 2
Write a script named icount to allow users to input a file name and display on the
standard output the number of lines, words or characters in the file.
indigo 21% icount
Enter input file name: phone_book.txt
Count lines, words, characters or all three (1, m, c, a)? b.
Invalid option
Count lines, words, characters or all three (1, m, c, a)? m
File 'phone_book.txt' contains 125 words.
Problem 3
Write a script named all files to allow a user to list all (ordinary) files in the current
directory, one per line, but not the sub-directories in the current directory. (Note:
command '1s' displays all files and directories.)
indigo 22% allfiles
allfiles
lab3a.c
lab3b.c
lab3b_out.txt
Problem 4
Write a script named fdisplay to allow a user to input a file name and an option to
display the content of the file (entire file, entire file one page at a time, first 10 lines, last 10
lines). Display the content of the file as the user specifies. If the user enters an invalid
option, display an error message and ask for a correct option.
indigo 23 fdisplay
Enter input file name: phone_book.txt
Enter option (e, p, f, 1): a
Invalid option
Enter option (e, p, f, 1): p
File is displayed one page at a time
Problem 5
Write a script named doublesp to allow a user to input an input and an output file name.
Copy the content from the input file to the output file, adding a blank line between every
two lines of the input file (double spacing). If the output file exists, display the message
shown below. If the user answers yes then append the output to the file. If the user
answers no, exit and do nothing (i.e., do not overwrite an existing file).
indigo 24 doublesp
Enter input file name: phone_book.txt
Enter output file name: outfile.txt
File outfile.txt' exists.
Append to it (y/n)? y
Problem 6
Write a script named addlines to allow a user to enter an input and an output file name.
Copy the content from the input file to the output file, line by line, adding a line number at
the beginning of each copied line. Add a space after the colon before copying the line.
If the output file exists, display a message as above. If the user answers yes then append
the output to the file. If the user answers no, exit and do nothing.
indigo 25 addlines
Enter input file name: phone_book.txt
Enter output file name: outfile.txt
indigo 26 cat outfile.txt
1: Ray Szeto
2: alex johnson
3: Tom Podstawka
(416) 564-6786 friend
(416) 555-1234 family doctor
(647) 579-3854
dentist
Notes
⚫ An input file must exist and be readable before being read from.
⚫ An existing output file must be writable before being written to.
.
Assume that all inputs from the user are case-insensitive, except file names. (Unix file
names are case-sensitive, so we keep the same convention.) For example, the user may
enter either 'y' or 'Y'.
Assume that all input files are text files containing no back slash characters.
See file unix_output.txt for more examples.
Do not use any text processing utility such as "sed" or "awk".
• Make sure to use the same error messages or confirmation messages as specified,
because the automatic grading script will use "grep" to look for these messages.
• Make sure to name the submitted files (scripts) exactly as specified, because the
automatic grading script will to look for those exact file names.
⚫ Submit only the Unix scripts. Do not submit output files.
Sample Error Messages
Enter input file name: yourfile.txt
File 'yourfile.txt' does not exist.
Enter input file name: ourfile.txt
File 'ourfile.txt' is not readable.
Enter output file name: hisfile.txt
File 'hisfile.txt' is not writable.
Enter output file name: outfile.txt
File 'outfile.txt' exists.
Append to it (y/n)? n
In the above examples, simply exit the script upon a file error, or an answer 'n' or 'N'.
indigo 52% isearch
Enter input file name: UFO.txt
File UFO.txt' does not exist.
indigo 53% isearch
Enter input file name: ghost.txt
File 'ghost.txt' is not readable.
indigo 55% isearch
Enter input file name: phone_book
Enter the word to search for: alex
Case-sensitive (y/n)? n
alex johnson
(416) 555-1234
family doctor
Alexander Smith (905) 555-9876 home renovation
contractor
indigo 56% isearch.
Enter input file name: phone_book
Enter the word to search for: alex
Case-sensitive (y/n)? Y
alex johnson
(416) 555-1234 family doctor
indigo 69% isearch
Enter input file name: phone_book
Enter the word to search for: Alex
Case-sensitive (y/n)? Z
Invalid option
Case-sensitive (y/n)? a
Invalid option
Case-sensitive (y/n)? Y
Alexander Smith (905) 555-9876 home renovation
contractor
indigo 71% isearch.
Enter input file name: phone_book
Enter the word to search for: microwave
Case-sensitive (y/n)? Y
Word 'microwave' not found.
indigo 72%
indigo 55% icount
Enter input file name: UFO.txt
File UFO.txt' does not exist.
indigo 56% icount
Enter input file name: ghost.txt
File 'ghost.txt' is not readable.
indigo 59% icount
Enter input file name: phone_book
Count lines, words, characters or all three (1, m,
c, a)? Z
Invalid option
Count lines, words, characters or all three (1, m,
c, a)? t
Invalid option
Count lines, words, characters or all three (1, m,
c, a)? L
File 'phone_book' contains 23 lines.
indigo 60% icount
Enter input file name: phone_book
Count lines, words, characters or all three (1, m,
c, a)? M
File 'phone_book' contains 125 words.
indigo 61% icount
Enter input file name: phone_book
Count lines, words, characters or all three (1, m,
c, a)? c
File 'phone book' contains 985 characters.
indigo 62% icount
Enter input file name: phone_book
Count lines, words, characters or all three (1, m,
c, a)? a
File 'phone_book' contains 23 lines, 125 words, 985
characters.
indigo 63%
indigo 75% doublesp
Enter input file name: phone_book
Enter output file name: ghost.txt
File 'ghost.txt' not writable.
indigo 76 doublesp
Enter input file name: phone_book
Enter output file name: new.txt
indigo 77 doublesp
Enter input file name: phone_book
Enter output file name: new.txt
File 'new.txt' exists.
Append to it (y/n)? K
Invalid option
Append to it (y/n)? b.
Invalid option
Append to it (y/n)? n
indigo 78% doublesp
Enter input file name: phone_book
Enter output file name: new.txt
File new.txt' exists.
Append to it (y/n)? Y
indigo 79% cat new.txt
This line was in the file before "doublesp" was
executed.
This line also existed before "doublesp" was
executed.
Ray Szeto
alex johnson
Tom Podstawka
(416) 564-6786 friend
(416) 555-1234 family doctor
(647) 579-3854 dentist
(905) 789-6703 neighbour
Greg Ivan.
Nikola Sandic
(647) 209-7932 friend from work
Tim Yu
(416) 570-9522 Jill's coach
indigo 80% cat phone_book
Ray Szeto
alex johnson
(416) 564-6786 friend
family doctor
(416) 555-1234
(647) 579-3854 dentist
neighbour
Tom Podstawka
Greg Ivan
Nikola Sandic
(905) 789-6703
(647) 209-7932
Tim Yu
(416) 570-9522
friend from work
Jill's coach
check_circle الجواب — حل مفصل خطوة بخطوة
hourglass_top
🔒
الحل الكامل متاح للمشتركين
اشترك في أرشيف الأسئلة لعرض هذا الحل وآلاف الحلول المفصلة خطوة بخطوة من معلمين معتمدين.