emt 2390l lecture 8 dr. reyes reference: the linux command line, w.e. shotts

12
EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Upload: merryl-casey

Post on 02-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

EMT 2390LLecture 8

Dr. ReyesReference: The Linux Command Line, W.E. Shotts

Page 2: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Outline• Shell Scripts• Hello World• Variables• Getting Input

Page 3: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Shell Scripts• What is a script?

o A file containing a set of commands

• How to write a shell script?1. Write a script file using a text editor2. Make the script executable3. Specify the location of the script

Page 4: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Hello World• Script File Format

• Features:o #! – shebang character is a sequence that indicates the name of the

interpreter to be used to execute the scripto # - is a commento echo – command to display output

Page 5: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Hello World• vi hello_world• Copy:

#!/bin/bash# This is our first script.echo 'Hello World!‘

• :wq• chmod 755 hello_world• ./hello_world

Page 6: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Scripts Location• Scripts are usually located in the following

directorieso ~/bin - for personal useo /usr/local/bin – for anyone to useo /usr/local/sbin – for administrators to useo Custom directories added to the PATH variable

• Best Practiceso Use correct indentation techniques for multiple commandso For commands that spam several lines, use the line-continuation

sequences (backslash)

Page 7: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Variables• To create a variable use a name and assignment

statement and assign a value to it.• Leave NO SPACES between the name and the

assignment statement• No datatype need to be specified, as the shell

treats everything as a stringo name=‘John Smith’o age=19

Page 8: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Variable Names Conventions

• You may use alphanumeric characters (letter and numbers) and underscore

• First character must be a letter or underscore• Space and special characters are not allowed• Software Engineering Considerations:

o Use Pascal case for variableso Use ALL CAPS for constants

• To read the content of a variable use $ and the variable nameo i.e. for variable name use $name

Page 9: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Hello John• Replace John with your name

Page 10: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Getting Input• To get input you may use the read command

o cp hello_world hello_whoo vi hello_whoo Do the following changes

Page 11: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Read Options

Page 12: EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts

Assignments• Check the class OpenLab site for new Labs

o Work on the first script of Lab 6

• Check Blackboard for new Quizzes• Work on your Project