shell script讲解、辅导c++设计、C/C++编程语言调试

- 首页 >> Database
1. Write a Bash shell script srclines.sh that accepts a
directory as its input and then displays the names of shell
scripts (.sh files) in the directory along with the number of
lines of code in it. The script should ignore lines with only
comments in it, as well as lines with no code in it (empty line
or lines with only spaces in it). For simplicity, you may assume
that the definition of "space" does not include tab characters.
The script should also display a totals for the number of shell
scripts and number of lines at the end.
Your shell script should display an appropriate error message
and terminate if it is not passed an argument or if the
arguments passed is not the name of a valid directory. The
script should also ignore any sub directories (as well as files
under them) that are inside the original directory passed as its
argument. If there are no shell scripts under the directory,
display that information as a message.

$ ./srclines.sh dir1
script1.sh 19
script2.sh 32
Total of 2 files with 51 lines of code
$
GRADING:
(i) 1 point for writing comments about logic throughout the
code (you need not include your personal information in the
beginning of the code).
(ii) 1 point for checking sufficient argument and displaying
messages.
(iii) 1 point for checking that the argument is valid (directory
and such), checking there are shell scripts in directory, etc.
(iv) 1 point for ignoring subdirectory structures.
(v) 6 points for executing the correct program logic.
2. Assume you are given the following c code:
#include
#include
char *getInfo() {
char buffer[1000];
scanf("%s", buffer);
return buffer;
}
int main() {
char *ptr;
ptr = getInfo();
printf("%s\n", ptr); // <--- error
return 0;
}
The above code has an error identified at a particular line.
Assume no other lines of code have errors. Answer the
following questions:
(a) If you were using gdb to find the error, what commands
would you use? Write all the commands, including launching
gdb for the first time. Assume the executable is called a.out.
(b) Why is this line of code wrong?
Write you answer in gdb.txt.
3. (A) Write a C Program rev.c that reverses the argument
passed to it. You can assume that only one argument is
passed to the program. Your program must not use any arrays
(other than the one used to capture the arguments passed to
the main). You can ignore the situation where the program is
invoked without being passed an argument. Below is an
example of it's invocation.
$ ./rev Hello
olleH
$
(B) Now write a Bash shell script revname.sh that accepts a
filename as the argument and renames the file using the
above program. The script should throw an appropriate usage
message if it is not passed a filename argument. It should
also throw an appropriate error message if it cannot find the
filename passed as argument. If there is already a file with the
new name, then the script should throw an appropriate
message and not proceed. You may choose to ignore other
error situations and also assume that the file is in the current
directory. Following is an instance of a successful run of the
script.
$ ls
rev.c rev revname.sh main.c
$ ./revname.sh main.c
$ ls
rev.c rev revname.sh c.niam
$
Any error message displayed by your shell script must be from an
explicit echo command from your shell script, and not an error
message thrown by a command/statement you used in your script.
GRADING:
(A)
(i) 1 point for writing comments about logic throughout the
code (you need not include your personal information in the
beginning of the code).
(ii) 9 points for correctly performing the word checks.
(B)
(i) 1 point for writing comments about logic throughout the
code (you need not include your personal information in the
beginning of the code).
(ii) 1 point for checking sufficient/valid argument (file exists)
and displaying messages.
(iii) 3 points for checking the existence of target filename,
renaming the source file.
4. Write a modular C program having the following modules:
main.c and replace.c. The program will be invoked from the
command-line this way:
./update /bob/filename word1 word2
Construct a makefile that will compile the program in the
ways specified in this question and following proper modular
programming rules (for example using .h files and
implementing private/public).
In summary, the program does the following: it opens the text
file provided at the command-line and replaces all
occurrences of word1 by word2 from within the file. When the
program ends, the file's contents have changed (word1
occurrences are replaced by word2).
Do the following:
1. The arguments at the command line follow these rules: the
filename may or may not have a path, both word1 and word2
are single words without spaces and without special
characters like a punctuation. Assume the user inputs the
arguments correctly. Do not do data validation.
2. You are not permitted to load the entire file into memory.
You are not permitted to load an entire line of the file into
memory. You CAN read an entire word into memory or you
can read the file character by character.
3. You CAN write helper functions. You can open more than
one text file, if you want, but at the end only the original text
file must exist.
4. The module replace.c is responsible for detecting that the
word read from the file matches word1. If it does match
word1 then it returns word2, otherwise it returns word1.
5. The module main.c controls the file processing (reading
and writing to the file) and it calls the functions in replace.c,
receiving the return value of word2 or not.
6. The only libraries you can use are: math.h, string.h, stdio.h,
and stdlib.h, BUT only the functions covered during class. Do
not use other advanced functions not covered during the
lectures.
Git question: In addition to the above programming question,
assume this program uses git to backup the source files.
Assume further two developers were working together on this
program. Write the git commands to handle the following
case: developer 1 is editing replace.c, but developer 2 wants
to edit replace.c at the same time. (A) How can git help them
do this? (write the git commands), and (B) How can they
overcome a conflict, assuming the two replace.c files have a
git conflict? (write the git commands). Write your answer in
file git.txt.
GRADING:
(a) 5 points: modular programming and .h files
(b) 5 points: makefile
(c) 5 points: main.c module with optional main.h
(d) 5 points: replace.c module with optional replace.h
(e) 5 points: git question answer in git.txt

站长地图