02. Basic Scripting Concepts (Part 2)

Running a Shell Script

  • There are 2 options to run a shell script called myscript:
    • The preferred option: add execute permission, then type its name on the command line: myscript
    • Don’t need to add execute permission, and use it as an argument to bash (the shell): bash myscript
  • The first option is preferred because it’s shorter to type
  • However, if a script uses the programming constructs that are specific to the bash shell, and you happen to be running some other shell as your default shell, then option 1 will not work. You must use option 2

Define a bash Shell Script

  • As the author of a script that is written specifically for bash, you don’t want other users who run other shells to have problems running your bash script
  • Therefore you want to explicitly define your script as a bash script
  • To do this, the very first line of the script should be:
    • #!/bin/bash
    • Note that this line should be the very first line of your script, even before the beginning documentation block
  • Then when you type: myscript
    • The current shell will specifically uses the bash shell to interprets your script, not the default shell that is set up for the user
  • Explicitly defining the type of script you write enables everyone to run your script using option 1 (the faster option), no matter what shell they use
  • The #! is the token that tells the current shell which interpreter to use for your script
  • The path /bin/bash is dependent on where bash is located on your system. If you’re not sure, use whereis to find the path of the shell you want

Good Practice in Shell Scripting

  • You can use all the Linux utilities you know in your scripts, and you can use all shell metacharacters to pipe, tee, redirect, run a process in the background, group commands, etc.
  • However, path names need to be considered carefully. You are not guaranteed that your script will always run in the directory that you test it.
    • Therefore a safe practice is to use absolute path names in your script
    • A second option is to start from an absolute path name and use the cd command to get to a known directory before using a relative path
  • It is also common practice that a script does not leave behind temporary files that it creates. Remove these temporary files before exiting the script so you don’t create “garbage” in a directory

Basic User IO

  • To interact with the user there are 2 basic utilities:
    • echo for printing to screen
    • read for reading in from the keyboard
  • When read runs, it reads in one word at a time from the command line (space delimited), so you give it as many variables as the number of words you expect to read and store
  • Before reading input from the user, prompt the user so they know what input you need
  • To have the prompt and the user input be on the same command line, there are 2 ways:
    • Use the –n option of echo when printing the prompt, then use read to read in the input
      • Example: echo -n “first and last names: ”
      • read first last
    • Use the –p option of read, with the prompt enclosed in quotes
      • Example: read -p “first and last names: ” first last
    • Both examples reads 2 words that the user enters, the first word goes into a variable called first and the second word goes into the variable last