Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: shell scripts

  1. #1
    Join Date
    Apr 2011
    Beans
    8

    shell scripts

    hi guys i am a student,

    i need help with this script;

    .....

    SIZE=`du -ls`

    if [ $SIZE -k -gt 100 ];
    then echo "You have used more than 100k of disk space"
    else echo "You have not used more than 100k of disk space"
    fi

    .....

    im running it in the bourne shell, but i keep getting this error..
    [: 6: 1892: unexpected operator

    basically what i have to do is displaye a message about disk space if there is more than 100k free and if there is not..

    regards

    Stefan

  2. #2
    Join Date
    Nov 2008
    Location
    Earth
    Beans
    73
    Distro
    Ubuntu Development Release

    Re: shell scripts

    hi sreaney89,

    I think you use the wrong options/arguments
    I get this error when trying it in my shell.
    You can check it.

    Code:
    du: invalid option -- 'g'
    du: invalid option -- 't'
    Try `du --help' for more information.
    Last edited by safarin; April 12th, 2011 at 06:26 PM. Reason: typo error

  3. #3
    Join Date
    Apr 2011
    Beans
    8

    Re: shell scripts

    hmm strange, i dont seem to get that.. what i am trying to do however is check that, using a simple command, if there is 100k space free..

    Can you offer any help?

    note this is a script.. im running it from an executable file
    Last edited by sreaney89; April 12th, 2011 at 06:32 PM.

  4. #4
    Join Date
    Apr 2011
    Beans
    17

    Re: shell scripts

    Hmm... if you type "du -s", you end up with a "." a few characters away. Cut this out with sed or something. Try this:

    Code:
    #!/bin/sh
    SIZE=$(du -s | sed 's/\.//')
    if [ $SIZE -gt 100 ];then
    echo "You have used more than 100k of disk space"
    else 
    echo "You have not used more than 100k of disk space"
    fi

  5. #5
    Join Date
    Nov 2008
    Location
    Earth
    Beans
    73
    Distro
    Ubuntu Development Release

    Re: shell scripts

    Quote Originally Posted by sreaney89 View Post
    hmm strange, i dont seem to get that.. what i am trying to do however is check that, using a simple command, if there is 100k space free..

    Can you offer any help?

    note this is a script.. im running it from an executable file
    If I not mistaken..
    $SIZE -k -gt 100 is equal to

    Code:
    $ du -ls -k -gt 100
    when you type in the terminal, so this is how I get this error. Try

    Code:
    $ man du
    you can see the available option and argument there.

    I don't know what -k -gt 100 for.. maybe you could make it clear.
    I never use du command. First time I see it.. but I think you should compare the result ($SIZE) with that 100k of space.

    for this line.. I don't see any comparison.. $SIZE -k -gt 100

    P.S: I also just learned scripts myself.. so maybe I got wrong.. If I could help, I will help.

  6. #6
    Join Date
    Apr 2007
    Location
    Coffee corner
    Beans
    405
    Distro
    Kubuntu 11.04 Natty Narwhal

    Re: shell scripts

    is -gt the 'greater than' operation for the if clause? It seems to get interpreted as another option for du.
    Also, du doesn't display free space (that's df), only used space.

  7. #7
    hakermania's Avatar
    hakermania is offline Τώρα ξέρεις τι γράφω εδώ!
    Join Date
    Aug 2009
    Location
    Greece
    Beans
    1,705
    Distro
    Ubuntu Development Release

    Re: shell scripts

    Guys, the problem is that the output of du -ls has a dot in the corner, so, it is not a number, it is a string!
    Also, prefer $() intead of ``

  8. #8
    Join Date
    Nov 2008
    Location
    Earth
    Beans
    73
    Distro
    Ubuntu Development Release

    Re: shell scripts

    Ahhh.. It's has dot(.) at the end of the numbers. Hahaha...
    @rosencrantz : thank you rosencrantz for giving me info about -gt

  9. #9
    hakermania's Avatar
    hakermania is offline Τώρα ξέρεις τι γράφω εδώ!
    Join Date
    Aug 2009
    Location
    Greece
    Beans
    1,705
    Distro
    Ubuntu Development Release

    Re: shell scripts

    f1tz has given a solution for the dot, please try it

  10. #10
    Join Date
    Apr 2011
    Beans
    8

    Re: shell scripts

    yea its greater than for the if clause and yea i thought it was interpreted as a string myself.. didnt know how to fix it. Anyway that seems to work, need to test it a bit more, sorry for the confusion it was the used space that i needed
    Last edited by sreaney89; April 12th, 2011 at 07:05 PM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •