Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER 8: USER INPUT.

Similar presentations


Presentation on theme: "© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER 8: USER INPUT."— Presentation transcript:

1 © Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER 8: USER INPUT

2 © Prepared By: Razif Razali 2 INTRODUCTION In this chapter you’ll learn how to read data from the terminal or from a file using the read command and how to print formatted data to standard output using the printf command.In this chapter you’ll learn how to read data from the terminal or from a file using the read command and how to print formatted data to standard output using the printf command.

3 READING DATA To read the data, user can use read command. To read the data, user can use read command. This read command is used to get input (data from user) from keyboard and store (data) to variable.This read command is used to get input (data from user) from keyboard and store (data) to variable. The general format of the read command is:The general format of the read command is: When the command is executed, the shell reads a line from standard input and assigns the first word read to the first variable listed in variables, the second word read to the second variable, and so on.When the command is executed, the shell reads a line from standard input and assigns the first word read to the first variable listed in variables, the second word read to the second variable, and so on. © Prepared By: Razif Razali 3 read variables

4 Example © Prepared By: Razif Razali 4 # !bin/sh #Script to read your name from key-board echo "Your first name please:" read fname echo "Hello $fname, Lets be friend!"

5 The printf command Although echo is adequate for displaying simple message, sometimes you’ll want to print formatted output. For example, lining up column of data.Although echo is adequate for displaying simple message, sometimes you’ll want to print formatted output. For example, lining up column of data. UNIX system provides the printf command.UNIX system provides the printf command. Those of you familiar with the C programming language will notice many similarities.Those of you familiar with the C programming language will notice many similarities. The general format of the printf command is:The general format of the printf command is: © Prepared By: Razif Razali 5 printf “format” arg1 arg2 …

6 The printf command Where format is a string that describes how the remaining arguments are to be displayed.Where format is a string that describes how the remaining arguments are to be displayed. Here is a simple example of printf command:Here is a simple example of printf command: © Prepared By: Razif Razali 6 $ printf “This is a number: %d\n” 10 This is a number: 10 $

7 Table below summarizes the different conversion specification characters:Table below summarizes the different conversion specification characters: © Prepared By: Razif Razali 7 CharacterUse for printing dIntegers uUnsigned integers oOctal integers xHexadecimal integers, using a – f XHexadecimal integers, using A – F cSingle character sLiteral string bString containing backslash escape character %Percent signs

8 Here are few of printf examplesHere are few of printf examples © Prepared By: Razif Razali 8 $ printf “The Octal value for %d is %o\n” 20 20 The Octal value for 20 is 24 $ printf “The hexadecimal value for %d is %x\n” 30 30 The hexadecimal value for 30 is 1e $ printf “The unsigned value for %d is %u\n” -1000 -1000 The unsigned value for -1000 is 4294966296 $ printf “This string contains a backslash escape: %s\n” “ test\nstring” This string contains a backslash escape: test\nstring $ printf “A string: %s and a character: %c\n” HELLO A A string: HELLO and a character: A $

9 Exercise Write the shell script for the user to input the information above and display it again:Write the shell script for the user to input the information above and display it again: –Name –Your Id –Address –Age –Age Next Year © Prepared By: Razif Razali 9

10 14-10Dr. Tim Gottleber read example

11 14-11Dr. Tim Gottleber expr examples

12 14-12 Other uses for expr Expr can also perform:Expr can also perform: –relational operations =, !=, >, =, and <= –logical operations whether one of the arguments is empty whether the arguments contain the same data if the second argument contains the first argument While expr can perform these tests, this type of usage is very uncommonWhile expr can perform these tests, this type of usage is very uncommon

13 Script example #!/bin/bash echo menu test program stop=0 # reset loop termination flag. while test $stop -eq 0 # loop until done. do cat << ENDOFMENU # display menu. cat << ENDOFMENU # display menu. 1 : print the date. 1 : print the date. 2, 3: print the current working directory. 2, 3: print the current working directory. 4 : exit 4 : exitENDOFMENU echo echo echo -n 'your choice? ' # prompt. echo -n 'your choice? ' # prompt. read reply # read response. read reply # read response. echo echo case $reply in # process response. case $reply in # process response. "1") date ;; # display date. "1") date ;; # display date. "2"|"3") pwd ;; # display working directory. "2"|"3") pwd ;; # display working directory. "4") stop=1 ;; # set loop termination flag. "4") stop=1 ;; # set loop termination flag. *) # default. *) # default. echo “illegal choice” ;; # error. echo “illegal choice” ;; # error. esac esac done done © Prepared By: Razif Razali 13


Download ppt "© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER 8: USER INPUT."

Similar presentations


Ads by Google