You are not logged in.

#1 2009-12-12 07:30:15

dalingrin
Member
Registered: 2009-03-18
Posts: 128

Bash script scope issue

I decided it was sad that I still didn't know BASH so I decided to make an AUR/ABS helper script. A redundant application, yes I know, but its
just for an exercise.
Everything was going well until I decided to store pkg names in an array. The array is global but given values within a function.
Unfortunately I can't seem to access the array outside of said function. Within the function that is assigning values at various indexes,
I can print out the array just fine. If I try to print the array anywhere else it is "blank."

I've cut out the irrelevant code to make for easier pasting. I'd appreciate any insight into the problem you can offer. smile

#BUILD DIRECTORY
BUILDDIR=~/build

#GLOBAL VARIABLES
PKGNAME="" 
PKGVERSION=""
PKGRELEASE=""
declare -a PKGUPGRADES  #The array in question
UPGRADEINDX=0

RESET=$(tput sgr0)
colorW="[1;37m" # white
colorR="[1;31m" # red

checkVersion () {
        
    TEMP="$(curl -s "http://aur.archlinux.org/rpc.php?type=info&arg=$PKGNAME" | awk -F '"' '{print $18}')"
    AURVERSION="$(echo $TEMP | awk -F "-" '{print $1}')"
    AURRELEASE="$(echo $TEMP | awk -F "-" '{print $2}')"

    if [ "$AURVERSION" \> "$PKGVERSION" ]; then
        UPGRADE="Upgrade Available"
        PKGUPGRADES[UPGRADEINDX]=$PKGNAME
        ((UPGRADEINDX++))    
    elif [[ "$AURVERSION" = "$PKGVERSION" && "$AURRELEASE" > "$PKGRELEASE" ]]; then
        UPGRADE="Upgrade Available"
        PKGUPGRADES[UPGRADEINDX]=$PKGNAME
        ((UPGRADEINDX++))
    else
        UPGRADE="Current"
    fi
        
    echo -e "$PKGNAME $PKGVERSION-$PKGRELEASE \e${colorR}$AURVERSION-$AURRELEASE \e${colorW}$UPGRADE${RESET}"

    #echo ${PKGUPGRADES[@]}  #echo works here
}


upgradeAUR() {
    echo -e "Checking AUR for package updates..."
    
    touch localList.txt
    pacman -Qm > $BUILDDIR/localList.txt

    sort $BUILDDIR/localList.txt | while read line; do
    PKGNAME="$(echo $line | awk -F " " '{print $1}')"
    PKGVERSION="$(echo $line | awk -F " " '{print $2}' | awk -F "-" '{print $1}')"
    PKGRELEASE="$(echo $line | awk -F " " '{print $2}' | awk -F "-" '{print $2}')"
    checkVersion $line
    done
    
    rm $BUILDDIR/localList.txt
    echo ${PKGUPGRADES[@]} #echo doesn't work here
}

upgradeAUR
echo ${PKGUPGRADES[@]} #echo doesn't work here

Offline

#2 2009-12-12 10:37:30

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Bash script scope issue

I believe functions are done in a sub-shell.  I'm pretty new to bash scripting myself so am not sure, but am pretty sure I've read that before, which would explain why your set variable inside the function is not recognized out of the function.

Off to the side tip: Though there is no rule that I know of, there is general guideline that variables that belong to the system are capitalized and newly assigned variables are lower-case.  I notice it does help with readability.

Last edited by Gen2ly (2009-12-12 10:40:00)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#3 2009-12-12 11:33:00

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash script scope issue

@Gen2ly: that is incorrect, something like this works:

$ funcA() { array[1]=a; array[2]=b; }
$ funcB() { funcA; }
$ funcB
$ echo ${array[@]}
a b

What is going wrong is that you have a subshell from a pipe in upgradeAUR in which you change the array. Try to rewrite it as

pacman -Qm | sort > localList.txt
while read line; do
...
...
checkVersion ...
done < localList.txt

Last edited by Procyon (2009-12-12 11:33:45)

Offline

#4 2009-12-12 12:58:20

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Bash script scope issue

Ooop :embarassed:

Btw, touching a file (i.e. creating a file) isn't necessary before redirecting output to a file, just 'command > file' will do.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#5 2009-12-13 06:22:03

dalingrin
Member
Registered: 2009-03-18
Posts: 128

Re: Bash script scope issue

Thanks Procyon, that fixed it.
Just this little bit of messing with BASH has really made me appreciate Python's clean syntax...

Offline

#6 2009-12-13 09:45:37

kaivalagi
Member
From: Norwich, UK
Registered: 2009-11-05
Posts: 145

Re: Bash script scope issue

dalingrin wrote:

Thanks Procyon, that fixed it.
Just this little bit of messing with BASH has really made me appreciate Python's clean syntax...

Python FTW!

If I ever have any scripting requirements that go beyond simple command line execs and checks I use python...to me it is easier to manage/update/adapt than a shell script and I also have a few functions I copy and paste here and there.


Running Arch 64 (Made the switch to Arch 10/2009)
AUR | BZR

Offline

Board footer

Powered by FluxBB