Shell Script Need Help

Discussion in 'Programming/Scripts' started by mphayesuk, Jun 25, 2006.

  1. mphayesuk

    mphayesuk New Member

    Ok what I want to do is get the size of a file and then assign that size to a variable.

    This is what I have so far:

    echo "total size of file"
    ls -l -s /tmp/systembackups/$Today/$Today/systemfiles.tar.gz | cut -d \ -f 7

    the $Today variable is the date that the backup script was run, so every night when the script runs it tar's everything up and then burns to a dvd, what I also have is all the output from my script goes into a log file, so at the end of the log file I want to put the file sizes of the backup.

    With the example above I have tried to assign it to a variable and get nothing, when I try and run it as an echo statement it wont recognise the $Today variable.

    Can anyone help with this.

  2. falko

    falko Super Moderator ISPConfig Developer

    So your problem is that the $Today variable is empty, and not that you cannot assign the size of a file to a variable?

    Where does the $Today variable come from?
  3. mphayesuk

    mphayesuk New Member

    The Today variable contains the date, which is caluculated at the start of the script, so it does contain data. The way the I have found to get the file size into a variable is to echo the whole statment and then assign a variable to the echo statement.

    That will work as long as I hard code the folder locations (ie no variables) which is no good to me as the Today string will change every day.

    So if I do:
    FILESIZE$= 'echo 'ls -l -s /tmp/systembackups/26.06.06/26.06.06-backup/systemfiles.tar.gz | cut -d \ -f 7'

    the filesize string will contain the information I need but if I put in variables instead of the date it will fail, because the command line will not know what $Today is set to.

    Am I making sense.... its starting to pee me off a little.

    Any ideas.

  4. vlsimpson

    vlsimpson New Member

    I'm going to assume that directory structure is in place and the 26.06.06 is day.month.year.

    Here's a variation with stat:
    FILESIZE=$(stat -c %s /tmp/systembackups/$(date +%d.%m.%y)/$(date +%d.%m.%y)-backup/systemfiles.tar.gz)

    This should work iff I think correctly what you're trying to do. :)
    docs for stat and date:
    info stat
    info date
  5. falko

    falko Super Moderator ISPConfig Developer

    What about
    FILESIZE=`ls -l -s /tmp/systembackups/${Today}/${Today}/systemfiles.tar.gz | cut -d \ -f 7`

Share This Page