ERROR: `(' is not expected.

Hi All,

I have written a shell script which works all right on bash shell, but when it comes to execute it using ksh on AIX it gives the following error::frowning:

    bash$ /bin/ksh getShortInfo.sh 
    getShortInfo.sh[10]: syntax error at line 26 : `(' unexpected

Could you please indicate what is wrong? In addition, if you could indicate how can I make it portable on bash/ksh/csh would be really appreciated.

Please you can find below the guilty part of the script:

#!/bin/sh

COMMANDS=""
file_name='systemInfo.txt'
OS_TYPE=`uname`


getCommandsSpecificOS(){
    AIX_COMMANDS[0]="hostname"

    SunOS_COMMANDS[1]="uname -a"
    SunOS_COMMANDS[2]="ulimit -a"

    Linux_COMMANDS[0]="hostname"
    Linux_COMMANDS[3]="cat /proc/cpuinfo"
    Linux_COMMANDS[4]="cat /proc/meminfo"

    if   [ $1 = "AIX" ]; then 
        COMMANDS=("${AIX_COMMANDS[@]}")
    elif [ $1 = "SunOS" ]; then 
        COMMANDS=("${SunOS_COMMANDS[@]}")
    elif [ $1 = "Linux" ]; then 
        COMMANDS=("${Linux_COMMANDS[@]}")

    fi
}

###################################################################################################

getCommandsSpecificOS $OS_TYPE

Thanks in advance indeed,
Regards,
Elvis
[/COLOR]

Remove the parens around:

COMMANDS=("${AIX_COMMANDS[@]}")
1 Like

BASH has arrays.

Most other shells don't.

If you want this to work in shells other than your favorite, you'll want to rewrite it without arrays.

We can help with that, if you could illustrate what you're trying to do.

1 Like

Hi All,

thank you very much in advance for your support.:slight_smile:

Basically i am trying to get platform' specific information through the use of the script.

The platform could be: SunOS, AIX or RHEL.

The script does not take any argument in input. It visualise on the screen the success or not of the platform' specific commands which are executed for gathering platform specific information.

The script create, in the directortory where it is launched, the file "systemInfo.txt" which contains all platform' specific information.

The script gives also the opportunity to execute further commands
based on the wishes of the user.

Thanks again for your help.

Please, below you can find the script .

Elvis

#!/bin/sh
  
COMMANDS=""
OS_TYPE='unknown'
file_name='systemInfo.txt'
OS_TYPE=`uname`


getCommandsSpecificOS(){
    AIX_COMMANDS[0]="hostname"
    AIX_COMMANDS[1]="uname -a"
    AIX_COMMANDS[2]="ulimit -a"
    AIX_COMMANDS[3]="smtctl"
    AIX_COMMANDS[4]="swap -l"
    AIX_COMMANDS[5]="/etc/prtconf"
    AIX_COMMANDS[6]="oslevel -s"
    AIX_COMMANDS[7]="lparstat -i"
    AIX_COMMANDS[8]="lscfg -vp | grep -ip proc | grep "PROC""
    AIX_COMMANDS[9]="mpstat"

    SunOS_COMMANDS[0]="hostname"
    SunOS_COMMANDS[1]="uname -a"
    SunOS_COMMANDS[2]="ulimit -a"
    SunOS_COMMANDS[3]='/usr/platform/`uname -i`/sbin/prtdiag'
    SunOS_COMMANDS[4]="/usr/sbin/psrinfo -pv"
    SunOS_COMMANDS[5]="/etc/prtconf"
    SunOS_COMMANDS[6]="cat /etc/release"
    SunOS_COMMANDS[7]="swap -l"
    SunOS_COMMANDS[8]="zoneadm list -vi"



    Linux_COMMANDS[0]="hostname"
    Linux_COMMANDS[1]="uname -a"
    Linux_COMMANDS[2]="ulimit -a"
    Linux_COMMANDS[3]="cat /proc/cpuinfo"
    Linux_COMMANDS[4]="cat /proc/meminfo"
    Linux_COMMANDS[5]="cat /etc/redhat-release"
    Linux_COMMANDS[6]="cat /proc/version"
    Linux_COMMANDS[7]="nvidia-smi  -q"

    if   [ $1 = "AIX" ]; then 
        COMMANDS=("${AIX_COMMANDS[@]}")
    elif [ $1 = "SunOS" ]; then 
        COMMANDS=("${SunOS_COMMANDS[@]}")
    elif [ $1 = "Linux" ]; then 
        COMMANDS=("${Linux_COMMANDS[@]}")
    fi
}

checkStorageFile(){
    if [ -s $1 ]; then
        rm $1
    fi
}

exitStatusDescription(){
    if   [ $1 -eq 127 ]; then 
    echo "$2: Command not found" >> $file_name
    elif [ $1 -eq 126 ]; then 
            echo "$2: Command invoked cannot execute - Permission problem or command is not an executable" >> $file_name
            cmd=`echo $2 | awk '{print $1}'`
            path=`whereis $cmd | awk '{print $2}'`
            echo "\"$path\" has the following permission: `ls -ltr $path | awk '{print $1}'`" >> $file_name
            echo "----> \"$path\" has the following permission: `ls -ltr $path | awk '{print $1}'`" 
    fi
}

printOnFileRichCommandInformation(){
    echo >> $file_name 
    echo >> $file_name 
    echo  �************************************************************ � >>$file_name 
    echo �************************************************************ � >>$file_name
    echo �************ Additional information for $1     ************ � >> $file_name
    echo �************************************************************ � >>$file_name
}

executeCommands(){
    COMMANDS=$1
    len=${#COMMANDS
[*]}
    command=0
    while [ $command -lt $len ]; do
        echo >> $file_name 
        echo "************************************************************" >> $file_name

        if [[ ${COMMANDS[$command]} == lpar* ]];
        then
            printOnFileRichCommandInformation "LPAR"
        fi
        
        if [[ ${COMMANDS[$command]} == nvidia* ]];
            then
                printOnFileRichCommandInformation "GPU NVIDIA"
        fi


        echo �************************************************************ � >> $file_name
        
        echo "   ${COMMANDS[$command]}"
        echo �bash$ ${COMMANDS[$command]}� >> $file_name
        eval ${COMMANDS[$command]} >> $file_name
        OUT=$?
        if [ $OUT -ne 0 ]; then 
            exitStatusDescription "$OUT" "${COMMANDS[$command]}"
        fi
        command=$((command+1))
    done

}

printHeadFormatInfo(){
    echo
    echo �***************************************************************************** �
    echo "Gathering system information ........"
    echo �*********************************************************** � > $file_name
    echo �*********     GETTING SYSTEM INFORMATION          ********* � >> $file_name
    echo �*********************************************************** � >> $file_name
    echo �***********   OPERATING SYSTEM TYPE:   $OS_TYPE   ************** � >> $file_name
    echo "*****    Date: `date`    ******" >> $file_name
    echo
}

execute(){
    COMMANDS=$1
    ########################  AIX Operating System ###########################################
    if [ "$OS_TYPE" = "AIX" ]; then
        echo "Operating System: AIX "
        echo "System Information from following commands: "
        executeCommands "${COMMANDS}"

    elif [ "$OS_TYPE" = "SunOS" ]; then
    ########################  SunOS Operating System ###########################################

        echo "Operating System: SunOS"
        echo "System Information from following commands: "
        executeCommands "${COMMANDS}"

    elif [ "$OS_TYPE" = "Linux" ]; then
    ########################  Gnu/Linux Operating System ###########################################
        echo "Operating System: GNU/Linux"
        echo "System Information from following commands: "
        executeCommands "${COMMANDS}"
    fi
}

executeFurtherCommand(){
    set WISH= "y"
    while [ "$WISH" != "n" ] 
    do
        echo �***************************************************************************** �
        echo "Do you wish to gather further information? <y or n>"
        read WISH

        if [ $WISH = "y" ] ; then
           echo >> $file_name 
           echo "************************************************************" >> $file_name
           echo "Please insert the command you would like to execute...."
           read -p '$bash ' COMMAND
           echo �bash$ $COMMAND� >> $file_name
           eval $COMMAND >> $file_name
           OUT=$?
           if [ $OUT -ne 0 ]; then 
            exitStatusDescription "$OUT" "$COMMAND"
           else 
            echo "......command \"$COMMAND\" executed successfully!"
           fi
        fi 

        if [ "$WISH" != "n" ] && [ "$WISH" != "y" ] ; then
           echo "your choice $WISH is incorrect"
        fi

    done
}

printBottomFormatInfo(){
    echo
    echo "........these information are stored on `pwd`/$1"
    echo �***************************************************************************** �
}


###################################################################################################
checkStorageFile "$file_name"

printHeadFormatInfo

getCommandsSpecificOS $OS_TYPE

execute "${COMMANDS}"

printBottomFormatInfo "$file_name"

executeFurtherCommand


echo >> $file_name 
echo >> $file_name 
echo "************************************************************" >> $file_name
echo

Note that the shell you get may not have functions, either.

Instead of arrays, how about strings?

AIX_COMMANDS='hostname
uname -a
ulimit -a
smctl
swap -l
/etc/prtconf
oslevel �s
lparstat �i
lscfg -vp | grep -ip proc | grep "PROC"
mpstat'

OLDIFS="$IFS"
# Split on newlines and only newlines
IFS="
"

for X in $AIX_COMMANDS
do
        echo "Got command $X"
done

IFS="$OLDIFS"