Find the answer to your Linux question:
Results 1 to 7 of 7
Code: #!/bin/bash # Script to check disk usage of individual users on shared folders # declare -a xcpt declare -i x # Check for folders we are not interested in ...
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Posts
    3

    Array Problems

    Code:

    #!/bin/bash

    # Script to check disk usage of individual users on shared folders #

    declare -a xcpt
    declare -i x


    # Check for folders we are not interested in #
    x=0
    cat exceptions | while read line; do
    xcpt[x]=$line
    echo ${xcpt[x]}
    echo $x
    x=x+1

    done

    echo ${xcpt[0]}


    Hello guys, forgive me if I sound really dumb. I am very new to bash scripting (I have had experience with batch files in DOS). The object of the above script will eventually be to look at directory usage on our companies project shares, determine the user from the various folder names and send an email off to the administrators and relevent users when the disks are getting full. I have already completed the first stage of the script seperately. My problem is that I want to load a list of folders from a text file called "exceptions" into an array called xcpt. The echo commands you can see are purely for my benefit and wont be part of the script. When the file is read the 2 lines in my exceptions file are successfully read as confirmed by the echo command. Why, however, when I try to refer to a specific dimension of the array afterwards does it not return anything? As far as I can see there is nothing wrong with the syntax I have used. I have trawled the internet looking for similar problems and can't find anything. I've only just started the script but doesn't seem to be much point in continuing if I cant even refer to a variable which I KNOW has a value! I would really appreciate any advice on this. I'm desperately trying to learn bash (as part of an IT apprenticeship) but it seems to fight me all the way!!
    Last edited by morgz84; 09-25-2008 at 11:33 AM. Reason: Code tags screwing up my post

  2. #2
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    morgz84,

    I'm pretty new to bash scripting myself, but I've been learning from this excellent tutorial: Advanced Bash-Scripting Guide. If you haven't looked at it yet, you may want to check it out.

    Anyway, in lines 12 and 13, don't you need to refer to "x" as "$x"?

  3. #3
    Just Joined!
    Join Date
    Sep 2008
    Posts
    3
    lol thankyou for pointing out the $ missing from the x. Doesn't seem to solve the issue. Just seems that Im able to echo the values from the array during the loop then afterwards they're history. I have rewritten this script as I have found a far simpler approach. However I would still like to use arrays later on . BUt why oh why won't it work

  4. #4
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    Hey,

    Is your x value actually increasing? I just remembered that I actually had trouble with variable incrementation/decrementation in bash a while ago. It turns out that for some reason or another, I couldn't use the syntax var=$var+1. It looked like I had to use the syntax x=$(($x+1)) instead.

    Besides that... try echoing more than just xcpt[0] at the end. Try xcpt[1], 2, 3, 4, etc, and see if maybe there's a pattern that will help you debug. Maybe you're just off by one somewhere that we aren't noticing.

  5. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    It's because bash executes pipes in a subshell so the variables within the loop are out of the scope of the main program. Change the first line to use the korn shell and you'll find it'll work, because Korn handles pipes differently.

  6. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    See also http://www.linuxforums.org/forum/lin...tml#post450106 for an additional solution ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #7
    Just Joined!
    Join Date
    Sep 2008
    Posts
    3
    Quote Originally Posted by scm View Post
    It's because bash executes pipes in a subshell so the variables within the loop are out of the scope of the main program. Change the first line to use the korn shell and you'll find it'll work, because Korn handles pipes differently.
    Thanks, found that out my self after much painstaking research. Tried it just using conventional variables and was still getting he same issue. A lot of people seem to suggest using Korn shell but I still get the same problem. I have got round this issue by using a for loop instead without involving pipes. Would still like to sort this for future reference. .

Posting Permissions

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