[Top level Bash Page] .... [References]
Example: bex2.sh.
this is an example of a script that provides a user interface to call all scripts in the current directory
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
This script can be used to identify all files that have been modified within a specified amount of time.
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
----------------------------
filesUpdated.sh (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
Last update: 9/6/2017