Exploring Bash

Witty Subtitle Goes Here

Mark Drago

Vice-President of LILUG

Outline

Prerequisites

Simple Command-Line Programs

STDIN and STDOUT

Pipes

Redirection

Advanced use of Pipes

Scripting Stuff

First Script

#!/bin/bash

#put a list of all threads into /tmp/all_procs
ps -eL > /tmp/all_procs

#filter out everything that has 'httpd' in it and put in /tmp/httpd_procs
cat /tmp/all_procs | grep httpd > /tmp/httpd_procs

#count all lines in /tmp/httpd_procs and store count in /tmp/num_httpd_procs
cat /tmp/httpd_procs | wc -l > /tmp/num_httpd_procs

#copy num_httpd_procs to desktop
cp /tmp/num_httpd_procs /home/mdrago

#remove temporary files
rm /tmp/num_httpd_procs
rm /tmp/httpd_procs
rm /tmp/all_procs

Picking Up the Pace - Variables

Picking Up the Pace - Loops

Picking Up the Pace - Conditionals

Useful File Tests

Putting It All Together

Resize Pictures v0.1

#!/bin/bash

dir="Thumbnails-$1"

mkdir -p $dir

for file in `ls *.jpg`; do
  convert "$file" -resize $1 $dir/$file
done

Using dialog to Resize Pictures

#!/bin/bash

tempfile='/tmp/dialog'

dialog --ok-label 'Resize' --inputbox 'Enter Desired Image Size' 8 30 '800x600' 2> $tempfile

exitstatus=$?

size=`cat $tempfile`

if [ $exitstatus -eq 0 ]; then
    dir="Thumbnails-$size"
    mkdir -p $dir

    for file in `ls *.jpg`; do
        convert "$file" -resize $size $dir/$file
    done
fi

Using zenity to Resize Pictures

#!/bin/bash

size=`zenity --entry --title='ResizePics' --text='Enter the desired size for th\e selected pictures.' --entry-text='800x600'`

dir="Thumbnails-$size"
mkdir -p $dir

for file in $@
do
    #get just the filename
    shortfile=`basename "$file"`

    #convert to smaller size and store in $dir
    convert -size $size "$file" -resize $size "$dir/$shortfile"

done

Further use of zenity

#!/bin/bash

size=`zenity --entry --title='ResizePics' --text='Enter the desired size for the selected pictures.' --entry-text='800x600'`

dir="Thumbnails-$size"

mkdir -p $dir

num_done=0
total=$#

(for file in $@
do

    #get just the filename
    shortfile=`basename "$file"`

    #convert to smaller size and store in $dir
    convert -size $size "$file" -resize $size "$dir/$shortfile"

    num_done=$(($num_done+1))

    percent=$(($num_done*100/total))

    echo $percent

done) | zenity --progress --title="ResizePics" --text="Resizing Pictures to $size"

The End