© prepared by: razif razali 1 tmk 265: unix system chapter 8: user input

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

Upload: barry-norris

Post on 20-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

© Prepared By:Razif Razali 1

TMK 265: UNIX SYSTEMTMK 265: UNIX SYSTEM

CHAPTER 8: USER INPUT CHAPTER 8: USER INPUT

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

© Prepared By:Razif Razali

2

INTRODUCTIONINTRODUCTION

• In this chapter you’ll learn how to In this chapter you’ll learn how to read data from the terminal or from a read data from the terminal or from a file using the read command and file using the read command and how to print formatted data to how to print formatted data to standard output using the printf standard output using the printf command.command.

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

READING DATAREADING 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 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 word read to the first variable listed in variables, the second word read to the second variable, and so on.so on.

© Prepared By:Razif Razali

3

read variables

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

ExampleExample

© Prepared By:Razif Razali

4

# !bin/sh#Script to read your name from key-board

echo "Your first name please:"read fnameecho "Hello $fname, Lets be friend!"

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

The printf commandThe printf command

• Although echo is adequate for displaying simple message, Although echo is adequate for displaying simple message, sometimes you’ll want to print formatted output. For example, sometimes you’ll want to print formatted output. For example, lining up column of data.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 Those of you familiar with the C programming language will

notice many similarities.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 …

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

The printf commandThe printf command

• Where format is a string that describes how the remaining Where format is a string that describes how the remaining arguments are to be displayed.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

$

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

• Table below summarizes the different conversion Table below summarizes the different conversion specification characters:specification characters:

© Prepared By:Razif Razali

7

Character Use for printing

d Integers

u Unsigned integers

o Octal integers

x Hexadecimal integers, using a – f

X Hexadecimal integers, using A – F

c Single character

s Literal string

b String containing backslash escape character

% Percent signs

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

• Here are few of printf examples Here 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

$

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

ExerciseExercise

• Write the shell script for the user to input the Write the shell script for the user to input the information above and display it again:information above and display it again:– Name– Your Id– Address– Age– Age Next Year

© Prepared By:Razif Razali

9

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

14-10 Dr. Tim Gottleber

read exampleread example

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

14-11 Dr. Tim Gottleber

expr examplesexpr examples

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

14-12

Other uses for exprOther 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 While expr can perform these tests, this type of usage is very uncommonuncommon

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

Script exampleScript example#!/bin/bash#!/bin/bashecho menu test programecho menu test programstop=0 stop=0 # reset loop termination flag.# reset loop termination flag.while test $stop -eq 0 while test $stop -eq 0 # loop until done.# loop until done.dodo cat << ENDOFMENU cat << ENDOFMENU # display menu.# 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 : exit4 : exitENDOFMENUENDOFMENU echoecho echo -n 'your choice? ' echo -n 'your choice? ' # prompt. # prompt. read reply read reply # read response.# read response. echoecho case $reply in case $reply in # process response.# process response. "1") "1") date ;; date ;; # display date # display date.. "2"|"3") "2"|"3") pwd ;; pwd ;; # display working directory.# display working directory. "4") "4") stop=1 ;; stop=1 ;; # set loop termination flag.# set loop termination flag. *) # default.*) # default. echo “illegal choice” ;; echo “illegal choice” ;; # error.# error. esacesac donedone

© Prepared By:Razif Razali

13