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 speciﬁed • For questions 1-3, you must provide the commands used to answer each question, your method to ﬁnd 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 ﬁles, and necessary input ﬁles 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 ﬁles 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] 2. 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 ﬁrst six months in 2018 that also includes week numbers. [4 pts] 3. i) Create two plaintext ﬁles called ﬁle1.txt and ﬁle2.txt in your home directory with “hello world” as content, and using diﬀerent ways (i.e., create ﬁle1.txt and ﬁle2.txt using diﬀerent commands). ii) Retrieve the page http://stonybrook.edu and save it to a ﬁle called index.html in your home directory. [4 pts] 4. i) What is the absolute pathname of the ﬁles 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]
2 UNIX File System[18 pts] 1. i) Go to your home directory. ii) Create a subdirectory and a ﬁle that contains some content. iii) What are the ﬁle access permissions of the subdirectory and ﬁle you just created respectively? iv) Describe what operations can be done by whom on a directory with permissions rwxrw—x and ﬁle a ﬁle with permissions rwxr–r-x respectively. Be concise. [5 pts] 2. i) Go into the subdirectory that you created in the ﬁrst part. Stay in this directory to complete ii) to iv). ii) Copy the ﬁle you created in part 1 here. iii) Remove the ﬁle you created in part 1. iv) Create a subdirectory in the current directory. [3 pts] 3. 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 ﬁle 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]
4. 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 ﬁle index.html from your home directory into the clone you created in part 1. [3 pts] 5. 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]
3 UNIX Shell Utilities[30 pts] 1. Print only the 23rd line of a given ﬁle using head and tail commands. [3 pts] 2. 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. Brieﬂy explain why adding this new directory to the $PATH variable is useful. [5 pts] 3. Write a command to display only the ﬁles under a directory(But not directories and others). [5 pts] 4. Cron is a job scheduling utility provided by Linux/Unix systems, and it allows for periodical execution of predeﬁned commands. Cron jobs are deﬁned using crontab ﬁles(You can read more about crontab ﬁles here http://www.adminschoice.com/crontab-quick-reference). Write a crontab line that creates an empty ﬁle named update.txt under the home directory at 3:43PM of every 3rd, 10th, 17st and 24th days of every month. [5 pts] 5. i) Create an A3tmp directory in your home directory. ii) Read the man page for the ﬁnd command. Find out all ﬁles (only ﬁles) within or below the /var/log directory that begin with letter “s”, while directing all stderr messages to a ﬁle called logerror.txt in the A3tmp directory in your home directory. [4 pts] 6. i)Howmanyentriesthatstartswithletter”a”andhas.gzextensionarethereinthe/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] 7. 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 ﬁle? iii) Save the words between 300-500 and 700-800 (both inclusive) in this ﬁle to ﬁles named 300-500.txt and 700-800.txt in your ˜/A3tmp directory, respectively. [4 pts]
4 Simple Scripting[40 pts] Please submit your solutions as shell scripts for this question. For example, create a ﬁle named q4_p1.sh for Part 1. There is no speciﬁc 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 speciﬁes a directory dir. The script ﬁrst 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. 2. A line starting “Current directory is : ”. Then, on the same line, the absolute pathname of the current working directory. 3. A line starting “Home directory is : ”. Then, on the same line, the absolute pathname of the home directory. 4. An empty line 5. The line “— 5 most recently modiﬁed non-empty subdirectories—” 6. the 5 most recently modiﬁed non-empty subdirectories in the current directory in long listing format (most recent ﬁrst) 7. An empty line 8. The line “— Files in last 45 minutes” 9. The ﬁles in the current directory that are modiﬁed less than 45 minutes ago with a size of at least 1000 bytes
10. An empty line, followed by a line of 70 equal signs
Run your script with a sample directory(and ﬁles) you created. Append the output of the script execution to a ﬁle 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 ﬁles using a while loop in Bash. Submit the shell script your created. Even numbered ﬁles should be named even2, even4, even6, even8 and even10 and should have rwxrw-r– permissions while the odd numbered ﬁles 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 ﬁxed number.