Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Script that sums the contents of a folder (help me) Post 302915503 by Don Cragun on Wednesday 3rd of September 2014 12:16:00 PM
Old 09-03-2014
There have been a couple of suggestions saying that you should use getopts to parse options (and I STRONGLY agree). Unfortunately, as far as I know, getopts in bash can't handle long options. Although it isn't often described in the man pages, getopts in many version of the Korn shell works well with both short and long options as long as there is a single letter equivalent for each long option. The following is a rewrite of your script using ksh instead of bash and using getopts instead of discrete option parsing. This doesn't make a lot of difference in your code since you only have one option that can be logically used at a time; but it does give you the ability to use -- as an option terminator in case you have a directory operand that starts with a -, and, if you add more options later, groups of short options that don't take option arguments can be combined into a single argument to your script (the same as allowed by standard utilities).

After parsing options this script will print the number of operands remaining, list them, and then process them as directory operands.

You haven't said what OS you're using, so this might not work on your system, but it is worth a try...
Code:
#!/bin/ksh
IAm=${0##*/}
usage() {
printf '
SYNOPSIS:
    %s [-d] directory...
    %s -h

DESCRIPTIN:
    For each directory operand given, estimate the amount of disk space
    used by the file hierarchy rooted in that directory, and count the
    number of regular files and directories in that hierarchy.

OPTIONS:
    -a, --subdir	Also list subdirectories of directory operands.
    -h, --help		Display this help notice.

OPERANDS:
    directory	The pathname of a direcotry to be processed.

EXIT STATUS:
    0	Successful completion.
    1	Unexpected options found.
    2	No operands found.
    3	One or more directory operands were not found.

' "$IAm" "$IAm"
}

# Parse options...
dswitch=0
err=0
while getopts -a "$IAm" "d(subdir)h(help)" opt
do	case $opt in
	(d)	dswitch=1;;
	(h)	usage
		exit 0;;
	(?)	err=1;;
	esac
done
# Shift away arguments used for options
shift $((OPTIND - 1))

# If no bad options were found, verify that we have at least one operand...
if [ $err -eq 0 ] && [ $# -lt 1 ]
then	printf '%s: No operands provided\n' "$IAm" >&2
	err=2
fi

# If we had bad optoins or do not have any operands, print help message as a
# diagnostic and exit.
if [ $err -gt 0 ]
then	usage >&2
	exit $err
fi

# Print operands remaining after option parsing...
printf '%s: %d operands remain after option parsing:\n' "$IAm" $#
printf '\t"%s"\n' "$@"
echo

# Process directory operands...
for dirname in "$@"
do	# Be sure directory named by an operand is present...
	if [ -d "$dirname" ]
	then	# The directory is present...
		printf '"CHECKING DIRECTORY: "%s"...\n' "$dirname"
		printf 'SIZE: '
		du -s "$dirname"
		printf 'REGULAR FILES: '
		find "$dirname" -type f | wc -l
		printf 'DIRECTORIES: '
		find "$dirname" -type d | wc -l

		# If -d (or --subdir) option was given, list subdirectories...
		if [ $dswitch -eq 1 ]
		then	find "$dirname" -type d
		fi
		echo
	else	# The directory is not present...
		printf 'Directory "%s" does not exist!\n\n' "$dirname" >&2

		# Set non-zero exit code for when we have finished processing
		# any remaining directory operands.
		err=2
	fi
done
exit $err

When invoked as:
Code:
sumcontent.sh --help
      or
sumcontent.sh -h

it produces the output:
Code:
SYNOPSIS:
    sumcontent.sh [-d] directory...
    sumcontent.sh -h

DESCRIPTIN:
    For each directory operand given, estimate the amount of disk space
    used by the file hierarchy rooted in that directory, and count the
    number of regular files and directories in that hierarchy.

OPTIONS:
    -a, --subdir	Also list subdirectories of directory operands.
    -h, --help		Display this help notice.

OPERANDS:
    directory	The pathname of a direcotry to be processed.

EXIT STATUS:
    0	Successful completion.
    1	Unexpected options found.
    2	No operands found.
    3	One or more directory operands were not found.

on standard output and exits with exit code 0.

When invoked as:
Code:
sumcontent.sh -d $HOME/dl .
      or
sumcontent.sh --subdir $HOME/dl .
      or
sumcontent.sh -d -- $HOME/dl .

on my system, it currently produces the output:
Code:
sumcontent.sh: 2 operands remain after option parsing:
	"/Users/dwc/dl"
	"."

"CHECKING DIRECTORY: "/Users/dwc/dl"...
SIZE: 158192	/Users/dwc/dl
REGULAR FILES:      239
DIRECTORIES:        3
/Users/dwc/dl
/Users/dwc/dl/pdf
/Users/dwc/dl/spreadsheets

"CHECKING DIRECTORY: "."...
SIZE: 16	.
REGULAR FILES:        3
DIRECTORIES:        1
.

And, if invoked as:
Code:
sumcontent.sh -subdir --hat $HOME/dl

it produces the output:
Code:
sumcontent.sh: -s: unknown option
sumcontent.sh: -u: unknown option
sumcontent.sh: -b: unknown option
sumcontent.sh: -i: unknown option
sumcontent.sh: -r: unknown option
sumcontent.sh: --hat: unknown option

followed by the help screen (all written to standard error output instead of to standard output) and exits with exit code 1.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Folder Contents

Hi, I'm trying to allow people to access the contents of a folder on a web site, I am automatically placing files in this folder for people to download. I'm using Apache on Mac OS X, if that makes a difference. Can anyone help with this? I've found no documentation on this so far... ... (6 Replies)
Discussion started by: spencer
6 Replies

2. UNIX for Dummies Questions & Answers

copy folder contents

I need to make a new dir in side the dir lab5 the new dir is called testLab5 without changing directories copy all files from your lab5 directory into your testLab5 directory then i have to without chaning directories and using exactly one command remove all files that start with the... (1 Reply)
Discussion started by: robsk8_99
1 Replies

3. UNIX for Dummies Questions & Answers

How to display contents of folder when 'cd' is used

Hi, I am a new learner of Unix. I am currently working on a Solaris 8 machine. Earlier, when I use 'cd <folder name>' command, I am not only able to change the folder but also able to see the contents of the folder as if a 'ls -lt' command was executed. However, since a week, suddenly this... (3 Replies)
Discussion started by: mumashankar
3 Replies

4. Shell Programming and Scripting

do a full comparison of folder contents in script

Hello everyone.... I have a small issue here at work and I am trying to script out a way to automate a fix for it. I have a small number of users (I work in a 1:1 with 6,000 macbooks) that aren't really managed in my deployment. They are managed with a few policies, but the policies are broken... (2 Replies)
Discussion started by: tlarkin
2 Replies

5. Shell Programming and Scripting

Compare folder contents over network

I use diff -r dir1 dir2 to get comparison of two folders that are on same machine. Now I need the same thing but one of the folders is on a different machine. Currently I ftp the folder to a temp folder compare using above command and delete the temp folder. Is there any other better options?... (5 Replies)
Discussion started by: ke3kelly
5 Replies

6. Shell Programming and Scripting

Script to create a folder with contents

I am working on HP Unix. Require a script for the below requirement. Requirement are: 1. Need to create a folder with files. 2. The folder should have a naming convention like - LRIC_ARCHIVE_ddmmyyhhmmss_version_nnn, the version number needs to be selected from an oracle table. 3. When the... (4 Replies)
Discussion started by: Roadies99
4 Replies

7. Shell Programming and Scripting

copy folder and its contents to another folder

Hi experts, I am coming to you with this basic question on copying a folder and its content from one location to another folder using PERL script. This is my requirement. I have a folder AB under /users/myhome I want to copy AB and its contents to /user/workspace. Finally it should... (1 Reply)
Discussion started by: amvarma77
1 Replies

8. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

9. Shell Programming and Scripting

Remove folder contents

for dir in BKP/*/ do echo You are in :$dir done O/P -- BKP/201448/ BKP/201449/ BKP/201450/ BKP/201451/ BKP/201452/ BKP/201501/ BKP/201502/ BKP/201503/ BKP/201504/ BKP/201505/ BKP/201506/ BKP/201507/ (3 Replies)
Discussion started by: rocking77
3 Replies

10. UNIX for Beginners Questions & Answers

Best way to move the contents of a folder to another one

what is the best way to move the contents of a folder to another one without deleting the structure of the first one. the contents could include subfolder too. both folder, the source-folder and the target-folder are on the same host. any idea is appreciated . (7 Replies)
Discussion started by: andy2000
7 Replies
All times are GMT -4. The time now is 10:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy