150 likes | 163 Views
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.
E N D
Introduction to Unix (CA263)More on Parameters By Tariq Ibn Aziz Dammam Community College Compunet Corporation
Objectives • In this lecture you will learn • The $0 Variable • The set Command • The IFS Variable • The readonly Command • The unset Command Compunet Corporation
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
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
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
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
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
set Example $ for arg; do wcho $arg; done one two three four Compunet Corporation
set Example $ cat words read line set $line echo $# $ words Here’s a line for you to count 7 $ Compunet Corporation
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
$ 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
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
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
The readonly Command • To get a list of your readonly variables, type readonly without any argument. $ readonly PATH HOME Compunet Corporation
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