CS 2505 Computer Organization I Test 1

Similar documents
CS 2505 Computer Organization I Test 1

Do not start the test until instructed to do so!

A shell can be used in one of two ways:

Table Of Contents. 1. Zoo Information a. Logging in b. Transferring files 2. Unix Basics 3. Homework Commands

Introduction to Linux

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

The Unix Shell & Shell Scripts

Traditional Access Permissions

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

Environment Variables

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

CS 2506 Computer Organization II Test 2

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Introduction: What is Unix?

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so!

Do not start the test until instructed to do so!

Vi & Shell Scripting

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

CS 2505 Computer Organization I

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

9.2 Linux Essentials Exam Objectives

CS246 Spring14 Programming Paradigm Notes on Linux

Environment Variables

Linux & Shell Programming 2014

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Lab 2: Linux/Unix shell

COMP2100/2500 Lecture 17: Shell Programming II

Linux Training. for New Users of Cluster. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Useful Unix Commands Cheat Sheet

CS 2506 Computer Organization II Test 1

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

Exploring UNIX: Session 3

Review of Fundamentals

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

5/20/2007. Touring Essential Programs

LING 408/508: Computational Techniques for Linguists. Lecture 5

Introduction to Linux

PROGRAMMAZIONE I A.A. 2015/2016

Files

UNIX System Programming Lecture 3: BASH Programming

Week 2 Lecture 3. Unix

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

CS CS Tutorial 2 2 Winter 2018

Connecting to ICS Server, Shell, Vim CS238P Operating Systems fall 18

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Links, basic file manipulation, environmental variables, executing programs out of $PATH

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Configuring Your Account (40 points)

Lecture 02 The Shell and Shell Scripting

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

Bash Shell Programming Helps

CS 2506 Computer Organization II Test 1. Do not start the test until instructed to do so! printed

Assignment clarifications

CS Unix Tools. Lecture 2 Fall Hussam Abu-Libdeh based on slides by David Slater. September 10, 2010

Shells and Shell Programming

Command-line interpreters

This is Lab Worksheet 3 - not an Assignment

GNU/Linux 101. Casey McLaughlin. Research Computing Center Spring Workshop Series 2018

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

A Brief Introduction to the Linux Shell for Data Science

My Favorite bash Tips and Tricks

Week 5 Lesson 5 02/28/18

THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering

UNIX shell scripting

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Linux Essentials. Programming and Data Structures Lab M Tech CS First Year, First Semester

CMSC 201 Spring 2017 Lab 01 Hello World

Introduction to the shell Part II

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Working with Basic Linux. Daniel Balagué

Introduction to UNIX Command Line

CS Unix Tools & Scripting

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Mauro Ceccanti e Alberto Paoluzzi

Traditional Access Permissions

EECS2301. Lab 1 Winter 2016

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CS 3114 Data Structures and Algorithms READ THIS NOW!

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Introduction in Unix. Linus Torvalds Ken Thompson & Dennis Ritchie

Introduction to Shell Scripting

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Shells & Shell Programming (Part B)

Solution READ THIS NOW! CS 3114 Data Structures and Algorithms

C Shell Tutorial. Section 1

Shell Start-up and Configuration Files

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Esercitazione Introduzione al linguaggio di shell

CS 3410 Intro to Unix, shell commands, etc... (slides from Hussam Abu-Libdeh and David Slater)

Bioinformatics? Reads, assembly, annotation, comparative genomics and a bit of phylogeny.

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Operating Systems, Unix Files and Commands SEEM

Transcription:

Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other computing devices may be used. The use of any such device will be interpreted as an indication that you are nished with the test and your test form will be collected immediately. Answer each question in the space provided. If you need to continue an answer onto the back of a page, clearly indicate that and label the continuation with the question number. If you want partial credit, justify your answers, even when justication is not explicitly required. There are 6 questions, some with multiple parts, priced as marked. The maximum score is 100. When you have completed the test, sign the pledge at the bottom of this page and turn in the test. If you brought a fact sheet to the test, write your name on it and turn it in with the test. Note that either failing to return this test, or discussing its content with a student who has not taken it is a violation of the Honor Code. Do not start the test until instructed to do so! Name Solution printed Pledge: On my honor, I have neither given nor received unauthorized aid on this examination. signed B 1

CS 2505 Computer Organization I Test 1 xkcd.com B 2

1. Answer the following questions as if you were executing the commands on the rlogin cluster. a) [8 points] What command will add the current directory to your path? What command should you use to make sure that your path is correctly updated, i.e., what command will display the updated path? This line will add the current directory to a user's path: PATH=$PATH:. You should put the command in.bash_prole b) [6 points] Create an alias, rmr, that will recursively remove a directory. The alias should contain any required information and needed switches. After creating the alias, to remove a directory you should be able to simply type rmr dir_to_rm, without additional parameters or switches. Something like this will work: alias rmr="rm -r" Note: if you didn't remember the -r switch for cp, you could also write a script function to do this, and alias it to cpr. The function given in question 5 is somewhat similar to this. 2. [6 points] Create an alias, scpbackup, that will let you secure copy (scp) the local le ~/backup.tar from another Linux installation to your home directory on rlogin. The alias should contain any login info and other needed switches. After creating the alias you should be able to simply type scpbackup, without additional parameters or switches, and then you should be prompted for a password (assuming haven t set up password-less login). Something like this will work: alias sshbackup="scp ~/.backup.tar hmonti@rlogin.cs.vt.edu:~/" alias sshbackup="scp ~/.backup.tar hmonti@rlogin.cs.vt.edu:" B 3

