Changes

Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

132 bytes added, 07:44, 19 July 2020
INVESTIGATION 2: USING VARIABLES IN SHELL SCRIPTS
# Run the '''hello.bash''' by just shell script name (i.e. to not use ./ prior to shell script name).<br><br>The shell script should run just by name.<br><br>
# Exit your Matrix session, and log back into your Matrix session.<br><br>
# Re-run the '''hello.bash ''' shell script by just using the name.<br><br>What did you notice?<br><br>The setting of the PATH variable only worked in the current session only. If exit the current Matrix session, then the recently changed settings for environment variables are lost. You will learned in Week 12 how to set environment variables in startup files.<br><br>
# Use a text editor to create a file called '''user-variables.bash'''<br><br>
# Add the following lines to the bottom of the file:<br><span style="font-family:courier;">#!/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 bottom of the file:<br><span style="font-family:courier;">#!/bin/bash<br>echo \$0: $0<br>echo \$2: $2<br>echo \$3: $3<br><br>echo \$#: $#<br>echo \$*: $*<br><br>echo shift 2<br>echo \$#: $#<br>echo \$*: $*</span><br><br>
# Save your editing changes and exit your text editor.<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;">./parameters.bash</span><br><br>What happened?<br><br>You shouldn't see any values for the parameters since you did NOT provide any arguments when running the shell script.<br><br>
# Issue the following to run the user-variables.bash Bash shell script with arguments:<br><span style="color:blue;font-weight:bold;font-family:courier;">./parameters.bash 1 2 3 4 5 6 7 8</span><br><br>What do you notice?
13,420
edits

Navigation menu