1 / 19

Introduction to Unix (CA263) Reading Data

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.

lluvia
Download Presentation

Introduction to Unix (CA263) Reading Data

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)Reading Data By Tariq Ibn Aziz Dammam Community College Compunet Corporation

  2. 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

  3. 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

  4. read variables[2] $ read text • Will read and store an entire line into the shell variable text. Compunet Corporation

  5. 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

  6. 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";;

  7. 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

  8. add program $ cat lu # # Look someone up in the phone book # grep "$1" phonebook Compunet Corporation

  9. rem program $ cat rem # # remove someone to the phone book # grep –v "$1" phonebook >/tmp/phonebook mv /tmp/phonebook phonebook $ Compunet Corporation

  10. add program $ cat add # # add someone to the phone book version 2 # echo "$1 $2" >> phonebook Sort –o phonebook phonebook $ Compunet Corporation

  11. 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

  12. 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

  13. The $$ Variable $ echo $$ 4668 $ ps PID TTY TIME COMMAND 4668 co 0:09 sh 6470 co 0:03 ps Compunet Corporation

  14. 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

  15. $ 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

  16. $ 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

  17. $ 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

  18. 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

  19. 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

More Related