bash | 代写shell | assignment代写 – Assignment #1: Linux

Assignment #1: Linux

bash | 代写shell | assignment代写 – 本题是一个利用shell进行练习的代做, 对shell的流程进行训练解析, 是比较典型的bash/shell等代写方向, 该题目是值得借鉴的assignment代写的题目

ass代做 assignment代写 代写assignment

Questions 1 and 2 are due on Due Date 1; the remainder of the assignment is due on Due Date 2.

Topics that must have been completed before starting Due Date 1:
  1. Linux: The Teaching Environment
  2. Linux: Interacting with the Shell
  3. Linux: Directories and Files
  4. Linux: Regular Expressions
  5. Handouts: Getting Started
  6. Handouts: Linux Commands
Topics that must have been completed before starting Due Date 2:
  1. Linux: shell 代做 script代写”> bash scripts
  2. Software Testing

Note 1:We suggest creating the directory~/cs246/s21/a1and creating all the assignment solutions in this directory. Note 2:If you finish A1 DD1 i.e. questions 1 and 2 early, westronglyrecommend that you start the other questions, since debugging bash scripts can take a lot of time. Make sure that you take advantage of using the-xoption to the shbang line (thoughremove it from your submitted scripts!) if your scripts arent behaving as expected, so that you can see exactly what is happening.

Testing tools

Note: the scripts you write in the following questions will be useful every time you write a program. Be sure to complete them!In this course, you will be responsible for your own testing. As you fix bugs and refine your code, you will very often need to rerun old tests, to check that existing bugs have been fixed, and to ensure that no new bugs have been introduced. This task isgreatlysimplified if you take the time to create a formal test suite, and build tools to automate your testing. In the following questions, you will implement such tools asbashscripts. These tools will assume that the suite and test files used by the scripts exist is the same directory as the scripts.

Hints
  • Lecture Software Testing: Examples gives an example of how theproduceOutputsandrunSuitescripts (Q3-Q5) should be used.
  • Lecture Pipes gives an example of how to substitute the output of one bash command as the arguments to another bash command.
  • Lecture Exercise: good password gives an example of how to redirect standard output to standard error.

Note that questions 3-5 all follow the same error-handling in terms of validating the command-line arguments for each ofproduceOutputsandrunSuite. The following sample execution shows you that the error messages are being redirected to the standard error stream, and that the shell script exit code is non-zero.

$ ./produceOutputs 1> err1.stdout 2> err1.stderr $ echo $? 1 $ cat err1.stdout $ cat err1.stderr Incorrect number of arguments $ ./produceOutputs test_suite.txt 1> err2.stdout 2> err2.stderr $ echo $? 1 $ cat err2.stdout $ cat err2.stderr Incorrect number of arguments $ ./produceOutputs no_read.args ./no_such_pgm 1> err3.stdout 2> err3.stderr $ echo $? 1 $ cat err3.stdout $ cat err3.stderr ./no_such_pgm is not executable $ ./produceOutputs no_read.args ./no_such_pgm 1> err4.stdout 2> err4.stderr $ echo $? 1 $ cat err4.stderr ./no_such_pgm is not executable $ ./produceOutputs no_read.args ./my_factorial_correct 1> err4.stdout 2> err4.stderr $ echo $? 1 $ cat err4.stderr no_read.args is not readable

  1. 5 marks.Write abashscript calledproduceOutputsthat is invoked as follows:
./produceOutputs suite-file program
The argumentsuite-fileis the name of a file containing a list of filename stems (more details below), and the
argumentprogramis the name of a program to be run.
TheproduceOutputsscript runsprogramon each test in the test suite and, for each test, creates a file that contains
the output produced for that test.
The filesuite-filecontains a list of stems, from which we construct the names of files containing the command-line
arguments used by each test. Stems will not contain spaces. For example, suppose our suite file is calledsuite.txt
and contains the following entries:
test1 test
reallyBigTest
Then our test suite consists of three tests. The first one (test1) will use the filetest1.args. The second one
(test2) will use the filetest2.args. The last one (reallyBigTest) will use the filereallyBigTest.args.
A sample run ofproduceOutputswould be as follows:
./produceOutputs suite.txt ./myprogram
The script will then run./myprogramthree times, once for each test specified insuite.txt:
  • The first time, it will run./myprogramwith command-line arguments provided to the program fromtest1.args. The results, captured from standard output, will be stored intest1.out.
  • The second time, it will run./myprogramwith command-line arguments provided to the program fromtest2.args. The results, captured from standard output, will be stored intest2.out.
  • The third time, it will run./myprogramwith command-line arguments provided to the program from reallyBigTest.args. The results, captured from standard output, will be stored inreallyBigTest.out.
