1. 程式人生 > >CSE 390A, Spring 2014 Assignment 2: More Unix Shell

CSE 390A, Spring 2014 Assignment 2: More Unix Shell

This assignment continues to practice using the bash shell and basics of combining commands using redirection and pipes.  Electronically turn in TWO files: 1) a file homework2.txt that contains your answers to the questions from tasks 1 and 3 below, 2) a file Backwards.java as your answer to task 2. PLEASE NAME THESE FILES EXACTLY AS LISTED! Some parts of this assignment depend on compiling and running Java programs from the command line.  Many distributions of Linux do not include Sun's Java Development Kit (JDK).  You may need to install JDK or use a different Linux machine that already has JDK installed, such as the CSE basement lab machines or the shared attu server.  See the course web site for directions about how to install JDK on your own Linux machine. 
 
Task 0: Log in / Prepare your home directory First, log in to a machine running Linux and launch a Terminal window as described previously in Homework 1. We have set up a ZIP archive full of support files that you must download to your Linux machine.  Download/unzip it to a directory on your system.  We suggest creating a hw2 directory for your files for this assignment. wget http://courses.cs.washington.edu/courses/cse390a/14sp/homework/2/hw2.zip unzip hw2.zip 
 
Task 1:
Learn more about the system The following are questions about your Linux system for you to investigate and discover the answers.  You should create a text file named homework2.txt and save it somewhere in your home directory, such as in your 390 folder.  In this file, write your answers to the questions below, one per line.  You don't need to explain how you found your answers. Each Linux system is different; you will receive credit if your answer is plausible for a typical Linux system in general. 

1. What is the full path of the diff program on this machine? 

Answer: /usr/bin/diff

2. What is the total number of files and directories stored directly within the /bin folder on this machine's file system?  (Not including the contents of any subdirectories within /bin .)  (Hint: There are a lot of files; it would take too long to count them all by hand.  Use the wc command to count words or lines in a given input or file.)

Answer: 160 

3. (Self-Discovery)   The file /etc/passwd stores a list of all users' names and user account names on the system, along with a bit of other information such as what shell program they use.  (The default shell for most users, and the one we have been learning about in this course is the Bash shell, stored in the file /bin/bash .) 
 How many users exist on this Linux system that use the Bash shell by default?  (Hint: To figure this out, you will need to search for lines in the passwd file that mention bash.) 

 You may assume that no line of /etc/passwd contains the phrase "/bash" other than to specify the Bash shell.) 

Answer: 2

4. (Self-Discovery)   In class we talked about links, which allow one file to refer to another file.  There are two kinds of links: "hard" and "soft" links.  See the lecture slides to recall the difference between these two types of links. 

 What happens when file a represents a "hard link" to another file b, but then b is deleted?  (Does a also disappear from the file system?  Does a remain but become a "broken link" that fails to function?  Etc.)  What about if a is a "soft link" to b?  Test this for yourself by creating a link from one file to another, and deleting the linked-to file and investigating what happens.  See if a is still there and/or if it can still be used after deleting b. 

Answer: 給animals.txt建立硬連結:ln animals.txt animalshard.txt
這時目錄下會多出一個檔案:animalshard.txt。其實animals和animalshard是同一個檔案的兩個名字,它們擁有相同的索引節點號和檔案屬性。
此時刪除animalshard.txt: rm animalshard.txt,目錄下的animalshard檔案被刪掉了,同時,檔案animals.txt的連結數從2減為1。若把animals.txt檔案也刪除,此時animals.txt的連結數從1減為0,核心會把此檔案從磁碟上刪除。
給animals2.txt建立軟連結:ln –s animals2.txt animals2soft.txt
這時目錄下會多出animals2soft.txt檔案,此檔案的索引節點號和檔案屬性與animals2.txt都不相同,animals2.txt的連結數仍然為1,沒有變化。此時刪除animals2soft.txt檔案,animals2soft檔案會消失,animals2.txt檔案的索引結點號減1,連結數仍為1。
從實驗可以看出,硬連結原檔案/連結檔案公用一個inode號,說明他們是同一個檔案,而軟連結原檔案/連結檔案擁有不同的inode號,表明他們是兩個不同的檔案;在檔案屬性上軟連結明確寫出了是連結檔案,而硬連結沒有寫出來,因為在本質上硬連結檔案和原檔案是完全平等關係;軟連結的連結數目不會增加,而硬連結是會增加的。

5. What is a reason it might be useful to have a link from one file to another?  Why not just make a copy of the file? 

