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"
  • Positional parameters
    • 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 Jane
      echo "hello $1" #should say hello Jane
    • Example to put Jane as $1 and Doe $2
                          ./testscript.sh Jane Doe
      echo "hello $1 $2" # should say hello Jane Doe
  • Positional parameters and backticks

Suggested Activities and Discussion Topics:

  • Watch this video and follow along to create a shell script.

Would you like to see some more classes? Click here