emt 2390l lecture 8 dr. reyes reference: the linux command line, w.e. shotts
TRANSCRIPT
EMT 2390LLecture 8
Dr. ReyesReference: The Linux Command Line, W.E. Shotts
Outline• Shell Scripts• Hello World• Variables• Getting Input
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
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
Hello World• vi hello_world• Copy:
#!/bin/bash# This is our first script.echo 'Hello World!‘
• :wq• chmod 755 hello_world• ./hello_world
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)
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
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
Hello John• Replace John with your name
Getting Input• To get input you may use the read command
o cp hello_world hello_whoo vi hello_whoo Do the following changes
Read Options
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