Changes

Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

349 bytes added, 07:49, 18 July 2020
INVESTIGATION 1: CREATING A SHELL SCRIPT
# Issue a command to '''confirm''' you are located in your home directory.<br><br>We want to create a Bash Shell script to welcome the user by their username. Let's first provide some useful tips in terms<br>of selecting an appropriate name for the shell script.<br><br>
# Issue the following linux command to check if your intended shell script name<br>is already exists to be run automatically from the Bash shell:<br><span style="color:blue;font-weight:bold;font-family:courier;">which hello</span><br><br>You should notice that there is no output and therefore, this shell script name can be used.<br>On the other hand, if you wanted to create a file called sed, then the '''which sed''' command<br>would indicate it is already being used by the shell and that '''sed''' wouldn't be an appropriate shell script name to use.<br><br>
# Use a text editor like vi or nano to create the text file called hello (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi hello</span>)<br><br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br>
# Enter the following two lines in your shell script, replacing "your-username" with your actual name:<br><span style="font-family:courier;">clear<br>echo "Hello your-username"</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<br><br>
# 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>You Although your shell script should now see work, it is recommended to force your screen clear and displays shell script to run in a greeting to you 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 your usernamea text editor.<br><br>
# x
13,420
edits

Navigation menu