EMT 2390L Lecture 8 Dr. Reyes Reference: 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 editor 2.Make the script executable 3.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 script o # - is a comment o 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 directories o ~/bin - for personal use o /usr/local/bin – for anyone to use o /usr/local/sbin – for administrators to use o Custom directories added to the PATH variable Best Practices o Use correct indentation techniques for multiple commands o 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 string o 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 variables o Use ALL CAPS for constants To read the content of a variable use $ and the variable name o 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_who o vi hello_who o 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