Script that sums the contents of a folder (help me)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script that sums the contents of a folder (help me)
# 8  
Old 08-31-2014
Quote:
Originally Posted by junior-helper
You're welcome Smilie
Please check my posting again, so you can remove that second IF statement, it is unnecessary.
Enjoy! Smilie
thank you, so that second if statement is unneccessary only the one if statement is neccessary for the script
# 9  
Old 08-31-2014
Exactly! The first if statement already covers the second one.

---------- Post updated at 01:32 PM ---------- Previous update was at 01:29 PM ----------

First if statement says "no directory OR too many", so the second if statement, which only checks for "too many", never gets tested.
# 10  
Old 09-01-2014
Quote:
Originally Posted by junior-helper
Exactly! The first if statement already covers the second one.

---------- Post updated at 01:32 PM ---------- Previous update was at 01:29 PM ----------

First if statement says "no directory OR too many", so the second if statement, which only checks for "too many", never gets tested.

i'm sorry but i have a question, i tried the script and i have a problem when i give 2 paramters

this is what i have when i type
Code:
./test.sh “Desktop Documents”

Size: du: cannot access ‘Dekstop Documents’: No such file or directory
Files: find: ‘Dekstop Documents’: No such file or directory
0
Directories:find: ‘Desktop Documents’: No such file or directory
0


Last edited by Don Cragun; 09-02-2014 at 06:48 AM.. Reason: Add CODE tags.
# 11  
Old 09-01-2014
The script was built to accept 1 parameter only as that was the initial requirement. "Desktop Documents" is treated as 1 directory containing a whitespace.

Here is the updated script, which will accepts more than one parameter:
Code:
#!/bin/bash

if [ $# -lt 1 ]; then
 echo "no directory provided as parameter. minimum 1 directory required."
 exit 1
fi

for dirname in "$@"
do
 echo "CHECKING $dirname..."
 echo -n "SIZE: "
 du -s "$dirname"
 echo -n "FILES: "
 find "$dirname" -type f | wc -l
 echo -n "DIRECTORIES: "
 find "$dirname" -type d | wc -l
 echo
done

Now you can test with ./test.sh Desktop Documents
If you want to check Desktop, Documents and a folder called My Files, then test it with ./test.sh Desktop Documents "My Files"
# 12  
Old 09-02-2014
Quote:
Originally Posted by junior-helper
The script was built to accept 1 parameter only as that was the initial requirement. "Desktop Documents" is treated as 1 directory containing a whitespace.

Here is the updated script, which will accepts more than one parameter:
Code:
#!/bin/bash

if [ $# -lt 1 ]; then
 echo "no directory provided as parameter. minimum 1 directory required."
 exit 1
fi

for dirname in "$@"
do
 echo "CHECKING $dirname..."
 echo -n "SIZE: "
 du -s "$dirname"
 echo -n "FILES: "
 find "$dirname" -type f | wc -l
 echo -n "DIRECTORIES: "
 find "$dirname" -type d | wc -l
 echo
done

Now you can test with ./test.sh Desktop Documents
If you want to check Desktop, Documents and a folder called My Files, then test it with ./test.sh Desktop Documents "My Files"

Thank you very much

I'm sorry to bother you but i have also a question
Now i want to implement parameters in my script for example the help function i have this some code for the help function but there is something wrong

Code:
# Check paramaters
if [ -z "$1" ] ; then 
	echo $Help
	exit
fi


# Arguments
while [[ -n "$@" ]]
do
case "$1" in
-h|--help)
		# Help
		echo "This is the help function"
		echo $Help
		exit

then i want also a parameter -d that shows all subfolders of the specified folder (s) it in the list.
can you help me with this parameter ?

Thank you very much to help me Smilie

Last edited by Roggy; 09-02-2014 at 06:53 AM.. Reason: Add CODE tags.
# 13  
Old 09-02-2014
The more I look at this thread, the more it sounds like homework. Are you asking for help solving a homework problem?
# 14  
Old 09-02-2014
Quote:
Originally Posted by Don Cragun
The more I look at this thread, the more it sounds like homework. Are you asking for help solving a homework problem?
No it just for my own , i'm learning bash scripting and i found some exercises i'm not very good i writting a script because i'm just learning it and asking some peoples helps me to write a script , i have already written a script and thats script is that i'm looking for files en this one i want to do is looking for folders and directories
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question