Changes

Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

160 bytes added, 12:47, 5 March 2021
INVESTIGATION 2: USING VARIABLES IN SHELL SCRIPTS
# 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>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>[[Image:user-variable1.png|thumb|right|275px300px|Prompting user to enter data via the '''read -p''' command storing into '''user-created variable'''.]]
# 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 did you notice?<br><br>
# Use a text editor to '''modify''' your Bash shell script called '''user-variables.bash'''<br><br>
# '''Insert''' the following lines immediately <u>'''below'''</u> the '''she-bang''' line:<br><span style="font-family:courier;font-weight:bold;">age=25<br>readonly age</span><br><br>
# Save your editing changes and exit your text editor.<br><br>[[Image:user-variable2.png|thumb|right|275px300px|Trying to change the value of a '''read-only''' variable'''.]]
# 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>Let's use '''positional parameters''' and '''special parameters''' in a Bash shell script.<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>[[Image:parameter1.png|thumb|right|300px|Results from running shell script (with arguments) that use '''positional parameters''' and '''special parameters'''.]]
# 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>The values for some of the ''positional parameters'' and ''special parameters'' may NOT be<br>displayed properly since you did NOT provide any '''arguments''' when <u>running</u> your Bash 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?<br><br>Take some time to view the results and how the ''parameters'' have changed when using the '''shift''' command. <br>
13,420
edits

Navigation menu