Note that if the test suite contains a stem but a corresponding.argsfile is not present, the program is run without
providing any command-line arguments.
Your script must also check for incorrect number of command-line arguments toproduceOutputs. If such an error
condition arises, print an informative error message to standard error (the exact message is up to you) and abort the
script with anon-zeroexit status. For example, runningproduceOutputswith invalid.argsfiles would produce
output and exit codes similar to the following:
$ ls -l
total 20
-rwxr-x--x 1 ctkierst ctkierst 14560 May 4 10:08 my_factorial_correct
--w-r----- 1 ctkierst ctkierst 2 May 4 10:09 no_read.args
-rw-r----- 1 ctkierst ctkierst 0 May 4 10:13 no_read.out
-rw-r----- 1 ctkierst ctkierst 2 May 4 10:08 no_write.args
-r--r----- 1 ctkierst ctkierst 0 May 4 10:08 no_write.out
-rwxr-x--x 1 ctkierst ctkierst 491 May 4 10:09 produceOutputs
-rw-r----- 1 ctkierst ctkierst 0 May 4 10:08 test0.args
-rw-r----- 1 ctkierst ctkierst 2 May 4 10:08 test1.args
-rw-r----- 1 ctkierst ctkierst 3 May 4 10:08 test2.args
-rw-r----- 1 ctkierst ctkierst 2 May 4 10:08 test3.args
-rw-r----- 1 ctkierst ctkierst 24 May 4 10:14 test_suite.txt
-rw-r----- 1 ctkierst ctkierst 41 May 4 10:14 tsuite_V2.txt
$ ./produceOutputs test_suite.txt ./my_factorial_correct 1> tsuite.stdout 2> tsuite.stderr
$ echo $?
0

$ cat tsuite.stdout $ cat tsuite.stderr $ ./produceOutputs tsuite_V2.txt ./my_factorial_correct 1> tsuite_V2.stdout 2> tsuite_V2.stderr $ echo $? 1 $ cat tsuite_V2.stderr ./produceOutputs: line 31: no_write.out: Permission denied $ cat test0.args $ cat test0.out $ cat test1.args 5 $ cat test1.out 5! = 120 $ ./produceOutputs test_suite.txt ./my_factorial_correct 1> tsuite.stdout 2> tsuite.stderr $ echo $? 0 $ cat tsuite.stdout $ cat tsuite.stderr $ cat test0.args $ cat test0.out $ cat test1.args 5 $ cat test1.out 5! = 120 $ cat test3.args 0 $ cat test3.out 0! = 1 $ cat test_suite.txt test test test test $ cat tsuite_V test test test test no_read no_write

You can find an example of the expected output ofproduceOutputsin the filea1/q3in your Git repository. Using the contents of this directory, running your script as:

./produceOutputs test_suite.txt ./my_factorial_correct

should produce files namedtest0.out,test1.out,test2.out, andtest3.outidentical to those provided to you as examples in the same directory.

Note on purpose of this script:This script will be useful in situations where we provide you with a binary version of a program (but not its source code) that you must implement. By creating your own test cases (.argsfiles) and then using this script to produce the intended output you will have something to compare with when you implement your own solution (see the next question for how to automate the comparisons).

  1. 10 marks.Create abashscript calledrunSuitethat is invoked as follows:
./runSuite suite-file program
The argumentsuite-fileis the name of a file containing a list of filename stems (more details below), and the
argumentprogramis the name of the program to be run.
In summary, therunSuitescript runsprogramon each test in the test suite (as specified bysuite-file) and
reports on any tests whose output does not match the expected output.
The filesuite-filecontains a list of stems, from which we construct the names of files containing the command-
line arguments and expected output of each test. Stems will not contain spaces. For example, suppose our suite file is
calledsuite.txtand contains the following entries:
test1 test
reallyBigTest
Then our test suite consists of three tests. The first one (test1) will use the filetest1.argsto hold its command-
line arguments, andtest1.outto hold its expected output. The second one (test2) will use the filetest2.args
to hold its command-line arguments, andtest2.outto hold its expected output. The last one (reallyBigTest)
will use the filereallyBigTest.argsto hold its command-line arguments, andreallyBigTest.outto hold
its expected output.
A sample run ofrunSuitewould be as follows:
./runSuite suite.txt ./myprogram
The script will then run./myprogramthree times, once for each test specified insuite.txt:
  • The first time, it will run./myprogramwith command-line arguments provided to the program fromtest1.args. The results, captured from standard output, will be compared with the contents oftest1.out.
  • The second time, it will run./myprogramwith command-line arguments provided to the program fromtest2.args. The results, captured from standard output, will be compared with the contents oftest2.out.
  • The third time, it will run./myprogramwith command-line arguments provided to the program from reallyBigTest.args. The results, captured from standard output, will be compared with the contents of reallyBigTest.out.
