Compunet Corporation Introduction to Unix (CA263) Reading Data By Tariq Ibn Aziz Dammam Community College.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
EMT 2390L Lecture 4 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
Lesson 10-Controlling User Processes. Overview Managing and processing processes. Managing jobs. Exiting/quitting when jobs have been stopped.
CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Compunet Corporation Introduction to Unix (CA263) More on Parameters By Tariq Ibn Aziz Dammam Community College.
CS 497C – Introduction to UNIX Lecture 20: - The Shell Chin-Chih Chang
Introduction to Unix (CA263) Decisions, Decisions By Tariq Ibn Aziz Dammam Community College.
CS 497C – Introduction to UNIX Lecture 4: Understanding the UNIX Command Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
CS 497C – Introduction to UNIX Lecture 1: Getting Started Chin-Chih Chang
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
QUOTATION This chapter teaches you about a unique feature of the shell programming language: the way it interprets quote characters. Basically, the shell.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
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.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Essential Shell Programming by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
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.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Introduction to Unix (CA263) File Processing (continued) By Tariq Ibn Aziz.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Introduction to Unix (CA263) Passing Arguments By Tariq Ibn Aziz Dammam Community College.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
Compunet Corporation Introduction to Unix (CA263) Your Environment By Tariq Ibn Aziz Dammam Community College.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Introduction to Unix (CA263) Command File By Tariq Ibn Aziz.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Linux+ Guide to Linux Certification, Second Edition
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER 8: USER INPUT.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
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.
Shell Control Structures
Lecture 9 Shell Programming – Command substitution
Shell Script Assignment 1.
CSE 374 Programming Concepts & Tools
UNIX Reference Sheets CSE 2031 Fall 2010.
Shell Control Structures
Shell Control Structures
Introduction to Unix (CA263) Passing Arguments
Review The Unix Shells Graham Glass and King Ables,
Essential Shell Programming
Presentation transcript:

Compunet Corporation 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 \b backspace \c Remove new line \f Formfeed \n NewLine \r Carriage return \t Tab character \\ Backslash character \nnn ASCII value is nnn

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 1)echo "Enter name to the look up:\c" read name lu "$name";;

Compunet Corporation 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 Exit status Example[1] $ cat addi # add pairs of # integer while read n1 n2 do expr "$n1" + "$n2" done $ $ addi Ctrl-d $

Compunet Corporation Exit status Example[2] $ cat data $ addi sums $ $ cat sums $

Compunet Corporation Exit status Example[3] $ cat number lineno=1 cat $* | While read line do echo "$lineno. $line" lineno=`expr $lineno + 1` done $ $ cat phonebook Tariq877 Rashid816 Arshad884 $ number phonebook 1.Tariq Rashid816 3.Arshad884

Compunet Corporation Exit status Example[4] $ who | number 1.root console Jul 25 07:55 2.tariq tty03 Jul 25 07:55 3.rashid tty13 Jul 25 07:55 4.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.