3. Like before, answer the following questions as if you were executing the commands on the rlogin cluster. a) [6 points] If input.txt contains the full text of Moby Dick (where the word Captain frequently occurs), what would be the result of the executing the following commands? [johokie@rlogin ~] cat input.txt grep "Captain" > output.txt 'cat input.txt' will output the le to stdout, the pipe (' ') will capture that output (and not display it), and then send it to the grep "Captain" command. The grep command will match all and only the lines that contain "Captain", and nally those lines will be written to output.txt using redirection ('>'). b) [6 points] Given the following terminal session. What would be the result of executing the following command? [johokie@rlogin ~] le test.jpg test.jpg: JPEG image data, JFIF standard 1.01 [johokie@rlogin ~] mv test.jpg test.pdf [johokie@rlogin ~] le test.pdf Despite renaming the le to test.pdf, the le command actually examines the le s contents and is able to determine that test.pdf is a JPEG document. So it will print out: test.jpg: JPEG image data, JFIF standard 1.01 c) [10 points] Given the following terminal session. What would be the result of executing the following commands? [johokie@rlogin ~] size=`stat -c "%s" test.tar` [johokie@rlogin ~] dd if=/dev/zero of=test.tar bs=1 count=$size The rst line runs the command stat -c "%s" test.tar (note the back ticks), and stores the output of the command in the shell variable size. This invocation of stat command will tell you how big a le is in bytes, so size is the number of bytes in the le. The next line uses dd to overwrite the le with zeros. Using the parameters bs=1 (so 1 byte) and count=$size (number of bytes in the le) we overwrite the entire le byte by byte. Further, the command uses $size to overwrite the exact number of bytes in the le. As a result these lines end up acting like the shred command, or safely "deleting" a le s contents. B 4

4. Like before, answer the following questions as if you were executing the commands on the rlogin cluster. Examine the following terminal session; you may assume these commands are run from the user s home directory. [johokie@rlogin ~] ls /bin/ bbcp cp ls sh [johokie@rlogin ~] ls /usr/bin/ bbcp curl cvs diff gnuplot [johokie@rlogin ~] ls ~/bin bbcp gnuplot python [johokie@rlogin ~] ls bbcp a) [7 points] Assume the $PATH variable contains the value show below. If the user were to run the command gnuplot which executable will run? Specify the absolute path to the executable (e.g., /foo/bar/gnuplot). [johokie@rlogin] echo $PATH /bin:/home/johokie/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:. This is the command that will run: /home/johokie/bin/gnuplot b) [7 points] Assume the $PATH variable contains the value show below. If the user were to run the command bbcp which executable will run? Specify the absolute path to the executable. [johokie@rlogin] echo $PATH /usr/bin:/home/johokie/bin:/usr/kerberos/bin:/usr/local/bin:/bin:. This is the command that will run: /usr/bin/bbcp c) [7 points] Given your answers above, how does bash choose which executable will run when a command is entered? bash searches the colon-separated list of directories from left to right in order, and chooses the rst executable it encounters. So order is important. This is why slides recommend that you add the current directory at the end of the path, so system executables are found rst. d) [5 points] Suppose you manage to severely mess up your.bashrc and/or.bash_prole so that your PATH variable is broken and you can't run standard commands on rlogin. How can you recover from this situation? There are a few way you could x this: Manually recreate you path using PATH=$PATH syntax, then use vi/nano/gedit to x the conguration les. If you've got a working.bashrc/.bash_prole on your own Linux install, you can transfer them to rlogin by using scp. B 5

Use absolute paths eg /bin/vi, to execute a text editor and x the les. Use absolute paths e.g. /bin/cp to copy the default system /etc/prole or /etc/bashrc B 6