Note that if the test suite contains a stem but a corresponding.argsfile is not present, the program is run without
providing any command-line arguments.
If the output of a given test case differs from the expected output, print the following to standard output (assuming test
test2failed):
Test failed: test
Args:
(contents of test2.args, if it exists)
Expected:
(contents of test2.out)
Actual:
(contents of the actual program output)
with the(contents ...)lines replaced with actual file contents, without any changes, as described. The literal
outputArgs:must appear, even if the corresponding file does not exist. Note that there is no whitespace after the
colon for each ofArgs:,Expected:, andActual:except for the newline character.
An example of such execution would look like:

$ cat test_suite2.txt test test test test test test test no_such_file $ ./runSuite test_suite2.txt ./my_factorial_buggy 1> tsuite2.stdout 2> tsuite2.stderr $ echo $? 1 $ cat tsuite2.stdout Test failed: test Args: 0 Expected: 0! = 1 Actual: 0! = 0 Test failed: test Args:

Expected: -3! = 1 Actual: $ cat tsuite2.stderr ./runSuite: line 27: 165558 Segmentation fault "$prog" $cmd_args > "$testfile" File no_such_file.out does not exist or is not readable

Follow these output specificationsvery carefully. You will lose a lot of marks if your output does not match them. If you need to create temporary files, create them in/tmp, and use themktempcommand to prevent name duplications. Also be sure to delete any temporary files you create in/tmp.

You can find an example of the expected output ofrunSuitein the directorya1/q4on your Git repository. Using the contents of this directory, running your script as:

./runSuite test_suite.txt ./my_factorial_buggy

should produce an output identical to the example provided to you in the filebuggytestresults.outin the same directory.

Note: DoNOTattempt to compare outputs by storing them in shell variables, and then comparing the shell variables. This is a very bad idea, and it does not scale well to programs that produce large outputs. We reserve the right to deduct marks (on this and all assignments) for bad solutions like this would be.

You can get most of the marks for this question by fulfilling the above requirements. For full marks, your script must also check for the following error conditions:

  • incorrect number of command-line arguments torunSuite
  • missing or unreadable.outfiles (for example, the suite file contains an entryxxx, butxxx.outdoesnt exist or is unreadable).

If such an error condition arises, print an informative error message to standard error and abort the script with anon-zero exit status.

  1. 15 marks.In this question, you will generalize theproduceOutputsandrunSuitescripts that you created in questions 3 and 4. As they are currently written, these scripts cannot be used with programs that take input from standard input. For this problem, you will enhanceproduceOutputsandrunSuiteso that, in addition to (optionally) passing command-line arguments to the program being executed, the program can also be (optionally) provided input from standard input. The interface to the scripts remains the same:
./produceOutputs suite.txt ./myprogram
./runSuite suite.txt ./myprogram
The format of the suite file remains the same. But now, for eachtestnamein the suite file, there might be an
optionaltestname.in. If the filetestname.inis present, then the script (produceOutputsorrunSuite)
will runmyprogramwith the contents oftestname.argspassed on the command-line as before and the contents
oftestname.inused for input onstdin. Iftestname.inis not present, then the behaviour is almost identical
to question 3/4 (see below for a difference in the output):myprogramis run with command-line arguments coming
fromtestname.argswith nothing supplied as input on standard input.
The output ofrunSuiteis changed to now also show the input provided to a test if the test failed. Assuming test
test2from Q4 failed, the output generated by the updatedrunSuiteis as follows:
Test failed: test
Args:
(contents of test2.args, if it exists)
Input:
(contents of test2.in, if it exists)
Expected:
(contents of test2.out)
Actual:
(contents of the actual program output)
with the(contents ...)lines replaced with actual file contents, as described. The literal outputArgs:and
Input:must appear, even if the corresponding files do not exist. Note that there is no whitespace after the colon for
each ofArgs:,Input:,Expected:, andActual:except for the newline character.
You can find an example of the expected output of the updatedproduceOutputsandrunSuitein the directory
a1/q5on your Git repository. Using the contents of this directory, running your script as:
./produceOutputs test_suite.txt ./my_factorial_correct
should produce files namedtest0.out,test1.out,test2.out,test3.out, andtest4.outidentical to
those provided to you as examples in the same directory.
And using the contents of this directory, running your script as:
./runSuite test_suite.txt ./my_factorial_buggy
should produce an output identical to the example provided to you in the filebuggytestresults.outin the
same directory.
All error-checking that was required in questions 3 and 4 is required here as well.
(a) ModifyproduceOutputsto handle input from standard input.
(b) ModifyrunSuiteto handle input from standard input.
Note:To get this working should require only very small changes to your solution to questions 3 and 4.

Submission

The following files are due at Due Date 1:a1q1a.txt,a1q1b.txt,a1q1c.txt,a1q1d.txt,a1q1e.txt,a1q1f.txt, a1q1g.txt,a1q1h.txt,a1q1i.txt,a1q1j.txt,a1q2a.txt,a1q2b.txt,a1q2c.txt,a1q2d.txt,a1q2e.txt. The following files are due at Due Date 2:produceOutputs,runSuite,produceOutputs,runSuite.