hydeparkk
hydeparkk

Reputation: 62

Wrong arguments count in next function calls

I've got function ShowJobHistory, and it's called from another. At first call of this function everything works fine, it count proper number of arguments, parse them as I want to. But in next calls, even if I specify a multiple arguments, this function treat them as one, and it looks ( jb.RJBGID=12871 12873 12868 ) after parsing them. What's wrong with my function?

ShowJobHistory () {
conditions=
argCount=$#
if [[ $argCount -ne 0 ]] then
    if [[ $1 == [iI] && $argCount -eq 3 ]] then
        if [[ $2 -lt $3 ]] then
            conditions="( jb.RJBGID between $2 and $3 )"
        else
            conditions="( jb.RJBGID between $3 and $2 )"
        fi
    else
        conditions="("
        for nr in $@
        do
            conditions="${conditions} jb.RJBGID=${nr} or "
        done
        conditions=${conditions%or }
        conditions=$conditions")"
    fi

    typeset query

Below function calls ShowJobHistory.

ShowJobHistoryMenu () {
typeset jobID
save=
echo "Enter jobIDs" 
read jobID?"Enter jobID: "  
while [[ $save != [nNyY] ]]
do
    read save?"Save output to file? [y/n]"
done
if [[ save = [yY] ]] then
    ShowJobHistory $jobID | tee $TRACEDIR/output.txt
else
    ShowJobHistory $jobID
fi
}

Upvotes: 1

Views: 463

Answers (1)

askmish
askmish

Reputation: 6684

set IFS=" " in your shell script and check if the problem is fixed.

Else try this workaround:

for nr in `echo $@` [[ Similar to: for nr in $@ ]]
do
   conditions="${conditions} jb.RJBGID=${nr} or "
done

Else this:

set -A array $@
for nr in `echo ${array[@]}` [[ Similar to: for nr in ${array[@]} ]]
do
  conditions="${conditions} jb.RJBGID=${nr} or "
done

To get the total no. of elements in the array you can use: echo ${#array[@]} And remember to unset array before using the array again(Although set -A array will do that every time its called, just to be safer).

Try all the solutions given above, let me know if there's still some unresolved problems.

Upvotes: 1

Related Questions