190 likes | 284 Views
Introduction to Unix (CA263) Reading Data. By Tariq Ibn Aziz Dammam Community College. Objectives. In this lecture you will learn how to read data Special echo Escape characters The $$ variable and temporary file The exit status from read. read variables.
E N D
Introduction to Unix (CA263)Reading Data By Tariq Ibn Aziz Dammam Community College Compunet Corporation
Objectives • In this lecture you will learn • how to read data • Special echo Escape characters • The $$ variable and temporary file • The exit status from read Compunet Corporation
read variables • When read command is executed, the shell read a line from standard input and assign the first word read to the first variable listed in variables, the second word read to the second variables, and so on. read x y Compunet Corporation
read variables[2] $ read text • Will read and store an entire line into the shell variable text. Compunet Corporation
Special echo Escape Characters • The echo command always automatically displays a terminating newline character after the last argument. • This can be suppressed if the last two character given to echo are the special escape character \c. • Use echo –e to make effect of escape characters Compunet Corporation
read Example[1] $cat rolo echo –e ' Would you like to: 1. Look up someone 2. Add someone to the phone book 3. Remove someone from the phone book Please select one of the above (1 – 3): \c ' read choice echo case "$choice" in • echo "Enter name to the look up:\c" read name lu "$name";;
read Example[2] 2) echo "Enter name to be added:\c" read name echo "enter number: \c" add "$name" "$number";; 3) echo "Enter name to be removed:\c" read name rem "$name";; *) echo "Bad Choice";; esac $ Compunet Corporation
add program $ cat lu # # Look someone up in the phone book # grep "$1" phonebook Compunet Corporation
rem program $ cat rem # # remove someone to the phone book # grep –v "$1" phonebook >/tmp/phonebook mv /tmp/phonebook phonebook $ Compunet Corporation
add program $ cat add # # add someone to the phone book version 2 # echo "$1 $2" >> phonebook Sort –o phonebook phonebook $ Compunet Corporation
The $$ Variable and Temporary Files • If two or more people on your system use the rolo program at the same time, there’s a potential problem that may occur. • In rem program problem may occur with the /tmp/phonebook. • If more than one person is using rolo to remove an entry at the same time, phonebook file can get messed up. Compunet Corporation
The $$ Variable and Temporary Files • $$ is equal to the process id number (PID) of your login shell. • Each process on the UNIX system is given a unique process id number, using the value of $$ in the name of file will minimize the possibility of another process using the same file. grep –v "$1" phonebook >/tmp/phonebook$$ mv /tmp/phonebook$$ phonebook Compunet Corporation
The $$ Variable $ echo $$ 4668 $ ps PID TTY TIME COMMAND 4668 co 0:09 sh 6470 co 0:03 ps Compunet Corporation
Exit Status from read • read always returns exit status of zero unless an end of the condition is detected on the input. • Ctrl +d is the end of input from terminal • If data is coming from a file, then it means that there’s no more data to read from the file. Compunet Corporation
$ cat addi # add pairs of # integer while read n1 n2 do expr "$n1" + "$n2" done $ $ addi 10 25 35 -5 12 7 123 3 126 Ctrl-d $ Exit status Example[1] Compunet Corporation
$ cat data 1234 7960 593 -595 395 304 3234 999 -394 -493 $ addi < data > sums $ $ cat sums 9194 -2 699 4233 -887 $ Exit status Example[2] Compunet Corporation
$ cat number lineno=1 cat $* | While read line do echo "$lineno. $line" lineno=`expr $lineno + 1` done $ $ cat phonebook Tariq 877 Rashid 816 Arshad 884 $ number phonebook Tariq 877 Rashid 816 Arshad 884 Exit status Example[3] Compunet Corporation
Exit status Example[4] $ who | number • root console Jul 25 07:55 • tariq tty03 Jul 25 07:55 • rashid tty13 Jul 25 07:55 • arshad tty04 Jul 25 07:55 $ Compunet Corporation
Exit status Example[5] • The number program don’t work well for lines that contains backslashes or leading whitespace characters. $ number Here are some backslashes:\ \* • Here are some backslashes: * $ • Leading whitespaces characters are removed from any line that’s read. The backslash characters are also interpreted by the shell when it reads the line. Compunet Corporation