24wi ver.

Note: this is for the Winter 2024 iteration of CSE 391. Looking for a different quarter? Please visit https://courses.cs.washington.edu/courses/cse391/.

  1. Suppose we have a file directories.txt where each line contains the name of a directory that we want to create. Our friend proposes that we run cat directories.txt | mkdir into order to do this? Will this command work? If not, how can we change this command to get it to work/could we improve it in any way?
Solutions

One correct way to solve this is the command:

xargs mkdir < dir.txt

Many people might write this as the following. It’s fine, but it is a bit unnecessary to invoke the cat command and using a | when we already have input redirection (<) available.

cat dir.txt | xargs mkdir
  1. Come up with a general statement about when we need to use the xargs command.
Solutions

We use xargs whenever we want to take something that would be coming as input to use them as command-line arguments. So for the last problem, the list of files is coming from a file that we can only redirect as input, but mkdir doesn’t read from stdin, it only takes command-line arguments.

On top of this, people often think of using xargs when you want to run one command over many possible input values.

  1. Suppose we want to run Mystery.java and save standard output to a file named out1.txt and have it print standard output to the console. How would we achieve this?
Solutions
java Mystery.java | tee out1.txt
  1. Again, suppose we are running Mystery.java. This time, however, we want standard output and standard error to print to the console, in addition to having ALL output printed to out1.txt. How would we achieve this?
Solutions
java Mystery.java 2>&1 | tee out1.txt
  1. Suppose we have the intro_survey.csv from the previous homework assignment. Using cut, how many unique answers were there to the question “what is your favorite candy”?
Solutions
tail -n+2 intro_survey.csv | cut -d, -f1 | sort -f | uniq -i | wc -l
  1. Write a command that would delete all directories, recursively, starting from the current directory. Be careful about where you run this command!! Don’t run it from your home directory!
Solutions
find -type d | xargs rm
  1. Suppose we know that Compile.java compiles with no errors. If we were to run javac Compile.java || java Compile what would be the result? Would the program compile? Would it run?
Solutions

When we use the || operator, if the first statement is true, then we know the entire line is true, thus we wouldn’t run the second statement. In other words, Compile.java will compile, but it won’t run.

  1. What would be some security issues with running some of the commands you learned from lecture (think of some of the practical implications of rm, find, and xargs in regards to malicious code injection)?
Solutions

An example of a security issue would be loss of data by running the command find -name *.java | xargs rm which deletes all java files. Secondly, if attackers can create files, then xargs is insecure unless the ‘-0’ flag is used (‘-0’ separates input items by null bytes and not necessarily ‘n‘). If a file with the name /home/someuser/foo/bar\n/etc/passwd exists (assume that ā€˜nā€™ stands for a newline character), then find ā€¦ -print can be persuaded to print these three lines (including the blank line):

/home/someuser/foo/bar

/etc/passwd
If it finds a blank line in the input, xargs will ignore it. Therefore, if some action is to be taken on the basis of this list of files, the /etc/passwd file would be included even if this was not the intent of the person running find.