Difference between revisions of "CSC231 Bash Tutorial 2"
(→References) |
(→Pushd/Popd) |
||
(16 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
<br /> | <br /> | ||
__TOC__ | __TOC__ | ||
+ | <br /> | ||
+ | <bluebox> | ||
+ | At the end of the lab, go to Moodle to answer the quiz. The quiz starts on 9/21/17 at noon, and closes on 9/22/17 at noon. | ||
+ | </bluebox> | ||
<br /> | <br /> | ||
=Pushd/Popd= | =Pushd/Popd= | ||
<br /> | <br /> | ||
− | Sometimes you need to go briefly in a directory to work on some files, and then you want to return where you were. '''Pushd''' pushes your current directory in a ''stack'' that '''bash'''manages, and takes you to the directory you want to go to. Once you're done there, you just '''popd''' back to the directory where you started. '''Popd''' pops the directory at the top of the internal '''bash''' stack, and takes you there. | + | Sometimes you need to go briefly in a directory to work on some files, and then you want to return where you were. '''Pushd''' pushes your current directory in a ''stack'' that '''bash''' manages, and takes you to the directory you want to go to. Once you're done there, you just '''popd''' back to the directory where you started. '''Popd''' pops the directory at the top of the internal '''bash''' stack, and takes you there. |
Let's try it. | Let's try it. | ||
Line 35: | Line 39: | ||
That should make sense, hopefully. | That should make sense, hopefully. | ||
<br /> | <br /> | ||
+ | |||
=Your History= | =Your History= | ||
<br /> | <br /> | ||
Line 89: | Line 94: | ||
</tanbox> | </tanbox> | ||
<br /> | <br /> | ||
+ | ==Fewer Keystrokes still, with !! == | ||
+ | <br /> | ||
+ | * Want to repeat the last command you just typed? Try '''!!''' | ||
<br /> | <br /> | ||
− | + | ==the '''history''' command== | |
− | =Finding the | + | <br /> |
+ | You can type the command '''history''' (yes, type the word history, then Enter). You will see a list of your last commands. If you want to execute one, say the one on Line 100, then you can simply type | ||
+ | |||
+ | !100 | ||
+ | |||
+ | at the keyboard. Cool, no? | ||
+ | <br /> | ||
+ | =Finding the Type of a File= | ||
<br /> | <br /> | ||
− | In our computers | + | In our computers there are many different file types: text files (created with an editor, such as emacs), doc files created by word processors (such as Microsoft Word), program files (java, python, assembly), object files, executable files, and more. |
Usually the file extension will tell us what type of file we have: .asm, .txt, .doc, .o, .java, .py, etc. | Usually the file extension will tell us what type of file we have: .asm, .txt, .doc, .o, .java, .py, etc. | ||
Line 108: | Line 123: | ||
file * | file * | ||
− | * With Linux (and Mac and Windows) the star character '''*''' matches any sequence of characters. So when you type '''file *''' you ask the '''file''' command to process all the file whose name matches '' | + | * With Linux (and Mac and Windows) the star character '''*''' matches any sequence of characters. So when you type '''file *''' you ask the '''file''' command to process all the file whose name matches ''anything''. So you get '''all''' the files in your current directory. If you wanted to get the type of just the files that start with "hello" you would type: |
file hello* | file hello* | ||
Line 116: | Line 131: | ||
Note that your executable files are reported by the '''file''' command as:<br /> | Note that your executable files are reported by the '''file''' command as:<br /> | ||
:<tt> ELF 32-bit LSB executable, Intel 80386</tt><br /> | :<tt> ELF 32-bit LSB executable, Intel 80386</tt><br /> | ||
− | This indicates that the file can be run directly by an Intel processor of the 80386 family ( | + | This indicates that the file can be run directly by an Intel processor of the 80386 family (which the Pentium is), using 32-bit registers (eax, ebx, ecx, etc.). The Pentium also supports 64-bit registers, but the assembly language for using them is totally different. In this class we will use only 32-bit assembly. |
</tanbox> | </tanbox> | ||
<br /> | <br /> | ||
+ | |||
=Displaying the Contents of Text Files= | =Displaying the Contents of Text Files= | ||
<br /> | <br /> | ||
Line 168: | Line 184: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 212: | Line 221: | ||
head -n 5 hello.asm | head -n 5 hello.asm | ||
− | Of course, if you want to see just the first line, you could use '''head -1 ''filename'' ''' | + | Of course, if you want to see just the first line, you could use '''head -n 1 ''filename'' ''' |
<br /> | <br /> | ||
==Multiple Files== | ==Multiple Files== | ||
Line 288: | Line 297: | ||
will display and number all the lines in Ulysses that contain the string "Mulligan." | will display and number all the lines in Ulysses that contain the string "Mulligan." | ||
+ | <br /> | ||
<br /> | <br /> | ||
{| style="width:100%; background:limegreen" | {| style="width:100%; background:limegreen" | ||
|- | |- | ||
| | | | ||
+ | |||
==Challenge #2:== | ==Challenge #2:== | ||
|} | |} | ||
[[Image:QuestionMark2.jpg|right|120px]] | [[Image:QuestionMark2.jpg|right|120px]] | ||
<br /> | <br /> | ||
− | What is the number of the line on which the name '''Roland''' | + | What is the number of the line on which the name '''Roland''' appears in Ulysses.txt? |
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 315: | Line 326: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | =Moodle Quiz= | ||
+ | [[Image:moodleLogo.png|right|150px]] | ||
+ | <br /> | ||
+ | You are now ready to take the Moodle quiz! Look for '''Lab #2''' in the '''Week 3''' section. | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 324: | Line 346: | ||
=Useful Links= | =Useful Links= | ||
<br /> | <br /> | ||
+ | * [http://www.computerhope.com/pushd.htm The '''pushd''' command] | ||
+ | * [http://www.computerhope.com/popd.htm The '''popd''' command] | ||
* [http://www.computerhope.com/unix/uless.htm The '''less''' command] | * [http://www.computerhope.com/unix/uless.htm The '''less''' command] | ||
* [http://www.computerhope.com/unix/ucat.htm The '''cat''' command] | * [http://www.computerhope.com/unix/ucat.htm The '''cat''' command] |
Latest revision as of 09:20, 22 September 2017
--D. Thiebaut (talk) 09:06, 9 February 2017 (EST)
Contents
At the end of the lab, go to Moodle to answer the quiz. The quiz starts on 9/21/17 at noon, and closes on 9/22/17 at noon.
Pushd/Popd
Sometimes you need to go briefly in a directory to work on some files, and then you want to return where you were. Pushd pushes your current directory in a stack that bash manages, and takes you to the directory you want to go to. Once you're done there, you just popd back to the directory where you started. Popd pops the directory at the top of the internal bash stack, and takes you there.
Let's try it.
- Type the following commands:
cd mkdir lab2demo cd lab2demo ls
- This was just to create a new working directory for you.
- Now let's go see what kind of games are installed on aurora
pushd /usr/games ls
- Run the game fortune a few times to see what it does:
./fortune ./fortune ./fortune
- Ok, time to go back to your lab2demo directory:
popd
That should make sense, hopefully.
Your History
The Bash shell keeps track of all the commands you type, at least the most recent ones. This list of commands is kept in a file, and when you login to your Linux account, the commands that you typed the last time you were connected are still in this file, available to you.
Let's figure out how to use your history to reduce your typing.
- If you do not have a working hello.asm program, get a copy of my version:
getcopy hello.asm
- Assemble, link, and run your program
nasm -f elf hello.asm ld -melf_i386 hello.o -o hello ./hello
- You should see the message printed.
Up and Down your History
- Edit hello.asm and modify the message, so that the program will print something different. It doesn't matter what.
emacs hello.asm
- You now have to assemble, link, and run your program again to see the result of the modification. Instead of typing the commands again...
- just press the UP arrow key on your keyboard a few times until your last nasm command. Press Enter when you have the command at the prompt.
- then press UP arrow again a few times and get the last ld command. Enter
- then press UP arrow again, and get ./hello
- You get the idea!
Fewer Keystrokes with !
- Go ahead and make another edit to change the message of your hello.asm program.
- Instead of using the arrow keys, you can be even more succinct: You know that the command you want to execute, nasm, again starts with na, so, at the prompt, type
!na
- this will ask Bash to locate the most recent command starting with the characters 'n' 'a', and to run it. Try it!
- Then try
!ld
- and you now have assembled and linked your program another time.
- You should have figured out a way to run it again:
!./
You do not need to use the first two characters. You could use just one. But if you do so, you may get the wrong command. For example, ls and ld start with the same letter "ell" so you would use two characters to specify that you want to repeat ld and not ls.
Fewer Keystrokes still, with !!
- Want to repeat the last command you just typed? Try !!
the history command
You can type the command history (yes, type the word history, then Enter). You will see a list of your last commands. If you want to execute one, say the one on Line 100, then you can simply type
!100
at the keyboard. Cool, no?
Finding the Type of a File
In our computers there are many different file types: text files (created with an editor, such as emacs), doc files created by word processors (such as Microsoft Word), program files (java, python, assembly), object files, executable files, and more.
Usually the file extension will tell us what type of file we have: .asm, .txt, .doc, .o, .java, .py, etc.
Sometimes not.
So we can ask Linux to tell us.
- Type the following commands, one after the other:
file hello file hello.asm file hello.o file *
- With Linux (and Mac and Windows) the star character * matches any sequence of characters. So when you type file * you ask the file command to process all the file whose name matches anything. So you get all the files in your current directory. If you wanted to get the type of just the files that start with "hello" you would type:
file hello*
Note that your executable files are reported by the file command as:
- ELF 32-bit LSB executable, Intel 80386
This indicates that the file can be run directly by an Intel processor of the 80386 family (which the Pentium is), using 32-bit registers (eax, ebx, ecx, etc.). The Pentium also supports 64-bit registers, but the assembly language for using them is totally different. In this class we will use only 32-bit assembly.
Displaying the Contents of Text Files
Sometimes we want to quickly check the contents of a text file without necessarily wanting to edit it. We could use emacs, but it is a bit slow and cumbersome. Instead we can just ask Linux to display the file on the screen.
Let's get a long text file to play with:
getcopy snowPoems.txt
Less
The first command you can use is less:
less snowPoems.txt
- The less command shows only one page of text at a time; one screen-full page.
- You can go down the pages with spacebar
- You can go up with b (for back one page)
- You can go up and down one line with the UP and DOWN arrow keys.
- You exit the less command by typing q (for quit).
- Go ahead and try these commands.
If you want to search for a particular word in the file, use less, and type / followed by the word you are looking for. This will take you to the first occurrence of the word. You can go to the next occurrence with n.
less snowPoems.txt /Buddha n n n q
Challenge #1: |
Get a copy of James Joyce's Ulysses. as follows:
getcopy Ulysses.txt
Using the less command, navigate through the lines of the book that contain the name of the main character: "Mulligan"
Cat
Less is a page-by-page explorer. Cat displays the whole file and returns to the bash prompt.
cat snowPoems.txt
Sometimes cat is a quick way that allows one to get a quick idea of what is inside a text file. Cat will also be useful later when we use pipes.
- Use cat to display the contents of your assembly programs, i.e. the files with a .asm extension.
cat prog1.asm cat hello.asm
- cat can also display two files one after the other. While this is a strange thing one might want to do, this will come in handy when we use pipes and redirection later on this semester.
cat prog1.asm prog2.asm
- Play with cat and display various text files.
- For fun, try to cat an executable, and see what you get. Why?
Head
If you want to just see the first few lines of a file, you can use the head command:
head snowPoems.txt head prog1.asm head hello.asm
If you want to see the first, say, 5 lines of a file, you can tell 'head with a switch:
head -n 5 snowPoems.txt head -n 5 prog1.asm head -n 5 hello.asm
Of course, if you want to see just the first line, you could use head -n 1 filename
Multiple Files
Head can also handle multiple files. If, for some reason, you wanted to list the first 5 lines of all your assembly programs, you would do this:
head -n 5 *.asm
Tail
Similarly, you can get the last 10 (default) or n lines of a text file with tail:
tail snowPoems.txt tail prog1.asm tail hello.asm tail -n 2 snowPoems.txt tail -n 3 prog1.asm tail -n 5 hello.asm
Searching Text Files with grep
Grep is one of the Linux commands one uses often. It's very efficient at finding strings inside text files.
Get 4 more assembly programs into your account, to make sure the examples below work well:
getcopy prog1.asm getcopy prog2.asm getcopy prog3.asm getcopy prog4.asm
- Assume we want to find the assembly program that contains the string "hello." You know you wrote one at some point, but forgot the program's name:
grep hello *.asm
Grep will list all the lines that contain hello, in all the .asm files in your directory. The file name is listed first, then the line containing the pattern.
- Use grep to find all the files that contain the word "world"
- Use grep to find all the files that contain the word World (note the uppercase W)
If you wanted to search for both world and World, you could do it with just one grep command, by using the "-i" switch to indicate that grep should be ignoring lower- or upper-case differences.
grep -i world *.asm
Searching for strings with spaces in them
If you want to search for several words, for example the string ecx, Hello in your assembly files, you must double quotes around the string:
grep -i "ecx, Hello" *.asm
Try it!
Escaping Special Characters
What if you actually are interested in searching for a double quote that is part of a string? For example, you'd like to find all the lines that declare strings starting with "hello
In this case, the solution is to escape the double quote with a \ character:
grep -i \"hello *.asm
This time grep understands that it has to look for a string starting with ", then h, then e, etc in all the assembly program.
Try this, and see how you can fine-tune your search!
Numbering Lines
You can ask grep to add line numbers in its output:
grep -n Mulligan Ulysses.txt
will display and number all the lines in Ulysses that contain the string "Mulligan."
Challenge #2: |
What is the number of the line on which the name Roland appears in Ulysses.txt?
Challenge #3: |
Find a quick and efficient way to figure out the line number for the first line and last line of the file snowPoems.txt that contain the words "snow" or "Snow".
Moodle Quiz
You are now ready to take the Moodle quiz! Look for Lab #2 in the Week 3 section.
Useful Links
- The pushd command
- The popd command
- The less command
- The cat command
- The head command
- The tail command
- The grep command