Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 22-11-2009, 11:26 AM   #1 (permalink)
Always Questioning?
 
emailaatif786's Avatar
 
Join Date: Feb 2006
Location: Srinagar
Posts: 354
Lightbulb Linux Shell Script that calculates the space occupied by files?


Linux Shell Script that calculates the space occupied by files?
Reply me wth a Linux Shell Script that calculates the space occupied by files?
There can be three levels:

Level 1: Write a script which takes in directory names as arguments and display total space occupied by files.

Level 2: If directory doesn’t exist, then display a warning message “Warning: Directory does not exist…”

Level 3: If directory doesn’t have access permissions then display message “Warning: Directory not accessible…”

Anyone please reply.
__________________
:smile: Love Digit
emailaatif786 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 02-12-2009, 05:05 PM   #2 (permalink)
In The Zone
 
Join Date: Mar 2004
Location: Welcome to the Matrix
Posts: 215
Default Re: Linux Shell Script that calculates the space occupied by files?

It will be a simple wrapper around "du". Why dont you start writing one and ask issues that you face, instead of asking for a readymade stuff.
__________________
----
enjoy is offline  
Old 02-12-2009, 06:14 PM   #3 (permalink)
Bond, Desi Bond!
 
desiibond's Avatar
 
Join Date: Oct 2007
Location: Bangalore
Posts: 10,062
Default Re: Linux Shell Script that calculates the space occupied by files?

du -ka <directory>

this command should give you size of directory and it's content.
when you pass the directory name as argument to the script, it will be stored in $0 environment vriable and you can use this in the script. all you need to write is a simple if loop.
__________________
My first blogspace: http://desiibond.blogspot.com
desiibond is offline  
Old 03-12-2009, 08:38 AM   #4 (permalink)
Always Questioning?
 
emailaatif786's Avatar
 
Join Date: Feb 2006
Location: Srinagar
Posts: 354
Smile Re: Linux Shell Script that calculates the space occupied by files?

Quote:
Originally Posted by desiibond View Post
du -ka <directory>

this command should give you size of directory and it's content.
when you pass the directory name as argument to the script, it will be stored in $0 environment vriable and you can use this in the script. all you need to write is a simple if loop.
A request,
Please write the 'If Loop' for me.

I do not know much of Linux. I am a basic user of Linux. I do not know accurately how to make shell script.
__________________
:smile: Love Digit
emailaatif786 is offline  
Old 25-12-2009, 01:51 AM   #5 (permalink)
Right Off the Assembly Line
 
Join Date: Dec 2006
Posts: 4
Default Re: Linux Shell Script that calculates the space occupied by files?

First of all IF is a conditional statement not a loop.

Evene im new to shell scripting but here you go!

if [ $FS = "TRUE" ]
then{
command 1 or Function_call;
command 2 or Function_call;
.
.
.
}
else{
command 3 or Function_call;
command 4 or Function_call;
}
fi

#where $FS is a variable and can hold any value depending on the script usage
azherdigit is offline  
Old 25-12-2009, 02:19 AM   #6 (permalink)
Simply a DIGITian
 
krishnandu.sarkar's Avatar
 
Join Date: Nov 2007
Location: Kolkata
Posts: 2,942
Default Re: Linux Shell Script that calculates the space occupied by files?

I think the syntax wud be

if [ $FS -eq "TRUE" ]
then
command1 / Func()
else
command2 / FUnc()
fi

I think the '{', '}' and ';' doesn't req and '-eq' in place of '='

According to desiibond ur script shud be

if [ -z $0 ]
then
echo "Enter a directory"
else
du -ka $0
fi

Try this. And correct me if I'm wrong.
__________________
  • Read The Forum RULES First.
  • Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
  • Before Starting A New Thread Read The STICKY THREADS First
  • Before Participating In Bazaar Section Read The BAZAAR RULES

Last edited by krishnandu.sarkar; 25-12-2009 at 02:25 AM.
krishnandu.sarkar is offline  
Old 25-12-2009, 02:57 AM   #7 (permalink)
Right Off the Assembly Line
 
Join Date: Dec 2006
Posts: 4
Default Re: Linux Shell Script that calculates the space occupied by files?

Thanks for correcting me. As i said even im new to shell scripting.

I included the blocks '{' '}' in case there are multiple commands to be executed in a IF statement. And you r right ';' is not required.

we can use = as well as -eq as long as it is in double quotes i guess.

here is a syntax for IF-then-else structure taken from a shell scripting book

In the
if-then statement, you only have one option of whether or not a command is successful.
If the command returns a non-zero exit status code, the bash shell just moves on to the next
command in the script. In this situation, it would be nice to be able to execute an alternate set
of commands. That’s exactly what the
if-then-else statement is for.
The
if-then-else statement provides another group of commands in the statement:

if
command

then
commands
else
commands
fi
If the command in the
if statement line returns with an exit status code of zero, the commands
listed in the
then section are executed, just as in a normal if-then statement. If the
command in the
if statement line returns a non-zero exit status code, the bash shell executes
the commands in the
else section.
Now you can modify the test script to look like this:

$ cat test4
#!/bin/bash
# testing the else section
testuser=badtest
if grep $testuser /etc/passwd
then
echo The files for user $testuser are:
ls -a /home/$testuser/.b*
else
echo "The user name $testuser doesn’t exist on this system"
fi
$ ./test4
The user name badtest doesn’t exist on this system
$

azherdigit is offline  
Old 25-12-2009, 10:45 AM   #8 (permalink)
Simply a DIGITian
 
krishnandu.sarkar's Avatar
 
Join Date: Nov 2007
Location: Kolkata
Posts: 2,942
Default Re: Linux Shell Script that calculates the space occupied by files?

^^Ya u r rite.....But a small mistake, we can use '=' in case of only string test but not others. As in the case of book "test string1=string2". And in case u need to execute multiple commands u don't need '{', '}'. The 'if' part is terminated then 'else' is found and 'else' part is terminated when 'fi' is found. So we don't need '{', '}' in shell scrips.
__________________
  • Read The Forum RULES First.
  • Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
  • Before Starting A New Thread Read The STICKY THREADS First
  • Before Participating In Bazaar Section Read The BAZAAR RULES
krishnandu.sarkar is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
One Giant Command Line MetalheadGautham Open Source 23 24-05-2008 02:31 AM
Overcome multimedia hurdles in Linux Cyrus_the_virus Open Source 2 04-12-2007 09:55 AM
MS Windows to Linux, Rise of Desktop Linux world!! mediator Technology News 87 24-09-2007 02:52 PM
Open Office crashes aditya.shevade Open Source 16 25-04-2007 01:26 AM
linux let otheres know what you know about linux snehal_prabhu Open Source 17 29-07-2005 12:03 AM

 
Latest Threads
- by Charan
- by Charan
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 03:12 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2