Writing Your First Shell Script: A Beginner's Guide
Learn how to write and execute shell scripts on Linux systems. Understand the basics of creating script files, making them executable, and specifying their location for easy access. Explore the use of key commands like echo and shebang (#!) to build a simple "Hello World" script. Discover best practices for script location and formatting to enhance script organization and readability.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
The Linux Command Line Chapter 24 Writing Your First Script Prepared by Dr. Reyes, New York City College of Technology
Shell Scripts What is a script? o A text file containing a set of commands o The file is read by the shell and the commands are executed as if they were typed How to write a shell script? 1. Write a script file using a text editor (e.g. vi/vim, gedit) 2. Make the script executable (assign appropriate permission) 3. Specify the location of the script so that the shell can find it
The echo command echo A command that displays a line of text into the console.
Hello World Script Our first script Script Features: o #! (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. o # - is a symbol used to specify comments o echo command used to display a line of text
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
Execute the Hello World Script First need to make the file executable. For that execute the following command o We will discuss file permissions in detail later in the course After chmod, execute the script as follows
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 span several lines, use the line-continuation sequences (backslash)