Difference between revisions of "CSC231 Bash Tutorial 4"
Line 2: | Line 2: | ||
---- | ---- | ||
− | <showafterdate after=" | + | <showafterdate after="20170104 11:00" before="20171231 00:00"> |
=Using the Vi Editor= | =Using the Vi Editor= |
Revision as of 11:41, 4 October 2017
--D. Thiebaut (talk) 14:13, 22 February 2017 (EST)
<showafterdate after="20170104 11:00" before="20171231 00:00">
Using the Vi Editor
Vi is the default editor with most distributions of Linux. If you get a Raspberry Pi, for example, and install Linux on it, chances are that the only editor you will have access to is vi, or vim (Vi IMproved), which is a friendlier version of vi.
This means that every so often, when you deal with Linux, you are faced with having to do some editing and emacs is not around, and you may not have privileges to install it, so knowing the basic commands of vi can be a life saver.
- Go through the first 3 sections of this interactive tutorial on Vim:
- Introduction
- Two Modes
- Basic Movement
- Once you are done, get a copy of a short text file, edit it (enter your first and last name, as well as your account number in the right place), and submit it. Do not user emacs to edit the file! The steps are illustrated below:
getcopy viFile.txt vi viFile.txt submitVi viFile.txt
- Good short reference on vi
- Colorado State's page on vi
Redirection
- We have seen in the previous lab how to use pipes to take the output of a program and feed it to the input of another.
- Redirection is similar, except that the input or the output is fed from, or fed to a file, respectively.
Example 1: storing the output of a command into a file
- Try this:
last
- this will list the last login times of all the users on Aurora. If you want to target a particular user, say 231b, you can try this:
last | grep "231b " (make sure there's a space after the b)
- Assume you want to store these login time into a file, so that you can share it with somebody else, or send as an attachment. You simply use the > sign to store the output of the last command into a file:
last | grep "231b " > logins231b.txt
- You won't see any output, as it is being redirected. Look at the files in your directory and see that you have a new file called logins231b.txt.
- Display the contents of this file with the cat or with the less command.
You now have a new way of storing the output of a program into a file.
Challenge #1: |
Create a text file called "231Students.txt" that contains their last login information, but make sure the information from User 231b (your instructor) is not stored in the file.
Side-step: Sorting text
Linux sports a command to sort text. It's called "sort." To see how it works, try this:
last | sort
- If you had wanted the output in reverse order, you could have typed:
last | sort -r
- Sometimes the information returned by a linux command contains numbers at the beginning of the first line. du, which returns the "disk usage" of a directory, i.e. the amount of disk space taken by each file, is such a command. Let's try it:
du /etc
- You get a list of all the files in the /etc directory, with their size at the beginning of each line.
- Try to sort this list:
du /etc | sort
- See the problem? Sorting numbers alphabetically does not yield what we expect. Instead we can tell sort to sort numerically:
du /etc | sort -n
Challenge #2: |
Create a text file called "231Students.txt" using redirection, and make it contain the login information for 231 students, sorted in alphabetical order, but make sure the information from User 231b (your instructor) is not stored in the file.
Challenge #3: |
Create a text file using redirection that contains only the 10 largest files found in the /etc/ directory. The file should be called top10etcFiles.txt. Remove the last line of the output of sort which lists the total for the /etc directory. We want the largest except /etc. In other words, /etc/ shouldn't be included in the top10etcFiles.txt file.
Redirecting Stderr
- Run the du command another time, and pay attention to the lines that contain a "Permission denied" message:
du /etc
- When I run it, I get these messages interspersed here and there:
28 /etc/pm 4 /etc/ndiswrapper 8 /etc/rc4.d 4 /etc/mate-settings-daemon/xrandr 8 /etc/mate-settings-daemon du: cannot read directory ‘/etc/chatscripts’: Permission denied 4 /etc/chatscripts 28088 /etc
- These messages are error messages reported by du. They do not create du to crash. du is just reporting failure to look at a particular directory or file.
- Let's capture all these "Permission denied" message to a file:
du /etc > duErrors.txt
- and let's look in the file just captured:
cat duErrors.txt
- ?
- Do you see anything in duErrors.txt?
- Why?
- What's happening?
- The explanation is that when du outputs one of the "Permission denied" lines, it does not output them to sdtout, the standard output. It outputs the to a different output stream, called stderr, for standard error. This is the stream one should use to output error messages.
- By default, outputs to stderr and stdout are sent to the screen. If you use > to capture the output of a program, you capture only the part sent to stdout and not the part sent to stderr. To capture the output sent to stderr to a file, we need to use 2> as a symbol:
du /etc 2> duErrors.txt
- and now try:
cat duErrors.txt
- You have captured the stderr output to a file.
Getting Rid of Stderr
Sometimes we know that there will be message sent to stderr by the command/program, but we don't want to see them. Possibly because there are too many.
For example, assume we want to apply du to all the student accounts, which live in /Users/classes/ and sort the resulting list to see who's hogging up the most disk space:
du /Users/classes | sort -n | tail -n 10
- Try the previous command. See how the error messages are polluting the output?
- What we could do, instead, is sent the stderr lines to a file, and sort only the stdout lines coming out of du:
du /Users/classes 2> duErrors.txt | sort -n | tail -n 10
- Try it.
- The output is much cleaner, isn't it?
Challenge #4: |
Get a copy of the java program GenErrors.class
getcopy GenErrors.class
and run it:
java GenErrors
- How many lines does this program send to stderr?
- How many lines does it send to stdout?
Redirecting stdin
Stdin normally represents the keyboard. To see how you can redirect it, create this very simple Python program called demo.py:
#! /usr/bin/env python3 print( "Enter 3 integers, one per line: " ) x = int( input( "> " ) ) y = int( input( "> " ) ) z = int( input( "> " ) ) print( "sum = ", x+y+z )
- and make the program executable:
chmod +x demo.py
- and run it:
./demo.py
- Assume that this program computes something much more involved than the simple summation shown here, and that you have to run and test your program many times with the same values, say 10, 20, and 30. One way to spend less time running your program is to force it to get its inputs from a file rather than from the keyboard, and force you to enter the same numbers over and over:
- Create a text file your favorite editor called myinputs.txt and add 3 lines containing the 3 numbers:
10 20 30
- 10 should be one the first line. 30 on the third. Make sure you type ENTER at least twice after the last line.
- Now run the program and force it to get its input from the file myinputs.txt:
./demo.py < myinputs.txt
- You will notice that the output is confusing. None of the numbers obtained from the input file appear on the screen, and all the input prompts are on the same line. This is because when you type an input at the keyboard, it automatically is echoed on on the screen, including when you press the ENTER key: it moves the cursor on the screen to the next line. When you feed the input to the program from a file, then there's no echo to the display. This is why the 3 prompts are on the same line, and why we don't see the 3 numbers that are fed to the program.
- Get used to it. That's how redirecting the input works.
Challenge #5: |
Run the demo.py program again, so that it gets its input from the myinputs.txt file, and such that you capture its output, and only the line that says "sum =" into a file called myoutput.txt.
Moodle Quiz
You are ready for the Moodle quiz!
</showafterdate>
<showafterdate after="2017024 11:45" before="20170601 00:00">
Challenges Solutions
- Challenge 1
last | grep "231b" | grep -v "231b " > 231students.txt
- Challenge 2
last | grep "231b" | grep -v "231b " | sort > 231students.txt
- Challenge 3
du /etc | sort -n | tail -n 11 | head -n 10 > top10filesInEtc.txt
- Challenge 4
java GenErrors 2> GenErrors.txt | wc -l 9915
- 9915 lines are sent to stdout
wc -l GenErrors.txt 85 GenErrors.txt
- 85 lines are sent to stderr
- Challenge 5
./demo.py < myinputs.txt | grep sum > myoutputs.txt cat myoutputs.txt > > > sum = 60
</showafterdate>