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


Example scripts


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 an example of a script that provides a user interface to call all scripts in the current directory

  1. #!/bin/bash
  2. while true; do
  3. echo
  4. echo "Select a CMD?"
  5. echo
  6. Files=("empty");
  7. Count=1
  8. for f in *.*;
  9. do
  10. if [ "${0:2}" != "$f" ] && [ "${f: -3}" == ".sh" ]; then
  11. echo ${0:2};
  12. echo $Count. $f;
  13. let "Count +=1";
  14. Files=("${Files[@]}" $f)
  15. fi
  16. done
  17. echo -n "Enter your choice, or 0 for exit: "
  18. read choice
  19. echo
  20. if [ "$choice" == "0" ]; then break; fi
  21. if [ "$choice" -ge "${#Files[@]}" ]; then echo "Invalid choice."; continue; fi
  22. echo Running ${Files[$choice]}...
  23. echo
  24. ./${Files[$choice]}
  25. done



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

  1. #!/bin/bash
  2. # specific conversion script to convert sh files to ./tmp/script
  3. LIST="$(ls *.sh)"
  4. for i in "$LIST"; do
  5. echo $i
  6. done
  7. if [ ! -d "./tmp" ]; then
  8. mkdir tmp;
  9. fi
  10. path=*.sh
  11. for f in $path;
  12. do
  13. cp "./$f" "./tmp/$f"
  14. done
  15. for f in ./tmp/*.sh;
  16. do
  17. echo -e "\n$(cat $f)" > $f
  18. echo -e "# ahayes@clemson.edu\n$(cat $f)" > $f
  19. echo -e "# Created by: Austen Hayes\n$(cat $f)" > $f
  20. done

Example script : filesUpdated.sh

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