5. Consider the bash script, showem.sh, given below. The script is syntactically correct and executes as its author intended. #! /bin/bash ################################################### fn to process directory process() { for fname in $1/*; do if [[ -f $fname ]] && [[ -x $fname ]]; then echo "$fname" elif [[ -d $fname ]] && [[ -r $fname ]]; then process $fname done } ##################################################### check arguments if [[ $# -ne 1 ]]; then echo "Please specify a directory" exit 1 if [[! -d $1 ]]; then echo "$1 is not a directory" exit 2 if [[! -r $1 ]]; then echo "I don't have read permission for $1" exit 3 #################################################### process directory process $1 exit 0 Suppose the current directory contains a subdirectory Cprogs, and that the following les occur in Cprogs and its subdirectories. The current user, johokie, has read access to all the directories except for Cprogs/RemEven. Cprogs: -rwxrwxr--. 1 johokie users1 379 Feb 20 20:18 runme Cprogs/Compare: total 8 -rwxrwxr--. 1 johokie users1 4869 Nov 20 22:11 compare Cprogs/ESpaced: total 20 -rwxrwxr-x. 1 root root 7431 Jan 30 23:04 driver -rw-rw-r--. 1 johokie users1 384 Jan 30 23:03 driver.c -rw-rw-r--. 1 johokie users1 584 Jan 30 23:04 EvenlySpaced.c -rw-rw-r--. 1 johokie users1 95 Jan 30 22:48 EvenlySpaced.h -rwxrwxr-x. 1 johokie users1 7431 Jan 30 23:04 jdriver Cprogs/RemEven: total 24 -rwxrwxr-x. 1 johokie users1 7076 Feb 2 21:14 driver -rw-rw-r--. 1 vthoky users1 412 Feb 2 21:14 driver.c -rw-rw-r--. 1 vthoky users1 1808 Feb 2 20:53 driver.o -rw-rw-r--. 1 johokie users1 612 Feb 2 20:54 RemoveEvenDigits.c -rw-rw-r--. 1 johokie users1 125 Feb 2 20:42 RemoveEvenDigits.h B 7

a) [4 points] What will be written if user johokie invokes the script as:./showem.sh? "Please specify a directory" The check for the number of parameters being 1 will trigger and this output will be written, after which the script will exit. b) [6 points] What will be written if user johokie invokes the script as:./showem.sh Cprogs/ESpaced? Cprogs/ESpaced/driver Cprogs/ESpaced/jdriver The script will process the list of les in Cprogs/ESpaced, and print the names of those which are executable ("-x"). c) [8 points] Suppose johokie invokes the script as:./showem.sh Cprogs. Then the output from the script will include the following (and possibly more): Cprogs/runme Cprogs/Compare/compare So, the script must actually process the subdirectories of Cprog. Why does that happen? That is, what is it about the script that makes it process subdirectories? Be very specic. Within the body of the function process(), we have the following if/elif statement:... if [[ -f $fname ]] && [[ -O $fname ]]; then echo "$fname" elif [[ -d $fname ]] && [[ -r $fname ]]; then process $fname... If fname names a readable directory, process() calls itself, so it processes subdirectories recursively. B 8

6. [14 points] Write a bash script, demote.sh, that takes a child directory as a parameter, moves as many regular les from the parent directory into the given child directory as is possible, then renames the parent directory to done if doesn t contain any les except the given child directory. For simplicity, you may overwrite les in the child directory that have the same name as a le in the subdirectory, and you may assume the parent directory contains only regular les, aside from the child directory itself. For example, the command./demote.sh /home/data/cellar would move as many les as possible from /home/data into /home/data/cellar and rename /home/data to /home/done, if /home/done was left holding no other les except /home/data/cellar. The script should verify the parameter is a writeable directory, that it has a parent directory, and that the parent directory is readable. If not, the script should exit with an informative message and exit code. Be careful of your logic, and be as exact with syntax as possible. Write comments to explain your intent; that will make it easier for us to evaluate your answer if you have syntax errors. The following page has been left blank so you can continue your answer to this question. Do not cram an illegible solution into the space below. Start here and continue onto page 9. One solution is shown on the following page. The essential steps the script must perform are: verify the script was invoked with one parameter decompose that parameter to get the name of the parent and subdirectories verify the parent directory exists and is readable verify the subdirectory exists and is writeable for each le in the parent directory o if it's a regular le, move it into the subdirectory (watch for les that can't be moved) o if it's not a regular le, and not the subdirectory, note that if there were no unmovable les and no other nonregular les, rename the parent directory B 9

#! /bin/bash # Invoke as: demote.sh DIR/SUBDIR ############################################################# verify dir # $1 is parent directory # $2 is child directory chk_directories() { if [[! -d "$1" ]] [[! -r "$1" ]]; then echo "$1 is not a readable directory." exit 2 if [[! -d "$2" ]] [[! -w "$2" ]]; then echo "$2 is not a writeable directory." exit 2 } # Verify script was invoked with 1 parameter, that is the of a readable # directory. Exit with an appropriate message if that is not true. if [[ $# -ne 1 ]]; then echo "Please specify a directory." exit 1 SUBDIR=$1 PARDIR=${SUBDIR%/*} echo $SUBDIR echo $PARDIR chk_directories $PARDIR $SUBDIR OK=0 # Copy all the les from PARDIR into SUBDIR: for element in $PARDIR/*; do if [[ -f $element ]]; then mv -f "$element" "$SUBDIR" if [[! $? -eq 0 ]]; then echo "Could not move $element from $PARDIR to $SUBDIR." OK=$((OK + 1)) elif [[ -d $element ]] && [[! $element == $SUBDIR ]]; then OK=$((OK + 1)) done # # When done, if PARDIR contains only SUBDIR, rename PARDIR. if [[ $OK -eq 0 ]]; then mv -f $PARDIR "done" exit 0 B 10

B 11