1 / 15

Introduction to Unix (CA263) More on Parameters

Introduction to Unix (CA263) More on Parameters. By Tariq Ibn Aziz Dammam Community College. Objectives. In this lecture you will learn The $0 Variable The set Command The IFS Variable The readonly Command The unset Command. The $0 Variable.

wrayj
Download Presentation

Introduction to Unix (CA263) More on Parameters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Unix (CA263)More on Parameters By Tariq Ibn Aziz Dammam Community College Compunet Corporation

  2. Objectives • In this lecture you will learn • The $0 Variable • The set Command • The IFS Variable • The readonly Command • The unset Command Compunet Corporation

  3. The $0 Variable • The shell automatically store the name of the program inside the special variable $0. $ cat lu # # Look someone up in the phone book # if [ "$#" –ne 1 ] then echo "Incorrect number of arguments" echo "Usage: $0 name" exit 1 fi grep "$1" $PHONEBOOK Compunet Corporation

  4. The set Command • The shell set command is a dual purpose command. It’s used both to set various shell options as well as to reassign the positional parameter $1, $2, ... $ x=* $ set –x $ echo $x +echo add greetings lu rem rolo add greetings lu rem rolo • To turn off trace option. $ set +x $ echo $x add greetings lu rem rolo Compunet Corporation

  5. The set Command • You can trace a subshell execution either by running the shell with the –x option followed by the name of the program to be executed. $ sh –x rolo Compunet Corporation

  6. Set with No Arguments • If you don’t give any argument to set, you will get alphabetized list of all of the variables that exist in your environment. $ set CDPATH=:/usr/steve:/usr/spool EDITOR=/bin/vi HOME=/usr/steve IFS= PATH PS1=$ PS2=> PWD=/usr/steve/misc SHELL=/bin/sh TERM=hp2621 x=* Compunet Corporation

  7. Using set to Reassign Positional Parameters $ set a b c assigns a to $1, b to $2, and c to $3. $# also gets set to 3. $ set one two three four $ echo $1:$2:$3:$4 one:two:three:four $ echo $# 4 $ echo $* one two three four Compunet Corporation

  8. set Example $ for arg; do wcho $arg; done one two three four Compunet Corporation

  9. set Example $ cat words read line set $line echo $# $ words Here’s a line for you to count 7 $ Compunet Corporation

  10. The IFS Variable • There is a special shell variable called IFS, which stand for Internal Field Seperator. • The shell uses this variable when parsing input from the read command, output from command substitution, and when performing variable substitution. $ echo "$IFS" Compunet Corporation

  11. $ IFS=: $ read x y z 123:345:678 $ echo $x 123 $ echo $z 678 $ list="one:two:three“ $ for x in $list > do > echo $x > done one two three $ var=a:b:c $ echo "$var“ a:b:c The IFS Example Compunet Corporation

  12. Changing IFS with set $ line=“Micro corp:Box 174: Dammam, 062152“ $ IFS=: $ set $line $ echo $# 3 $ for field; do echo $field; done Micro Corp Box 174 Dammam, 062152 $ Compunet Corporation

  13. The readonly Command • The readonly command is used to specify variables whose values cannot be subsequently changed. readonly PATH HOME makes the PATH and HOME variable readonly. After this assigning a value to these variable will cause shell to issue an error message. Compunet Corporation

  14. The readonly Command • To get a list of your readonly variables, type readonly without any argument. $ readonly PATH HOME Compunet Corporation

  15. The unset Command • Sometime you may wish to remove the definition of a variable from your environment. $ x=100 $ echo $x 100 $ unset x $ echo $x $ • You can’t unset a readonly variable. Furthermore , the variable IFS, MAIL-CHECK, PATH, PS1, and PS2 cannot be unset. Compunet Corporation

More Related