CSC231 Bash Tutorial 6

From dftwiki3
Revision as of 22:36, 21 March 2017 by Thiebaut (talk | contribs) ($1)
Jump to: navigation, search

--D. Thiebaut (talk) 22:18, 21 March 2017 (EDT)


Script File


Let's create a simple batch file that will allow you to automatically assemble, link, and run an assembly program. Furthermore, we'll link it with the 231Lib library, just to be safe. If it doesn't use it, it should be fine.

The commands you normally use to go through this process are the following:

nasm -f elf progName.asm
ld -melf_i386 progName.o 231Lib.o -o progName
./progName

Let's create a script file called nald (for nasm ld) that will run these 3 commands automatically. For now, just copy the code without worrying too much about what happens. We'll explain what's going on later.

  • emacs a new file called nald
  • Store the following lines in it:


 
#! /bin/bash
 
nasm -f elf $1.asm
nasm -f elf 231Lib.asm
ld -melf_i386 $1.o 231Lib.o -o $1
./$1


  • Make sure that the #-sign is the first character on the first line of the script.
  • Make the script executable:
chmod +x nald

chmod is a command that changes the permissions of a file. "+x" means that everybody including you, everybody in the 231 class, and everybody who has an account on Aurora can execute your script.


  • now run the script and pass it the name of one of your assembly files. Let's assume that you have a file called helloWorld.asm in your directory:
./nald  helloWorld

You should see the output of your program printed on the screen.
  • Using emacs, make a slight modification to your helloWorld.asm program. Maybe, make it print something different.
  • reassemble and run it in one command:
./nald  helloWorld

  • You now have a nice script that will save you several keystrokes when you create your next assembly programs!


Explanations


Shebang


#! /bin/bash
the first line of the script starts with the pair "#!" which is referred to as "shebang" in the Linux world. It indicates that the command in the script should be executed by the bash shell. If we had Python program that we would want to run automatically, we would use a shebang with Python. Let's try that.
  • Create a new file with emacs called hello.py


 #! /usr/bin/python
 
 from __future__ import print_function
 
 def main():
      print( "Hello world of Python!" )
 
 main()


  • Save the file, and make it executable
chmod +x hello.py

  • run the program from the command line:
./hello.py
Hello world of Python!

  • Notice that you didn't write "python hello.py" to run your program. Just "./hello.py." That's a nice trick.
  • In summary, the shebang defines the interpreter to use to read the file and execute the lines.


$1


Command line parameters
inside the script, we can access the parameters that were typed on the command line using $1, $2, $3, and some others. To best understand how this works, create this simple shell script with emacs called script1.sh:


#! /bin/bash

echo "You typed $# words on the command line"
echo "The whole collection of words is $@"
echo "Word 0 is $0"
echo "Word 1 is $1"
echo "Word 2 is $2"
echo "Word 3 is $3"
echo "Word 4 is $4"


  • Make the script executable:
 chmod +x  script1.sh

  • And try it with several command line parameters:
 ./script1.sh  hello world 3 20 2017

 ./script1.sh  CSC231


  • so this is how the script can access the parameters passed on the command line. When you typed nald fileName on the command line earlier, the name of your file becomes $1 inside the script.