Homework Assignment 3 Solution

$30.00

Description

Instructions

Please read the the following instructions carefully before coding. You may lose points if you fail to follow these instructions.

Deadline for this assignment is November 25, 11:59PM. No late submissions accepted.

Please use Bash unless otherwise specified

For questions 1-3, you must provide the commands used to answer each question, your method to find the answers using the command output, and selected output and/or commands to support/verify your answers(as screenshots or in plain text), whenever possible. You can submit these as a report in PDF format.

Make sure your programs work in other machines. One way to test this is to test your program in our UNIX servers, and see if it works there. You will lose marks if your program fails to run in our machines.

Put all of your answer files, and necessary input files in a single folder. (Do not make a sub-folder for each question).

Note: Please rename your folder in the right way before zipping! For example, if your name is Ying Lu and your student ID is 111345678, put all submission files in a folder named Ying_Lu_1112345678, then compress that folder to Ying_Lu_1112345678.zip.

1 Simple UNIX Commands[12 pts]

  1. (i) What is your default login shell? (ii) What’s the command to invoke bash? (iii) What’s the command to terminate the bash you invoked in (ii)? [2 pts]

  1. Read the man page of cal. (i) Find the date of Easter (Orthodox church) in 2027. ii) Display a calendar from July 2016 to June 2018, inclusive. iii) Display a calendar of the first six months in 2018 that also includes week numbers. [4 pts]

  1. i) Create two plaintext files called file1.txt and file2.txt in your home directory with “hello world” as content, and using different ways (i.e., create file1.txt and file2.txt using different commands). ii) Retrieve the page http://stonybrook.edu and save it to a file called index.html in your home directory. [4 pts]

  1. i) What is the absolute pathname of the files that you created in part 3? ii) What is its relative pathname if the current directory is the parent directory of the home directory? [2 pts]

  • UNIX File System[18 pts]

    1. i) Go to your home directory. ii) Create a subdirectory and a file that contains some content. iii) What are the file access permissions of the subdirectory and file you just created respectively? iv) Describe what operations can be done by whom on a directory with permissions rwxrw—x and file a file with permissions rwxr–r-x respectively. Be concise. [5 pts]

    1. i) Go into the subdirectory that you created in the first part. Stay in this directory to complete ii) to iv). ii) Copy the file you created in part 1 here. iii) Remove the file you created in part 1. iv) Create a subdirectory in the current directory. [3 pts]

    1. i) Go to your home directory. ii) Change the permission of the subdirectory that you created in part 1 so that when in your home directory, you can’t list the content of that subdirectory but can display and change the content of the file in that subdirectory. iii) While in your home directory, are you able to enter the subdirectory that you created in part 2.iv? [4 pts]

1

  1. i) Create a clone of the subdirectory that you created in part 1, including contents of all its subdirectories, sub-subdirectories, if there are any. ii) Move the file index.html from your home directory into the clone you created in part 1. [3 pts]

  1. i) Remove all contents within the subdirectory that you created in part 1, including all sub or sub-sub contents within. ii) Verify that it is now empty. ii) Remove this empty directory itself. [3 pts]

  • UNIX Shell Utilities[30 pts]

    1. Print only the 23rd line of a given file using head and tail commands. [3 pts]

    1. Create a directory under your home directory, and add this directory to your $PATH variable. Verify it by printing the value of $PATH variable, and running an executable placed under this directory from your home directory. Briefly explain why adding this new directory to the $PATH variable is useful. [5 pts]

    1. Write a command to display only the files under a directory(But not directories and others). [5 pts]

    1. Cron is a job scheduling utility provided by Linux/Unix systems, and it allows for periodical execution of predefined commands. Cron jobs are defined using crontab files(You can read more about crontab files here http://www.adminschoice.com/crontab-quick-reference). Write a crontab line that creates an empty file named update.txt under the home directory at 3:43PM of every 3rd, 10th, 17st and 24th days of every month. [5 pts]

    1. i) Create an A3tmp directory in your home directory. ii) Read the man page for the find command. Find out all files (only files) within or below the /var/log directory that begin with letter “s”, while directing all stderr messages to a file called logerror.txt in the A3tmp directory in your home directory. [4 pts]

    1. i) How many entries that starts with letter “a” and has .gz extension are there in the /usr/share/man/man1 directory? ii) Within them, how many are symbolic links? Note that in the output of ls -l, a symbolic link has letter l at the start of its line, it also has a “ -> ” sign in its line. [4 pts]

    1. i) How many words, lines and characters are there in /usr/share/dict/words respectively? ii) Based on the result in i), how many words are there on each line of the file? iii) Save the words between 300-500 and

˜

700-800 (both inclusive) in this file to files named 300-500.txt and 700-800.txt in your /A3tmp directory, respectively. [4 pts]

  • Simple Scripting[40 pts]

Please submit your solutions as shell scripts for this question. For example, create a file named q4_p1.sh for Part 1. There is no specific input/output format other than the ones stated in the questions.

4.1 Part 1 [12 pts]

Write a script using simple shell commands. The script takes a command line argument that specifies a directory dir. The script first changes directory to dir, then prints the following in sequence:

  1. A line starting “Current shell is: ”. Then on the same line, path to current shell executable.

  1. A line starting “Current directory is : ”. Then, on the same line, the absolute pathname of the current working directory.

  1. A line starting “Home directory is : ”. Then, on the same line, the absolute pathname of the home directory.

  1. An empty line

  1. The line “— 5 most recently modified non-empty subdirectories—”

  1. the 5 most recently modified non-empty subdirectories in the current directory in long listing format (most recent first)

  1. An empty line

  1. The line “— Files in last 45 minutes”

  1. The files in the current directory that are modified less than 45 minutes ago with a size of at least 1000 bytes

2

10. An empty line, followed by a line of 70 equal signs

Run your script with a sample directory(and files) you created. Append the output of the script execution to a file called q4_p1.txt

Note that you may assume that there are at least 5 directories in an input directory.

4.2 Part 2 [7 pts]

Create 10 empty files using a while loop in Bash. Submit the shell script your created. Even numbered files should be named even2, even4, even6, even8 and even10 and should have rwxrw-r– permissions while the odd numbered files should be named odd1, odd3, odd5, odd7 and odd9 and should have r-xr-xr– permissions.

4.3 Part 3 [6 pts]

Write a shell script that prompts the user to enter a sentence, and prints the number of occurrences of letter “k” in that sentence.

4.4 Part 4 [6 pts]

Write a shell script that counts the number of directories under a directory provided as a command line parameter, without using the ls command, then prints the number to the screen.

4.5 Part 5 [9 pts]

Write a shell script that takes a list of numbers as command-line parameters(e.g., q4_p5.sh 4 2 2 1 6), and calculates min, max and median of these numbers. Note: Number of command-line parameters is not a fixed number.

3


error: Content is protected !!