==== Meeting (Bash Scripting) ==== ** Steve Jones 2018-08-26**\\ I ran this script as a presentation for this meeting - it's about bash, in bash, named Bashy. You can download it by clicking on the name at the top of the code window below. #!/bin/bash -i # This script shows some of the features of the Bourne Again SHell # or bash. # It is designed to be run from a terminal as a presentation, # # # Steve Jones, for the 2008-04-26 and 2018-08-26meeting, CLUG.org # Updates and information will be made available as quickly # as time permits. # Set the values for $LINES and $COLUMNS eval $(resize) # This runs a sub-shell, the output of which # is evaluated by the current shell. # Test to see if we were given a starting slide on # the command line if [ "$1x" = "x" ] # The special variable $1 is the first then Slide=1 # argument on the command line, $2 is else # the second, etc... Here we check to Slide=$1 # see if a slide number was given. fi # Wait for a keypress and then continue on with the script. Wait() { # This is a function, it is delimited by the curly brackets # above and below. It stops at the current cursor location # and silently waits for a keypress read -s -n 1 Anykey } # Format stdin to fit nicely on the screen. Format() { # Another function, this one is handed input on stdin, it then # breaks it into nice, screen width-or-less lines. fmt --split-only --width=$COLUMNS } # Pause for a second Sleep() { read -s -n 1 -t 1 Anykey } # Wait for a keypress at the bottom of the screen then clear # the screen before continuing on with the script. Pause() { echo -en "\033[$LINES;5H" # Read (-s, Silently) (-n 1, Single Char) (-p, "Prompt") Variable read -s -n 1 -p "$Slide " Anykey } # Set the screen for black text on a grey background. CodeOn() { echo -e "\033[47m\033[30m" } # Set the screen back to black on white. CodeOff() { echo -e "\033[0m" } # Put a title on each page and center it on the top line Title() { clear EightySpaces=" " Spaces="${EightySpaces:0:$(( ($COLUMNS/2)-$(( ${#1}/2 )) ))}" echo "$Spaces"$1 # Okay, this needs an explanation. The EightySpaces variable is # just what it says, eighty space characters. The next line # assigns a substring of $EightySpaces, starting at the first # character (0, or leftmost), and continuing for half of the # screen width minus half of the length of the title. The # title is handed to the function at the time of invocation. } S-01() { Title "What is Bash?" # Our first slide, it is a function that contains a number of # functions. First is the one above, which calls the title # function to make things pretty, then the format function below # to make the rest prettier. cat <\$LOGFILE echo "This statement is appended to \\"\$LOGFILE\\"." >>\$LOGFILE echo "This statement is also appended to \\"\$LOGFILE\\"." >>\$LOGFILE echo "This statement is echoed to stdout, not \\"\$LOGFILE\\"." # cp with more characters, this will create xyzzy.txt if it does # not exist, otherwise it appends to the end of the file. cat < Primes.txt >> xyzzy.txt find /etc -type f -perm -o=rw find: /etc/cups/ssl: Permission denied find: /etc/ssl/private: Permission denied find: /etc/asterisk: Permission denied find /etc -type f -perm -o=rw 2>/dev/null EOF CodeOff Pause } S-17() { Title "Arithmetic" cat <