Shellscripts with variables
Learning outcomes:
- Use a script to demonstrate the use of a positional parameter
- Show how to get user input for a script using read
- Explain the use of set with backticks
Would you like to download my PowerPoint to follow along?
- Shell Scripting with inputs and variables
- Variables
- Variables in Linux are untyped
- Any word (that isn't reserved) can be a variable
- Use $ to use variable
- Small script asking for name and then saying hello, try out the code below yourself!
- Example:
#!/bin/bash
Echo "What's your name?"
Read name
Echo "Hello $name"
- Used to pass info to a script when executed instead of using an input method such as "read"
- Numbered starting at 1 with some special parameters (such as $* and $? to get all or some of the contents of the temp variables)
- Example to put Jane as $1:
./testscript.sh Janeecho "hello $1" #should say hello Jane - Example to put Jane as $1 and Doe $2
./testscript.sh Jane Doeecho "hello $1 $2" # should say hello Jane Doe
- use set `date` (backticks not single quote) Puts the results in positional parameters, an example can be found here
- An example of the set command
- Set command in shell scripting
- Set - $VARIABLEā in bash
Suggested Activities and Discussion Topics:
- Lab assignment: PDF version (accessible HTML version)
- Watch this video and follow along to create a shell script.
- Activity: Listen to This Podcast That was created using AI from these materials. Transcript for the Podcast What are your thoughts? Did the AI do a good job representing the materials? Did you find any mistakes?
- Go through This AI generated study guide, what do you think? Did it capture the week materials well? How did you do on the self quiz? Do you know all the vocab used?
- Go through This AI generated briefing document, what do you think? Did it capture the week materials well? Was there anything it missed? Do you know all the vocab used?
Would you like to see some more classes? Click here