diff options
| author | mo <mo.khan@gmail.com> | 2019-05-25 14:53:54 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2019-05-25 14:53:54 -0600 |
| commit | 64baefb6c8fdbd7ac5d78d2037eff4c5ec68de3d (patch) | |
| tree | c414656d45bc06ae04063056f8fedd097fd194da /README.md | |
| parent | a2a32c22acdc6e06963ffa7c0f57a43570e64cce (diff) | |
start assignment 2
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 621 |
1 files changed, 31 insertions, 590 deletions
@@ -1,602 +1,43 @@ -# COMP-325 Assignment 1 - mo khan (3431709) +# COMP-325 Assignment 2 - mo khan (3431709) -I have completed this assignment on two different machines. +* Write shell scripts specified in problem 6 at the end of Chapter 16. +* Write shell scripts specified in problem 7 at the end of Chapter 16. +* shell scripts specified in problem 13 at the end of Chapter 16. -1. macOS 10.14 -2. Fedora release 30 +## Chapter 16 -For some sections, I chose to run a `Fedora` docker container to isolate -the impact to my host system. The captured output from the `script` -command is placed in the `exercises` directory and named appropriately. -In most cases, I have provided a formatted version of the output in this -document to save the reader from having to jump back and forth. +### Problem 6 -The source code for the `c` program is placed in the `src` directory. A -minimal `Makefile` is also provided for the reader. +Write a Bourne shell script that takes a list of integers as the command line argument and displays a list of their squares and the sum of the numbers in the list of squares. -This assignment has been placed in a private git repository hosted on -`GitHub`. Access to this repository is available upon request. +### Problem 7 -Thanks, enjoy and feedback is always welcome and appreciated! +Write a Bourne shell script that takes a machine name as an argument and displays a message informing you whether the host is on the local network. -mo (mo@mokhan.ca) +### Problem 13 -## Solve problems 7–11 and 15–17 at the end of Chapter 7. +Write a Bourne shell script for Problem 16 in Chapter 15, but use functions to implement the service code for various options. -7. Give three commands that you can use to list the absolute pathname of your home directory. - -```bash -$ echo $HOME -/home/mokha -$ echo ~/ -/home/mokha -$ cd -$ pwd -/home/mokha -``` - -See `exercises/chapter-7.7`. - -8. Write down the line in the /etc/passwd file on your system that contains information about your login. What are your login shell, user ID, home directory, and group ID? Does your system contain the encrypted password in the /etc/passwd or /etc/shadow file? - -```text -$ grep mokha /etc/passwd -mokha:x\:1000:1000:mo:/home/mokha:/bin/bash - -$ grep mokha /etc/shadow -mokha:password:18036:0:99999:7::: -``` - -* user id: 1000 -* group id: 1000 -* `$HOME`: /home/mo -* shell: /bin/bash -* Password is stored in `/etc/shadow` - -See `exercises/chapter-7.8`. - -9. What would happen if the last field of the line in the `/etc/passwd` file were replaced with `/usr/bin/date`? Why? - -The login shell would become the `date` command. The `date` program prints the date. This would print the date and end. - -```bash -$ date -Wed May 22 20:07:38 MDT 2019 -``` - -10. What are the inode numbers of the root and your home directories on your machine? Give the commands that you used to find these inode numbers. - -```bash -$ ls -id / -$ ls -id $HOME -``` - -See `exercises/chapter-7.10`. - -11. Create a directory, called `memos`, in your home directory. Go into this directory and create a file `memo.james` by using one of the editors discussed in Chapter 5. Give three pathnames for this file. - -```bash -$ cd -$ mkdir -p memos -$ cd memos/ -$ vim memo.james -$ ls ./memo.james -./memo.james -$ ls ~/memos/memo.james -/Users/mokha/memos/memo.james -$ ls /Users/mokha/memos/memo.james -/Users/mokha/memos/memo.james -``` - -See `exercises/chapter-7.11`. - -15. Give the command for deleting the memos directory. How do you know that the directory has been deleted? - -```bash -$ cd -$ rm -fr memos/ -$ ls -alh memos -``` - -See `exercises/chapter-7.15`. - -16. Why does a shell process terminate when you press <Ctrl-D> at the beginning of a new line? +Modify the script in the `case_demo` file in Section 15.6.6 so that it allows you to try any number of options and quits only when you use the q option. ```text -The ^D represents the eof or end of file. -``` - -```bash -$ stty -a -speed 9600 baud; 14 rows; 238 columns; -lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl - -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo - -extproc -iflags: -istrip icrnl -inlcr -igncr -ixon ixoff ixany imaxbel iutf8 - -ignbrk brkint -inpck -ignpar -parmrk -oflags: opost onlcr -oxtabs -onocr -onlret -cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow - -dtrflow -mdmbuf -cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>; - eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V; - min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T; - stop = ^S; susp = ^Z; time = 0; werase = ^W; -``` - -17. Give a command line to display the types of all the files in your `∼/unix` directory that start with the word chapter, are followed by a digit 1, 2, 6, 8, or 9, and end with .eps or .prn. - -```bash -$ cd ~/unix && ls . | grep -e 'chapter[12689]\.[eps|prn]' | xargs file -chapter1.eps: empty -chapter1.prn: empty -chapter2.eps: empty -chapter6.eps: empty -chapter8.eps: empty -chapter9.eps: empty -``` - -See `exercises/chapter-7.17`. - -## Solve problems 7–17 at the end of Chapter 8. - -7. How do the read, write, and execute permissions work in UNIX? Illustrate your answer with some examples. - -Permission bits are organized as follows: - -| - | r | w | x | r | w | x | r | w | x | - -* The first bit indicates entry type. (d: directory, c: character special file, l: symbolic link, s: socket) -* The following 3 bits represent the read, write, execute permissions for the `user`. -* The following 3 bits represent the read, write, execute permissions for the `group`. -* The following 3 bits represent the read, write, execute permissions for `everyone` else. - -E.g - -```bash -$ ls -alh README.md --rw-r--r-- 1 mokha staff 7.8K 18 May 16:20 README.md -``` - -* The `-` indicates a file. -* The `rw-` indicates read and write permissions for the owner of the file. -* The following `r--` indicate read permissions for members of the group. -* The following `r--` indicate read permissions for everyone else. - -8. How many user groups exist on your system? - -There are 3 groups on my system. - -```bash -$ groups -mokha wheel docker -``` - -What groups are you a member of and what is your default group? - -I am a member of the groups `mokha`, `wheel` and `docker`. `mokha` is my -default group. - -```bash -$ groups mokha -mokha : mokha wheel docker -``` - -How many groups is root a member of, on your system? - -```bash -$ groups root -root : root -``` - -See `exercises/chapter-8.8`. - -9. Create a file `test1` in your present working directory and set its access privileges to read and write for yourself, -read for the users in your group, and none to everyone else. -What command did you use to set privileges? -Give another command that would accomplish the same thing. - -```bash -$ touch test1 -$ chmod 640 test1 -$ ls -alh test1 --rw-r----- 1 mokha staff 0B 18 May 18:16 test1 -$ chmod u=rw,g=r,o= test1 --rw-r----- 1 mokha staff 0B 18 May 18:17 test1 -``` - -See `exercises/chapter-8.9`. - -10. The user `sarwar` sets access permissions to his home directory by using the command `chmod 700 $HOME`. -If the file `cp.new` in his home directory has read permissions to `777`, can anyone read this file? Why or why not? Explain your answer. - -Yes. The final seven in the permissions means read, write, execute -for all other users. - -| Read (4) | Write (2) | Execute (1) | -| ------------- | ------------- | ------------- | -| x | x | x | - -11. What is the effect of each command? Explain your answers. - -The following will change the permissions for a file named `lab5` in the -`$HOME` directory to: - -* read, write, execute for the owner (4 + 2 + 1 = 7) -* read, write, execute for the group (4 + 2 + 1 = 7) -* read and write for everyone (4 + 2 = 6) - -```bash -$ chmod 776 ∼/lab5 -``` - -The following will change the permissions for any files that start with -lab and is followed by any optional single character in the `$HOME` directory. - -* read, write, execute for the owner (4 + 2 + 1 = 7) -* read, execute for the group (4 + 1 = 5) -* execute for everyone (1 = 1) - -```bash -$ chmod 751 ∼/lab? -``` - -Change the permissions for all files in the current directory that end -with a `.c` file extension. - -* read, execute for the owner (4 + 1 = 5) -* execute for the group (1 = 1) -* execute for everyone (1 = 1) - -```bash -$ chmod 511 *.c -``` - -Change permissions for all files in the `$HOME` directory. - -* read, write, and execute for the owner (4 + 2 + 1 = 7) -* execute for the group (1 = 1) -* execute for everyone (1 = 1) - -```bash -$ chmod 711 ∼/* -``` - -List files in current directory using the long listing format. - -```bash -$ ls -l -``` - -List the current directory in long list format. - -```bash -$ ls -ld -``` - -List files/directories in `$HOME/personal` directory using the long list -format. - -```bash -$ ls -l ∼/personal -``` - -List the directory `$HOME/personal` in long list format. - -```bash -$ ls -ld ∼/personal -``` - -12. What does the execute permission mean for a directory, a file type for which the execute operation makes no sense? - -The execute permission on a directory allows you to enter it. .ie. `cd directory`. - -13. Create a file `dir1` in your home directory and use `cp /etc/passwd dir1/mypasswd` command to copy the `/etc/passwd` file in it. Use the `chmod` command to have only the search permission on for it and execute the following commands. What is the results of executing these commands? Do the results make sense to you? Explain. - -Change directory to a directory named `dir1`. - -```bash -$ cd dir1 -``` - -List the contents of the current directory. -```bash -$ ls -``` - -Remove the file named `mypasswd` from the directory named `dir1`. - -```bash -$ rm dir1/mypasswd -``` - -Copy the file from `/etc/passwd` to the directory named `dir1`. -```bash -$ cp /etc/passwd dir1 -``` - -See `exercises/chapter-8.13`. - -14. What `umask` command should be executed to set the permissions bit mask to `037`? With this mask, what default access privileges are associated with any new file that you create on the system? Why? Where would you put this command so that every time you log on to the system this mask is effective? - -The `umask` indicates which permission bits to turn off. ie. `777 - mask`. - -To change the default umask to `037`. - -```bash -$ mkdir -p before -$ umask 037 -$ mkdir -p after -$ ls -alh before after -drwxr-----. 2 root root 4.0K May 25 19:31 after -drwxr-xr-x. 2 root root 4.0K May 25 19:31 before -``` - -The default access privileges are: - -```text -777 - 037 = 740 - -rwx|r--|--- -``` - -User: Read, write, execute. -Group: Read -Everyone: no access - -The `umask` can be placed in any startup scripts such as: - -* $HOME/.profile (System V UNIX) -* $HOME/.login (BSD UNIX) -* $HOME/.bash\_profile - -See `exercises/chapter-8.14`. - -15. Give a command line for setting the default access mode so that you have read, write, and execute privileges, your group has read and execute permissions, and all others have no permission for a newly created executable file or directory. How would you test it to be sure that it works correctly? - -I can use the `umask` builtin to set the default access privileges. I -can test this by creating a file before the change and then creating a -file after the change. Then I can list the permissions for both the -before and after files to witness the difference. I can also run `umask` -to see what the current setting is. - -To change the default access privileges to `rwx|r-x|---` (750) I can use the -formula: - -```text -777 - 750 = 027 -``` - -The desired `umask` value would be `027`. - -```bash -$ mkdir before -$ umask 027 -$ mkdir after -$ ls -l -drwxr-x---. 2 root root 4096 May 25 19:36 after -drwxr-xr-x. 2 root root 4096 May 25 19:36 before -``` - -See `exercises/chapter-8.15`. - -16. Give `chmod` command lines that perform the same tasks that the `mesg n` and `mesg y` commands do. (Hint: Every hardware device, including your terminal, has an associated file in the /dev directory.) - -`mesg n` blocks write access to the currently logged in users `tty` by -any other users. `mesg y` allows write access to the currentl logged in -users `tty` session. - -To accomplish the same tasks without using `mesg` you will need to -identity which device the current tty is attached to. This can be done -by using the `tty` program. Once the device is identified, you can -change the permissions for that file. - -```bash -$ tty -/dev/pts/6 -$ ls -alh /dev/pts/6 -crw--w----. 1 mokha 136, 6 May 25 13:45 /dev/pts/6 -$ chmod 600 /dev/pts/6 -``` - -See `exercises/chapter-8.16`. - -17. What are the purposes of the set-user-ID (SUID), set-group-ID (SGID), and sticky bits? - -The purpose of the `SUID` and the `SGID` is to allow one user to run a -program on behalf of or another user. A common example is when a program -needs to write to a file that is owned by root. If the program is owned -by `root` and the `SUID` is permission is enabled on that program, then -a non-root user can run that program as if the `root` user was running -the program. This allows for non root users to do things like change -their passwords etc. This feature can also sometimes be abused if the -program with the `SUID` is on isn't written properly. e.g. buffer -overflow leading to arbitrary code execution. - -The `SGID` operates very similar to the `SUID` except that the program -runs as if it were a member of the group that owns the file. - -The `sticky bit` is used to ensure a user cannot remove files from a -directory owned by another user. This is common for shared folders like -`/tmp`. - -## Do problem 9 (What do the following commands do?) in Chapter 9. - -What do the following commands do? - -Force copy `sample` to `sample.bak`. - -```bash -$ cp -f sample sample.bak -``` - -Force copy `sample` to `sample.bak` and preserve the mode, ownership and timestamps from the original file. - -```bash -$ cp -fp sample sample.bak -``` - -Remove all files from `$HOME/personal/` that start with `memo` and have -a `.doc` file extension. Prompt for deletion before each file. - -```bash -$ rm -i ∼/personal/memo*.doc -``` - -Remove all files from `$HOME/unixbook/finals/` that start with `ch`, is followed by 2 characters and has -a `.prn` file extension. Prompt for deletion before each file. - -```bash -$ rm -i ∼/unixbook/final/ch??.prn -``` - -Fore delete all files from `$HOME/unixbook/finals/` that ends with a `.o` file extension. - -```bash -$ rm -f ∼/unixbook/final/*.o -``` - -Force delete all files that start with `lab` followed by any # from 1-6 -and has a file extension of `.c` or `.C` from the directory -`$HOME/courses/ece446/`. - -```bash -$ rm -f ∼/courses/ece446/lab[1-6].[cC] -``` - -Recursively delete the directory `$HOME/NotNeededDirectory`. - -```bash -$ rm -r ∼/NotNeededDirectory -``` - -Recursively force delete the directory `$HOME/NotNeededDirectory`. - -```bash -$ rm -rf ∼/NotNeededDirectory -``` - -Recursively delete the directory `$HOME/NotNeededDirectory`, prompting -before each item to delete. - -```bash -$ rm -ri ∼/NotNeededDirectory -``` - -## Enter a C program that reads a temperature in Celsius entered from a keyboard and displays the corresponding temperature in Fahrenheit and complete the steps below. -(Hint: Look for such a program in the textbook or online.) - -Compile this program using the cc compiler, or any other compiler. Fix any errors, and recompile. - -Your program must have declaration statements, such as float c, f; -**Delete the semicolon from the end of the statement.** Recompile and report the kind of error. -The C compiler often provides cryptic error messages. Interpret this message. - -See `src/temperature.c` and `Makefile`. - -The compiler error is the following: - -```bash -$ make -cc -Wall -g -std=c99 -Isrc -c src/temperature.c -src/temperature.c: In function ‘main’: -src/temperature.c:5:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘printf’ - 5 | printf("Degrees in Celsius?\n"); - | ^~~~~~ -src/temperature.c:8:3: error: ‘f’ undeclared (first use in this function) - 8 | f = 9*c/5 + 32; - | ^ -src/temperature.c:8:3: note: each undeclared identifier is reported only once for each function it appears in -make: *** [Makefile:14: temperature.o] Error 1 -``` - -It states that a `;` colon is missing from before line 5 to terminate -the previous line. - -**Change the float c, f; statement to (float c; char f;)** - -Do you get any errors during compilation? - -No. I do get a compiler warning, but not an error. -```bash -$ make -cc -Wall -g -std=c99 -Isrc -c src/temperature.c -src/temperature.c: In function ‘main’: -src/temperature.c:9:35: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=] - 9 | printf("Degrees in Fahrenheit: %f\n", f); - | ~^ ~ - | | | - | | int - | double - | %d -cc -Wall -g -std=c99 -Isrc -o ./bin/temperature temperature.o -./bin/temperature -Degrees in Celsius? -100 -Degrees in Fahrenheit: 0.000000 -``` - -* What are they and why? - -The compiler warning says that the `%f` format specifier was expecting a -type of double, but received a type of `int`. The `char f` is being -interpreted as a memory address rather than a value. - -* Do you see any difference between running this program and the earlier version? Why? - -Yes, the output is now incorrect because the value for `f` is coming -from the memory address instead of the value. - -* If your program uses a `cout` statement, then replace it with a `printf` statement that does the same thing and vice versa. -* If you have used neither `cout` nor `printf` in the first version, then replace what you have with `printf`. - -## Explain how you can schedule the executable program to run at 1:00 a.m. and to take the input from a file, rather than the keyboard, without any changes to the source program. - -You can use the `at` program to run a script at a specific time. - -E.g. - -```bash -$ at 1am -f script.sh -``` - -## Explain how you can find the time it took the computer to execute your program. - -You can use the `time` command. - -E.g - -```bash -$ time curl -s -I https://www.mokhan.ca/ -HTTP/2 200 -content-type: text/html -content-length: 8017 -date: Tue, 21 May 2019 08:49:34 GMT -last-modified: Fri, 19 Apr 2019 18:06:57 GMT -etag: "902a8badcb38234a5d17b0f4ad54406e" -accept-ranges: bytes -server: AmazonS3 -vary: Accept-Encoding -age: 7455 -x-cache: Hit from cloudfront -via: 1.1 805232684895bb3db77c2db44011c8d0.cloudfront.net (CloudFront) -x-amz-cf-id: AxXM-x6oQ4RnM_YrQoR0nYtbrVF5mW7pREVHyu5tPD3SNswhLR2h0g== - - -real 0m0.136s -user 0m0.030s -sys 0m0.010s -``` - -The `time` program will emit 3 times at the end of the run. The `real` -time is the total time from invocation to completion. The `user` time is the time spent in user mode. -The `sys` time is the time spent in kernel mode. The difference between -`user` and `sys` shows the amount of time spent in the program version -the `kernel`. - -## Explain how the output of your program can be automatically emailed to your friend upon completion. - -You can pipe the output from the first program to the `mail` program. - -```bash -$ curl -s -I https://www.mokhan.ca/ | mail -s "My Website" destination@example.org +#!/bin/sh +echo "Use one of the following options:" +echo " d: To display today's date and present time" +echo " l: To see the listing of files in your present working directory" +echo " w: To see who's logged in" +echo " q: To quit this program" +echo "Enter your option and hit <Enter>: \c" +read option +case "$option" in + d) date + ;; + l) ls + ;; + w) who + ;; + q) exit 0 + ;; +esac +exit 0 ``` |
