Sponsored Content
Top Forums Shell Programming and Scripting Bash variable assignment failure/unary operator expected Post 303012604 by port43 on Tuesday 6th of February 2018 02:47:26 PM
Old 02-06-2018
Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue.
Code:
#!/bin/bash
PLATFORM="AIX"
NEEDSPC=3000
set -x

if [ "${PLATFORM}" = "AIX" ]; then
    lsvg | grep -v rootvg | while read VGNAME
    do
        PPSTRG=$(lsvg ${VGNAME} | awk -F: '/PP SIZE/ {print $NF}')
        PPSIZE=$(echo ${PPSTRG} | awk '{print $1}')
        PPFREE=$(lsvg ${VGNAME} | awk -F: '/FREE PP/ {print $NF}' | awk '{print $1}')
        ((FREESPACE=${PPFREE}*${PPSIZE}))
        if [ ${FREESPACE} -gt ${NEEDSPC} ]; then
            printf "Sufficient free space for LV in ${VGNAME}\n"
            ((PPNEED=${NEEDSPC}/${PPSIZE}+1))
            NOTFREE=0
            break
        else
            printf "Insufficient space to create LV in ${VGNAME}\nFree space: ${FREESPACE} MB  Required: ${NEEDSPC} MB\nExpand ${VGNAME} and retry\n"
            NOTFREE=1
        fi
    done
    if [ ${NOTFREE} -eq 1 ]; then
        printf "nope\n"
        exit 64
    fi
else
    printf "Solaris\n"
fi
printf "NOTFREE: ${NOTFREE}\n"

yields:
Code:
prompt> ./dammit
+ '[' AIX = AIX ']'
+ lsvg
+ grep -v rootvg
+ read VGNAME
++ lsvg datavg
++ awk -F: '/PP SIZE/ {print $NF}'
+ PPSTRG='        256 megabyte(s)'
++ echo 256 'megabyte(s)'
++ awk '{print $1}'
+ PPSIZE=256
++ lsvg datavg
++ awk -F: '/FREE PP/ {print $NF}'
++ awk '{print $1}'
+ PPFREE=239
+ (( FREESPACE=239*256 ))
+ '[' 61184 -gt 3000 ']'
+ printf 'Sufficient free space for LV in datavg\n'
Sufficient free space for LV in datavg
+ NOTFREE=0
+ break
+ '[' -eq 1 ']'
./dammit: line 23: [: -eq: unary operator expected
+ printf 'NOTFREE: \n'
NOTFREE:

Yet if I strip out everything except the assignments and if blocks like:
Code:
#!/bin/bash
PLATFORM="AIX"
FREESPACE=4
NEEDSPC=3
set -x

if [ "${PLATFORM}" = "AIX" ]; then
    if [ ${FREESPACE} -gt ${NEEDSPC} ]; then
        NOTFREE=0
    else
        NOTFREE=1
    fi
    if [ ${NOTFREE} -eq 1 ]; then
        printf "nope\n"
    fi
fi
printf "NOTFREE: ${NOTFREE}\n"

The NOTFREE=0 assignment is made like:
Code:
prompt> ./test.sh
+ '[' AIX = AIX ']'
+ '[' 4 -gt 3 ']'
+ NOTFREE=0
+ '[' 0 -eq 1 ']'
+ printf 'NOTFREE: 0\n'
NOTFREE: 0

I'm being dragged, kicking and screaming, into bash from ksh due to SuSE gradually dropping ksh so I'm positive that I have some syntax issue going on but I have no idea what.


Also:
Code:
prompt> bash --version
GNU bash, version 4.2.50(1)-release (powerpc-ibm-aix6.1.2.0)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

unary operator expected error

Hi I am doing a script like if then echo "table name dosent exist" exit fi the problem is if $table_name is null then i am getting the error Please help me Thanks in advance (2 Replies)
Discussion started by: ssuresh1999
2 Replies

2. Shell Programming and Scripting

unary operator expected

hi i am trying to compare a value with value 50. but i am getting " I am using if then echo "------------" fi please help thanks in advance Satya (2 Replies)
Discussion started by: Satyak
2 Replies

3. UNIX for Dummies Questions & Answers

[: =: unary operator expected

HI, while running a script, I got the below mentioned error. /bin/sh: line10 : supportedMozillaVersion() { case "$*" in *rv:1.*) return 0;; *rv:.*) return 0;; *rv:*) return 1;; Mozilla\ 1.*) return 0;; Mozilla\ .*) return 0;; *) return 1;; esac } supportedFirefoxVersion() { case... (7 Replies)
Discussion started by: bhag281
7 Replies

4. Shell Programming and Scripting

unary operator expected

Im trying to fix my /etc/weekly that rotates various logs however it does them no matter what filesize they are and i want them to only do it if there file size exceeds 2M or something. So I'm playing with a script to get the filesize using a ls -l command which works and puts the value into a... (3 Replies)
Discussion started by: timgolding
3 Replies

5. Shell Programming and Scripting

line 5: [: -gt: unary operator expected

Hello all, :confused: I am not getting this error. Pls help me. ./construct_operon.sh: line 5: #!/bin/bash # Construct Operons using gene pairs. rm -rf operons_result lines=`awk 'END {print NR}' ecoli_operons` while ; do head -1 ecoli_operons | awk '{print $1"\n"$2}' > pattern ... (5 Replies)
Discussion started by: admax
5 Replies

6. UNIX for Dummies Questions & Answers

[: =: unary operator expected error

Why am I getting this error.... #!/bin/sh # iOS-Ad-Remover # Marshall Ford @ marshallbford@gmail.com # This project is hosted @ http://ios-ad- # remover.sourceforge.net # Under the GNU GPL open source license clear echo if ; then echo "You need to be root to run this script."; exit 0; #... (24 Replies)
Discussion started by: mbf123
24 Replies

7. Shell Programming and Scripting

unary operator expected

i=0 while Shell script reports an error "unary operator expected" pointing the above line. $i by default is 0 and holds integer value and $buf is also holding integer value. Please can some one let me know what is missing. Thanks. (1 Reply)
Discussion started by: sunrexstar
1 Replies

8. UNIX for Dummies Questions & Answers

: unary operator expected

Hiya all, Why do I get a :unary operator expected when I try to put a condition statement in my shell script (sh) like so and how to fix? if ; then echo "say hello" else echo "don't say hello" fi ? It seems if the script receives an argument it works but if I don't put an... (4 Replies)
Discussion started by: cyberfrog
4 Replies

9. UNIX for Dummies Questions & Answers

[: -gt: unary operator expected

Hi I have problem with my script. I dont now why but i don't change anything and script stop working. this is my code: #!/bin/sh for i in `ps -A | grep pocron.sh | grep -v grep | awk '{print $2}'` do COUNT=$((COUNT+1)) done ostatnie_wykonanie=`cat porader.log`... (1 Reply)
Discussion started by: fotex
1 Replies

10. Shell Programming and Scripting

Unary operator expected

In all my Googling, this usually happens when someone is comparing a variable, and that variable is unset. That doesn't appear to be the case for me... #!/bin/bash -x while read line do f=$(echo $line | tr -s ' ' | cut -d' ' -f 3) echo $f if then echo "This... (2 Replies)
Discussion started by: jnojr
2 Replies
All times are GMT -4. The time now is 06:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy