Difference between revisions of "Shell Scripting"

From CDOT Wiki
Jump to: navigation, search
(Tips to Speed up Scripting)
 
Line 18: Line 18:
 
== Tips to Speed up Scripting ==
 
== Tips to Speed up Scripting ==
  
* Labs are designed so students can copy and paste scripting contents to their c6host or virtual machines (except for centos3 which is text-based OS)
+
* Labs are designed so students can copy and paste scripting contents to their c7host or virtual machines (except for centos3 which is text-based OS)
 
* You are required to add execute permissions (at least for owner) in order to run the created shell script
 
* You are required to add execute permissions (at least for owner) in order to run the created shell script
 
* When retrieving other shell scripts via the wget command, set execute permissions and then run the shell script from the current directory
 
* When retrieving other shell scripts via the wget command, set execute permissions and then run the shell script from the current directory

Latest revision as of 12:12, 19 January 2016

Purpose of Shell Scripting

You may have learned about creating and running Bash Shell Scripts in your ULI101 course. Shell scripts help Linux users and system administrators to automate repetitive tasks to become more efficient and to help them save time. You will be reviewing and building a basic Bash Shell script to generate information reports for your newly-installed Linux host machine.


Online Scripting Resources

If you require additional practice in creating shell scripts and using the vi text editor, run the commands in your Matrix account:

/home/murray.saul/vi-tutorial
/home/murray.saul/scripting-1


Lab 1 Scripting Content


  • She-bang line: #!/bin/bash

    Shell scripts have evolved of the past 40 years. To avoid running a newer shell script on an older shell, it is recommended to force running the shell script in the correct shell. In order to do this, on the first line at the very beginning of the shell script, you add the #! (# as in "shhhh" - a comment, and ! is referred to as "bang" run a commmand: in this case, run the command: /bin/bash). You can issue the Linux command which bash to get the correct location. If there is no bash shell on that machine, the shell script will not run (as a precaution - the Linux admin will know how to make a fix to the shell script if required).

  • Variables:

    There are 3 types of variables that can be used in shell scripting: ENVIRONMENT (eg. $USER), user-defined ($varName), and positional parameters (eg. $1, $2... containing arguments after shell script or by using set command (eg. set $(ls) ). Using dollar sign ($) in front of variable expands the value assigned.

  • Command Substitution:

    Examples

    file $(ls)
    set $(ls);echo $#;echo $*
    echo "hostname: $(hostname)"

  • Logic Control Flow Statements:

    The test command can be used to see if a condition is true or false
    (i.e. test $USER = "root") . The $? special shell variable stores the result (zero if true, non-zero if false). Square brackets [ ] can be used to represent the test command with the condition inside the brackets (spaces separating brackets).Can use if / if-else / if-elif-else statements with brackets. The exit command can be used to terminate the shell script with a false value.

    Examples

    if [ $USER = "root" ]
    then
     echo "You must be root" >&2
     exit1
    fi

    # For number comparison: use:
    # -gt,-ge, -lt, -le, -eq, -ne

    if [ $age -gt 65 ]
    then
     echo "retire"
    else
     echo "don't retire"
    fi

    if [ $grade -gt 79 ]
    then
     echo "You get Good Mark"
    elif [ $grade -gt 49 ]
    then
     echo "You pass"
    else
     echo "You fail"
    fi


Tips to Speed up Scripting

  • Labs are designed so students can copy and paste scripting contents to their c7host or virtual machines (except for centos3 which is text-based OS)
  • You are required to add execute permissions (at least for owner) in order to run the created shell script
  • When retrieving other shell scripts via the wget command, set execute permissions and then run the shell script from the current directory