DispersedNet DispersedNet

Shell Functions  «Prev  Next»
Lesson 6Sharing data with functions
ObjectiveUse variables to share data with a function.

Sharing data with functions for Unix Shell Scripts

You can pass information to a function when you call it. List the information right after the function call. Listing information after the function call is referred to as passing arguments to the function. The first argument to the function is copied into the variable $1, which can be used inside of the function definition. The second argument is copied into $2, third into $3, and so on. <Create graphic with the following information. Arrows should be inserted pointing from val1 to $1, val2 to $2, val3 to $3, etc.> $1 $2 $3 $4 $5 funcname val1 val2 val3 val4 val5 <end graphic> Step through the simulation below to practice calling a function inside a script.

Common Commands used within the vi Editor

The vi editor is a powerful text editor that is commonly used in the Unix/Linux environment. Some common commands used within the vi editor are:
  1. i: switch to insert mode to start editing text
  2. x: delete the character under the cursor
  3. dd: delete the entire line
  4. yy: copy the entire line
  5. p: paste the last deleted or copied text
  6. u: undo the last change
  7. :w: save the file
  8. :q: quit the editor
  9. :wq: save and quit the editor
  10. /search_term: search for a specific term within the file
  11. :set number: display line numbers in the editor
  12. :set syntax=python: enable syntax highlighting for Python code (replace "python" with the appropriate language)
These are just a few examples of the many commands available within the vi editor. The editor can be a bit difficult to learn at first, but with practice, it can become a powerful tool for editing text files.
You are inside the vi editor, editing a script called sortfunc. You must add a call to the sort_file function here. Type in a call to sort_file using column 6 and filename petchip as your arguments. Press Enter when you are finished.
action, Wait for them to type
sort_file 6 petchip
, then go to node 2 on pressing Enter/Return.
any other action, “Please type in
sort_file 6 petchip

to call the sort_file function.”

We will save the file for you and exit vi. Press return when you are ready.
action, Wait for then to type Return/Enter, then go to node 3.
any other action, “Please type Return/Enter when you are ready.”
instructions, You are now on the command line. Run the sortfunc script.
action, Wait for them to type
, then go to node 4.
any other action, “Please type sortfunc and then press enter to run the script.”
instructions, Your call to the sort_file function passed your two values (6 and petchip) to the sort_file function. The output of the command shows the petchip file sorted on the seventh field, the owner’s last name. This is the end of the simulation. Press Exit.
If you are familiar with basic programming concepts, you might be interested in reading this sidebar [[variable scope and functions]].
The next lesson identifies the qualities that make a script easy to reuse.