Download presentation
Presentation is loading. Please wait.
1
The Linux Command Line Chapter 24
Writing Your First Script Prepared by Dr. Reyes, New York City College of Technology
2
Shell Scripts What is a script? How to write a shell script?
A text file containing a set of commands The file is read by the shell and the commands are executed as if they were typed How to write a shell script? Write a script file using a text editor (e.g. vi/vim, gedit) Make the script executable (assign appropriate permission) Specify the location of the script so that the shell can find it
3
The echo command echo – A command that displays a line of text into the console.
4
Hello World Script Our first script Script Features:
#! (shebang) - is a sequence of characters that indicates the name of the interpreter to be used to execute the script. Used in the first line of a script. # - is a symbol used to specify comments echo – command used to display a line of text
5
Create the Hello World Script
Execute the following command to create a file named hello_world: vi hello_world Type this code in the hello_world file: #!/bin/bash # This is our first script. echo 'Hello World!‘ Close and save the file with the command: :wq
6
Execute the Hello World Script
First need to make the file executable. For that execute the following command We will discuss file permissions in detail later in the course After chmod, execute the script as follows
7
Scripts Location Scripts are usually located in the following directories ~/bin - for personal use /usr/local/bin – for anyone to use /usr/local/sbin – for administrators to use Custom directories added to the PATH variable Best Practices Use correct indentation techniques for multiple commands For commands that span several lines, use the line-continuation sequences (backslash)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.