1

I am trying to feed arguments sequentually using set -A, so I can delete the highest version first and then the base. But I get an error for set command.

Here is the code

    _install=$(rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}\t%{INSTPREFIXES}\n" | grep MQSeriesRuntime | grep 7.0 | grep -v 7.0.1-0 | sort -r | awk '{print $1}')
 _numver=$(rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}\t%{INSTPREFIXES}\n" | grep MQSeriesRuntime | grep 7.0 | grep -v 7.0.1-0 | wc -l)
set -A arrinstall ${_install}

##########################################
# Get a list of all MQ install instances #
##########################################
i=${_numver}
arrayindex=0
while [ i -ne 0 ]
do
  _inst_level=${arrinstall[$arrayindex]}
  _Unum=$(echo ${_inst_level} |  sed -e 's/-/ /g' | awk '{print $2}')
  _Level=$(echo ${_inst_level} |  sed -e 's/-/ /g' | awk '{print $4}')
  i=$((i-1))
  arrayindex=$((arrayindex+1))
done

Here is the error I get

./test.sh: line 8: set: -A: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option-name] [arg ...]
./test.sh: line 15: [: i: integer expression expected

What am I doing wrong here.

here are the output of variables,

$ rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}\t%{INSTPREFIXES}\n" |grep MQSeriesRuntime |grep 7.0 | grep -v 7.0.1-0 |sort -r |awk '{print $1}'
MQSeriesRuntime-U860943-7.0.1-12
MQSeriesRuntime-U860173-7.0.1-11

$ rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}\t%{INSTPREFIXES}\n" |grep MQSeriesRuntime |grep 7.0 | grep -v 7.0.1-0 |wc -l
2
8
  • 1
    What is it that you think set -A does? Commented Oct 2, 2015 at 16:42
  • Assign the arguments sequentially ? Commented Oct 2, 2015 at 16:43
  • 1
    Check the documentation. Commented Oct 2, 2015 at 16:44
  • 1
    So, your shell is ksh and the script is bash? Commented Oct 2, 2015 at 16:53
  • 1
    that seems to be my problem. I have incorrect shell I am referencing to. I changed it to korn shell and is now working fine. Thank you Commented Oct 2, 2015 at 17:02

3 Answers 3

3

set -A is a ksh command, but you are executing your script with bash. The bash equivalent would be simply

arrinstall=(${_install})
3
set -A array -- values ...

is the ksh88 syntax, also recognised by pdksh and derivatives, ksh93 and zsh (in zsh, the -- after -A is not recognised nor needed except in ksh emulation), but not bash.

array=(values  ...)

is the zsh syntax (though strictly speaking rc with an otherwise non-Bourne syntax, supported it before that), also recognised by bash 2.0 and above, ksh93, yash and mksh (based on pdksh) but with some major differences between implementations.

In the case of array=() (an empty list), in ksh93, the variable must have been declared as an array variable beforehand (with typeset -a) as that array=() conflicts with the compound variable declaration.

As for the $_install scalar variable splitting, that's the Bourne split+glob operator invoked whenever you leave a variable expansion unquoted in list context, which you generally need to tune beforehand:

IFS=$'\n' # split on newline only
set -o noglob # disable the glob part which we don't want here
array=($_install) # invoke split+glob

Note that in zsh, split and/or globbing have to be requested explicitly, so:

IFS=$'\n'
array=($=_install) # split only, with the $=var operator

Though, there, you'd rather use the (f) parameter expansion flag to split on linefeeds:

array=(${(f)_install})

In bash, you can use the readarray command to get lines into an array:

readarray -t array < <(printf '%s\n' "$_install")

Or directly:

readarray -t array < <(rpm -q...)

Also note that while the operands of the -ne or other arithmetic test/[ operators are any arithmetic expression in ksh, in bash (and most other shells), they have to be decimal integers, so while:

[ i -ne 0 ]

would work in ksh as i is an arithmetic expression that resolves to the value of the $i variable, in bash, you'd need:

[ "$i" -ne 0 ]

(with $i quoted so as not to invoke the split+glob operator), or you could use the ksh ((...)) arithmetic evaluation operator also supported by bash and zsh:

((i != 0))
1

Please use $ sign while referring to variable i

i=${_numver}
while [ $i -ne 0 ] 

This will correct the error

"./test.sh: line 15: [: i: integer expression expected"
1
  • Note that [ i -ne 0 ] would work in ksh, as ksh accepts any arithmetic expression there. Commented Oct 31, 2017 at 9:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.