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

版权声明:此文为博主原创,如需转载请注明出处!!! https://blog.csdn.net/ilike_program/article/details/80655387
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

猜你喜欢

转载自blog.csdn.net/ilike_program/article/details/80655387
cse
今日推荐