Download presentation
Presentation is loading. Please wait.
1
Compunet Corporation Introduction to Unix (CA263) More on Parameters By Tariq Ibn Aziz Dammam Community College
2
Compunet Corporation Objectives In this lecture you will learn –The $0 Variable –The set Command –The IFS Variable –The readonly Command –The unset Command
3
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
4
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
5
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
6
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=*
7
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
8
Compunet Corporation set Example $ for arg; do wcho $arg; done one two three four
9
Compunet Corporation set Example $ cat words read line set $line echo $# $ words Here’s a line for you to count 7 $
10
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"
11
Compunet Corporation The IFS Example $ 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
12
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 $
13
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.
14
Compunet Corporation The readonly Command To get a list of your readonly variables, type readonly without any argument. $readonly PATH HOME
15
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.