Answer: 主要優點並不是它可以節省磁碟空間(儘管它也是如此),但是,相反,檔案許可權的更改應用於所有連結接入點。該連結將顯示lrwxrwxrwx的許可權,但這是針對連結本身而不是訪問到連結指向的檔案。 因此,如果你想改變一個的許可權命令,比如su,你只需要在原文上做。 有副本你必須找到所有副本並更改每個副本的許可權。

Task 2: Java program w/ command-line arguments Write and turn in a Java class called Backwards in a file named Backwards.java.  Your program should print all of its command-line arguments with their characters reversed.  For example, if the user runs the program from a terminal using: java Backwards hello how-are you? "I'm fine" The program should produce the output below.  Submit Backwards.java along with your homework2.txt file. olleh era-woh ?uoy enif m'I 

public class Backwards{

	public void swap(char[]arr,int begin,int end)
	{
		while(begin<end){
			char temp=arr[begin];
			arr[begin]=arr[end];
			arr[end]=temp;
			begin++;
			end--;
		}

	}
	public String swapWords(String str)
	{
		char[]arr=str.toCharArray();
		swap(arr,0,arr.length-1);
		return new String(arr);
	}

	public static void main(String[]args){
		for(int i=0;i<args.length;i++)
		{
			System.out.println(new Backwards().swapWords(args[i]));
		}
	}
}

Task 3: Bash shell commands For each item below, determine a single bash shell statement that will perform the operation(s) requested.  Each solution must be a one-line shell statement, but you may use input/output redirection operators such as >, <, and |.  Write these commands into your homework2.txt file, one per line.  In your file, write the command that will perform the task described for each numbered item; don't write the actual output that such a command would produce. To test your commands, you should have unzipped hw2.zip.  Use man pages or the Linux Pocket Guide to help you. 

1. Compile the Java program stored in the file Crunch.java. 

Answer: javac Crunch.java

2. The file animals2.txt contains an alphabetized list of animal names.  It includes many duplicates.  Output the first 16 distinct animals from the file, one per line.  (The last one should be adlie penguin .)

Answer: uniq animals2.txt | head -16 

3. Run the Java Crunch program stored in Crunch.class , suppressing (hiding) its console output.  See the lecture slides for how to hide the console output of a program.  (The program produces both console and file output.  If your command is correct, there will be no console output, but the output file crunch.txt will still be created.)

Answer: java Crunch > /dev/null 

4. Run the Java Pow program stored in the file Pow.class, redirecting its input to come from the file numbers.txt instead of from the console.  (Pow accepts integers from standard input and computes exponents.  If you ran it normally, it would sit there waiting for input.  You'd have to press Ctrl-D to end the input.) 

Answer: java Pow < ./numbers.txt

5. Combine the contents of files verse1.txt, verse2.txt, and verse3.txt into a new file lyrics.txt. 

Answer: cat verse1.txt verse2.txt verse3.txt > lyrics.txt

6. Output the names of all files/folders in the current directory whose names do NOT contain the phrase "txt", one file name per line.  (Hint: Use the same command that you'd use to find lines that do contain a pattern.)

Answer: ls -1|grep -v "txt" 

7. (Self-Discovery)  The du command shows the disk space used by all files in a given directory and its contents. 

 Using du, output the total number of bytes (not kilobytes, megabytes, etc.) used by the /etc/X11 folder and its subdirectories.  Output a single line containing the total bytes (it's okay if the line also shows the folder name). (Hint: you do NOT need to cd into the directory to run this command.) (/etc/X11 contains configuration files for the X window system, Unix/Linux's graphical user interface.) 

Answer: du -sb /etc/X11

8. (Self-Discovery)  The curl command fetches the contents of a document at a given URL.  While wget downloads and saves the file to your local disk, curl instead outputs it to the terminal. 

 Using curl, output the number of words in the text file at the following URL: 
 http://courses.cs.washington.edu/courses/cse390a/14sp/homework/2/hamlet.txt Count these words without using any graphical program (such as a web browser) to download the file to your computer.  The word count should be the only output; don't show the number of characters/lines, file name, etc. 

 Note: You will have to find the appropriate command-line argument(s) to suppress some of curl's normal output and run it in "silent mode".  Recall that you can search man pages for a phrase using the / key. 

Answer: curl -s http://courses.cs.washington.edu/courses/cse390a/14sp/homework/2/hamlet.txt | wc -w

9. Display all lines from animals.txt that contain the text "growl" ignoring case, in reverse-ABC-sorted order and with no duplicates.  Output the lines themselves only. 

Answer: cat animals.txt | grep -i "growl" | sort -r | uniq

10. Run the Java program stored in Fresh.class.  Do not display any of the program's output on the console; instead, capture the last 3 lines of output produced by the program into a file named willsmith.txt . 

Answer: java Fresh | tail -3 > willsmith.txt