Changes

Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

51 bytes added, 12:00, 3 March 2021
INVESTIGATION 2: USING VARIABLES IN SHELL SCRIPTS
# Enter the following linux command to return to your Bash shell: <span style="color:blue;font-weight:bold;font-family:courier;">exit</span><br><br>Let's use some '''ENVIRONMENT variables''' in our Bash Shell script.<br><br>
# Use a text editor to edit the shell script called '''hello.bash'''<br><br>
# Add the following lines to the bottom of the file:<br><span style="font-family:courier;font-weight:bold;">echo<br>echo "The current directory location is: $PWD"<br>echo "The current user home directory is: $HOME<br>echo</span><br><br>
# Save your editing changes and exit your text editor.<br><br>
# Run your renamed shell script for confirmation by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./hello.bash</span><br><br>Take time to view the output and the values of the environment variables.<br><br>
# If you encounter errors, make corrections and '''re-run''' the checking script until you<br>receive a congratulations message, then you can proceed.<br><br>Let's create a Bash shell script that contain '''user-created variables'''.<br><br>
# Use a text editor to create a Bash shell script called '''user-variables.bash'''<br><br>
# Add the following lines to the beginning of this file:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>age=25<br>readonly age<br>read -p "Enter your Full Name" name<br>read -p "Enter your age (in years): " age<br>echo "Hello $name - You are $age years old"</span><br><br>
# Save your editing changes and exit your text editor.<br><br>
# Issue the '''chmod''' command to add execute permissions for the user for the '''user-variables.bash''' file.<br><br>
# Issue the following to run the user-variables.bash Bash shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">./user-variables.bash</span><br><br>What do you notice when you try to change the age variable? Why?<br><br>
# Use a text editor to create a file called '''parameters.bash'''<br><br>
# Add the following lines to the beginning of this file:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>echo \$0: $0<br>echo \$2: $2<br>echo \$3: $3<br><br>echo \$#: $#<br>echo \$*: $*<br><br>shift 2<br>echo \$#: $#<br>echo \$*: $*</span><br><br>
# Save your editing changes and exit your text editor.<br><br>Notice how the quoting character "'''\'''" is used to display positional parameters like "'''$2'''"<br>as opposed to the value stored in the <u>second</u> positional parameter.<br><br>
# Issue the '''chmod''' command to add execute permissions for the user for the '''parameters.bash''' file.<br><br>
13,420
edits

Navigation menu