[Top level Bash Page] .... [References]


Example scripts  (bash, awk, sed, cut)


Example:  bex1.sh:   A simple script that gives information about a file.  To run the script  :    ./bex1.sh filename      

#!/bin/bash

# This script gives information about a file.

FILENAME="$1"

  1. if [ -z "$FILENAME" ] ; then
  2.   echo "Usage: ./bex1.sh [filename]"
  3.   exit 0
  4. fi
  5. if [ -e $FILENAME ] ; then
  6.   echo "Properties for $FILENAME:"
  7.   echo "Size is $(ls -lh $FILENAME | awk '{ print $5 }')"
  8.   echo "Type is $(file $FILENAME | cut -d":" -f2 -)"
  9.   echo "Inode number is $(ls -i $FILENAME | cut -d" " -f1 -)"
  10.   echo "$(df -h $FILENAME | grep -v Mounted | awk '{ print "On",$1", \
  11. which is mounted as the",$6,"partition."}')"
  12. else
  13.   echo "File does not exist."
  14. fi

Discussion:




Example:   bex2.sh :  this is a simple start to a top level script you can use to call other scripts.


Example:  bex3.sh  : this is a simple example of looping through the set of files in the current directory.   It copies the *.sh files in the cd to the subdirectory ./tmp


Example:   awk   


Example:  sed   

 

Example:  cut   

 

Example script : filesUpdated.sh

 

Here are two different solutions to the following scripting assignment given ina prior semester

Create a bash script that identifies all files that have been modified within a specified amount of time.

HINT: the find command with the –mtime option will provide the data, your script should then process and present the data as required.

Syntax: filesUpdated.sh param1 param2 param3

Example output for script mode 1 :
Owner Number of files changes
jjm 3
root 1
---------------

Example output for mode 2

jjm
----------
w------- 1 jjm jjm 1998 Feb 8 20:30 plotPDF.m
-rw-rw-r-- 1 jjm jjm 45000 Feb 8 20:29 ex2.1
-rw-rw-r-- 1 jjm jjm 45000 Feb 8 20:29 ex2.2

Root
----------
w-r--r-- 1 root root 3798 Feb 5 20:37 boot.log
----------------------------

Solution 1 : (credit to Anna Kutch - CPSC4240 Spring 2015)

#!/bin/bash
# Anna Kutch (akutch@g.clemson.edu)
# CPSC 4240 HW2 Script

# init variables
startDir=$1
amtTime=$2
mode=$3
count=0

# make sure there's a correct number of arguments
if (( $# < $"3" )); then
echo "Usage: [starting dir] [amt time] [script mode]"
exit 0
fi

# OUTPUT will be a list in the form:
# <count of files> <user>
#
# find $startDir -mtime -$amtTime -ls : find the files modified within $amtTime
# -type f : it is a file (not a directory)
# awk '{print $5}' : get the user name field only
# sort -r : sort in descending order
# uniq -c : print the count for unique user names
# awk '{printf ("%s\t%s\n", $2, $1)}' : print the name followed by the number

OUTPUT="$(find $startDir -mtime -$amtTime -type f -ls | awk '{print $5}' | sort -r | uniq -c | awk '{printf ("%s\t%s\n", $2, $1)}')"

# for script mode 1 (summary mode):
if(($mode==1));
then
printf "%-20s\tNumber of files changed\n" "Owner"
for i in $OUTPUT; do

# print the number of modified files
if(($count%2));
then
# count%2 = 1 -- file count
printf "$i\n"

# print the user name
else
# count%2 = 0 -- user name
printf "%-20s\t" $i
fi
let "count++"
done
fi

# for script mode 2 (verbose mode):
if(($mode==2));
then

# VOUTPUT will be a list of filenames
#
# find $startDir -mtime -$amtTime -ls : find the files modified within $amtTime
# -type f : it is a file (not a directory)
# awk '{print $11}' : print the file name

VOUTPUT="$(find $startDir -mtime -$amtTime -type f -ls | awk '{print $11}')";

# for each unique person...
for i in $OUTPUT; do
# print the files associated with the user
if(($count%2)); then
# only print the files for the specified user
echo "$(ls -lt $VOUTPUT | grep $username)"

# print and set the user name
else
# count%2 = 0 -- user name
printf "\n%-20s\n" $i
username="$i"
fi
let "count++"
done
fi



Solution 2: (credit to Cullum Smith - CPSC4240 Spring 2015)

#!/bin/bash

# Cullum Smith (gcsmith@clemson.edu)
# CPSC 624 - Spring 2015 - Dr. Martin
# Homework 2 - filesUpdated.sh

usage () {
echo "usage: filesUpdated.sh [directory] [daysAgo] [mode]"
}

INTEGER_REGEXP='^[1-9][0-9]*$'
MODE_REGEXP='^(1|2)$'

# ensure 3 args were passed
if (( $# != 3 )) ; then
echo "[ERROR] missing operand"
usage
exit -1
fi

directory="$1"
daysAgo="$2"
mode="$3"

# ensure daysAgo is a positive integer
if ! [[ "$daysAgo" =~ $INTEGER_REGEXP ]] ; then
echo "[ERROR] daysAgo must be a positive integer"
usage
exit -1
fi

# ensure mode is either 1 or 2
if ! [[ "$mode" =~ $MODE_REGEXP ]] ; then
echo "[ERROR] mode must be either 1 or 2"
usage
exit -1
fi

# associative array mapping usernames to file counts
declare -A fileCount

# get file count in $directory for each system user
while read -r user ; do
count=$(find "$directory" -type f -mtime "-$daysAgo" -user "$user" -print0 | tr -d -c '\0' | wc -c)
if (( "$count" > 0 )) ; then
fileCount["$user"]="$count"
fi
done <<< "$(awk -F':' '{ print $1 }' /etc/passwd)"

case $mode in
1) # summary mode
printf "Owner Files changed in past %d days\n" "$daysAgo"
for user in "${!fileCount[@]}" ; do
printf "%-10s %d\n" "$user" "${fileCount["$user"]}"
done | sort -rn -k2
;;

2) # verbose mode
while read -r user ; do
echo $user
find "$directory" -type f -mtime "-$daysAgo" -user "$user" -exec ls -lt {} \;
echo
done <<< "$(for user in "${!fileCount[@]}" ; do echo "$user" "${fileCount["$user"]}" ; done | sort -rn -k2 | awk '{ print $1 }')"
;;
esac



Last update: 2/2/2017