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

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Printing. printf: formatted printing So far we have just been copying stuff from standard-in, files, pipes, etc to the screen or another file. Say I have.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Chapter 15 Introductory Bash Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
CS465 - UNIX The Bourne Shell.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Chapter 2 Simple Data Types By C. Shing ITEC Dept Radford University.
1 CSE 303 Lecture 5 bash continued: users/groups; permissions; intro to scripting read Linux Pocket Guide pp slides created by Marty Stepp
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Control Statements and More
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
Compunet Corporation Introduction to Unix (CA263) Reading Data By Tariq Ibn Aziz Dammam Community College.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Shell scripts – part 1 Cs 302. Shell scripts  What is Shell Script? “Shell Script is series of command written in plain text file. “  Why to Write Shell.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Shell Control Structures
TMF1414 Introduction to Programming
Writing Shell Scripts ─ part 3
Chapter 9 - Formatted Input/Output
Introduction to C Programming
Shell Control Structures
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Presentation transcript:

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

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

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

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!"

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 …

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 $

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

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” The Octal value for 20 is 24 $ printf “The hexadecimal value for %d is %x\n” The hexadecimal value for 30 is 1e $ printf “The unsigned value for %d is %u\n” The unsigned value for is $ 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 $

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

14-10Dr. Tim Gottleber read example

14-11Dr. Tim Gottleber expr examples

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

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