Difference between revisions of "CSC231 Bash Tutorial 2"

From dftwiki3
Jump to: navigation, search
(Fewer Keystrokes with !)
(Finding the "type" of a File)
Line 67: Line 67:
 
Sometimes not.
 
Sometimes not.
  
So we can ask Linux to tell us:
+
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 '''*'''.  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*
 +
 +
<br />
 +
<tanbox>
 +
Note that your executable files are reported by the '''file''' command as:<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 (to which the Pentium belongs), 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>
 +
<br />
  
 
<br />
 
<br />

Revision as of 09:34, 9 February 2017

--D. Thiebaut (talk) 09:06, 9 February 2017 (EST)




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.



Finding the "type" of a File


In our computers we have 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 *. 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 (to which the Pentium belongs), 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.