Changes

Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

1,150 bytes removed, 11:16, 3 September 2020
INVESTIGATION 1: CREATING A SHELL SCRIPT
# Issue the following linux command to run your shell script in your current directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">./hello</span><br><br>You should notice an error indicating you don't have permissions to run the file.<br>You need to first add execute permissions prior to running the shell script.<br><br>
# Issue the following linux command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x hello</span><br><br>
# Re-run your shell script: <span style="color:blue;font-weight:bold;font-family:courier;">./hello</span><br><br>Although your shell script should work, it is recommended to force your shell script to run in a specific shell. This helps prevent your shell script encountering errors when run in the incorrect shell (i.e. syntax not recognized in a specific shell).<br><br># '''Edit''' your '''hello''' shell script using a text editor.<br><br># Insert the following line at the '''beginning''' of the '''first''' line of your hello file:<br><span style="font-family:courier;">#!/bin/bash</span><br><br>This is referred to as a '''she-bang line'''. It forces the script to be run in the Bash Shell. When your Bash Shell script finishes execution, you are returned to your current shell that you are using (which in our case in Matrix, is still the Bash shell).<br><br># Save your editing changes and exit your text editor.<br><br># It is a good idea to rename your shell script to include an extension to indicate that the file is a Bash Shell script file. Issue the following linux command to rename your shell script file:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv hello hello.bash</span><br><br># Run your renamed shell script by issuing the following:<br><span style="color:blue;font-weight:bold;font-family:courier;">./hello.bash</span><br><br>
::In the next investigation, you will learn to create and run shell scripts that<br>use variables, positional and special parameters. You will also learn how to add a she-bang line at the top of a shell script to force it to run in a specified shell.<br><br>
=INVESTIGATION 2: USING VARIABLES IN SHELL SCRIPTS =
13,420
edits

Navigation menu