Open main menu

CDOT Wiki β

Changes

Tutorial10: Shell Scripting - Part 1

4 bytes added, 09:43, 11 March 2021
INVESTIGATION 2: SHE-BANG LINE / VARIABLES / PARAMETERS
# Add the following line to the <u>bottom</u> of the file ('''copy''' and '''paste''' to prevent ''errors''):<br><span style="font-family:courier;font-weight:bold;">echo "The current shell you are using is: $(ps -o cmd= -p $$|cut -d' ' -f1)"</span><br><br>'''FYI:''' This command displays the '''name''' of the ''shell'' that the shell script is running in.<br>The command within <span style="font-family:courier;font-weight:bold;">$( )</span> uses a technique known as '''command substitution'''.<br><br>
# Issue the following Linux command to change to the '''Bourne Shell''':<br><span style="color:blue;font-weight:bold;font-family:courier;">sh</span><br><br>You should notice your shell prompt changed that indicates that you are in a different shell.<br><br>[[Image:hello2.png|thumb|right|275px|Changing the Bourne shell and running shell script '''without''' a '''She-bang''' line.]]
# Issue the following Linux command to run your shell script in the ''Bourne Shell'':<br><span style="color:blue;font-weight:bold;font-family:courier;">./hello</span><br><br>You should see that you are currently running the shell script "'''sh'''" that <br>which represents the '''Bourne shell'''.<br><br>'''NOTE:''' Due to the fact that shells (and their features) have '''evolved''' over a period of time,<br>an error may occur if you include a '''NEWER shell feature''' (e.g. ''Bash Shell'') but run it in an '''OLDER shell''' (For example: the ''Bourne Shell'').<br><br>[[Image:she-bang-1.png|thumb|right|275px|Adding a '''she-bang line''' at the BEGINNING of the first line in you shell script forces the shell script to be run in that specific shell (in this case, the Bash shell).]]You can add a '''special comment''' called a '''she-bang line''' at the BEGINNING of the <u>FIRST line</u> of your shell script to<br>'''force''' it to run in the shell you want (for example: the Bash 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;font-weight:bold;">#!/bin/bash</span><br><br>This is referred to as a '''she-bang line'''. It forces this script to be run in the '''Bash Shell'''.<br>When your Bash Shell script finishes execution, you are returned to your current shell that you are using<br>(which in our case in Matrix, is still the Bash shell).<br><br>[[Image:hello3.png|thumb|right|275px|Changing the Bourne shell and running shell script '''with''' a '''She-bang''' line (forcing script to run in the '''Bash''' shell).]]
13,420
edits