build details

Show: section status errors & todos local changes recent changes last change in-page changes feedback controls

Part 1: Introduction to Linux

Modified 2022-09-19 by Stefanie Tellex

The learning objectives of this assignment are to familiarize you with basic Linux shell commands, standard input, standard output, standard error, and pipes. You will use these ideas when interacting with the Linux shell to operate your drone. Additionally you will use these ideas in the next section when working on the networking exercises.

Background Information

Modified 2019-09-03 by matthewberg

When you enter a command in a shell, it executes a program. These programs read from a stream, known as “standard input” and write to two output streams, “standard output” and “standard error”. When you print in python, it writes its output to standard output. In another language, such as C, you use other functions, such as printf to write to standard output.

In addition to writing to standard output, a program can read from standard input. The program cat, short for concatenate, reads from standard input and writes the result to standard output.

Standard Output (10 points)

Modified 2019-09-03 by matthewberg

  1. Write a python program that prints “Hello world” to standard output. Save the program as hello1.py and submit it.

  2. Write a python program that prints “Hello world” to standard output using sys.stdout. Save the program as hello2.py and submit it.

  3. Write a bash script that prints “Hello World” to standard output. Save the script as hello.sh and submit it.

Standard Input (10 points)

Modified 2022-09-24 by Stefanie Tellex

Write answers to questions 1-2 inshell.txt. Submit this file.

  1. Run cat with no arguments. Why does cat seem like it is hanging?

  2. When you run cat, type a message into your terminal, and press Control-D. Describe what cat does. Make sure to include which streams are being used, and for what purpose.

  3. Write a python program my_cat.py that reads a message from standard input and prints to standard output, just as cat does. You only need to reproduce the behavior of cat when run with no arguments. Submit this file.

Pipes (20 points)

Modified 2022-09-24 by Stefanie Tellex

Pipes are used to redirect standard input, standard output, and standard error. First, > is used to redirect standard output to a file. For example, echo "Hello World" > test.txt will write the string Hello World to test.txt. Write answers to questions 1-4 in shell.txt. Submit this file.

  1. Create files one.txt, two.txt and three.txt that contain the strings 1, 2, and 3, respectively using echo and output redirect.

  2. By convention, almost all shell programs read input from standard input, and write their output to standard output. Any error messages are printed to standard error. You can chain shell programs together by using |. For example, the program ls writes the contents of a directory to standard output. The program sort reads from standard input, sorts what it reads, and writes the sorted content to standard output. So you can use ls | sort to print out a sorted directory list. Read the man page for sort (man sort) to learn how to sort in reverse order. What is the bash script (using |) that prints the contents of a directory in reverse alphabetical order?

  3. Use cat, | and echo to print hello world. Do not write to any files and use both commands one time.

  4. This is not the simplest way to print hello world. Can you suggest a simpler way? (We asked you to do it the more complicated way to practice with pipes.)

  5. Write a python script that reads from standard input, sorts lines in reverse alphabetical order, and prints the result. It should behave like sort -r. It does not need to process any command line arguments. Submit your script in a file called my_reverse_sort.py. Do not submit this script in shell.txt

Standard Error (10 points)

Modified 2020-10-08 by sageshoyu

In addition to standard input and standard output, there is a third stream, standard error. If there is an error in a chain of pipes, it will be printed to the terminal rather than buried in the input to the next program.

  1. Recall that ls -a | sort > sorted.txt puts all the names of files in a directory sorted in alphabetical order into the file sorted.txt. If you modify the command to be ls -a -hippo | sort > sorted.txt, what text is in sorted.txt, what is outputted as standard error, and why? Answer this question in shell.txt. Submit this file.
  2. Create a python script that prints reversed sorted output to standard error. Use it to sort ls -a instead of sort. Submit the file containing the script as my_sort_status.py.