Linux Shell Script that calculates the space occupied by files?

Discussion in 'Programming' started by emailaatif786, Nov 22, 2009.

Thread Status:
Not open for further replies.
  1. emailaatif786

    emailaatif786 Always Questioning?

    Joined:
    Feb 16, 2006
    Messages:
    351
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Srinagar
    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.
     
  2. enjoy

    enjoy New Member

    Joined:
    Mar 15, 2004
    Messages:
    226
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Welcome to the Matrix
    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.
     
  3. desiibond

    desiibond Bond, Desi Bond!

    Joined:
    Oct 9, 2007
    Messages:
    9,754
    Likes Received:
    9
    Trophy Points:
    0
    Location:
    Bangalore
    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.
     
  4. OP
    OP
    emailaatif786

    emailaatif786 Always Questioning?

    Joined:
    Feb 16, 2006
    Messages:
    351
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Srinagar
    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.
     
  5. azherdigit

    azherdigit New Member

    Joined:
    Dec 4, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  6. krishnandu.sarkar

    krishnandu.sarkar Simply a DIGITian

    Joined:
    Nov 13, 2007
    Messages:
    4,693
    Likes Received:
    15
    Trophy Points:
    38
    Location:
    Kolkata
    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.
     
    Last edited: Dec 25, 2009
  7. azherdigit

    azherdigit New Member

    Joined:
    Dec 4, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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
    $​

     
  8. krishnandu.sarkar

    krishnandu.sarkar Simply a DIGITian

    Joined:
    Nov 13, 2007
    Messages:
    4,693
    Likes Received:
    15
    Trophy Points:
    38
    Location:
    Kolkata
    ^^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.
     
Thread Status:
Not open for further replies.

Share This Page