0

I am trying to perform a variable swap where the user enters 2 different numbers and a check is performed to make sure that the second number "end" is bigger then the first number "start". If the starting number is bigger then the end number it will perform a swap so that the later calculations will work.

The error that I am getting is: ./Boot.sh: line 94: [: -ne: unary operator expected

                    if [ $end -gt $start ]
                        then
                            start=$swapper
                            end=$start
                            swapper=$end
                    fi

This is the full code as I seem to have made a few mistakes by the comments, I have just tried double brackets and am still having the same error.

                    {
counter=0
while :
    do
        if [ $counter -gt '0' ]     
            then
                printf "\nWould you like to continue terminating processes?\n"
                echo "Type 1 to contiue or 0 to exit"
                read endProgram
            if [ $endProgram -ne '1' ]
                then
                    break
            fi
        fi      
        echo "Welcome to Max process killer"
        while :
            do
                echo "Enter the first number of the process range"
                read start
                echo "Enter the last number of the process range"
                read end
                if [ $start -le '3' -o $end -le '3' ]
                    then
                        echo "Your first and last pid IDs must be geater then 3"
                        read -n 1
                        break
                    if [[ $end -gt $start ]]
                        then
                            start=$swapper
                            end=$start
                            swapper=$end
                    fi
                fi
                printf "\nAre you sure you would like to terminate these processes?\n"
                echo "Enter 1 to confirm or 0 to cancel"
                read confirm
                if [ $read -ne '1' ]
                    then
                        break
                fi
                while [ $start -le $end ]
                    do
                        kill -9 "$start"
                        echo "Process ID:"$start "has been terminated" 
                        start=$(( $start + 1 ))
                done
                break
        done
        counter=$(($counter + 1))
done
                    }
6
  • 2
    Have you tried quoting your variables? What are their values at the time of the if? Commented Aug 24, 2015 at 6:54
  • 2
    Oh, and that swapping code looks wrong, but that's an other question. Commented Aug 24, 2015 at 6:55
  • 1
    "-ne"???? Where??? I suspect one or more of your variables might be corrupt. set -v or bash -x is your friend. And yes, that swapping code is wrong ;) Commented Aug 24, 2015 at 6:55
  • 1
    Show line 94 which contains -ne. Commented Aug 24, 2015 at 7:03
  • I have made an update to the code to show that block which is being effected. The line 94 is the line which has the if statement that I have shown above, excuse some of the formatting of the starting and ending brackets for the block Commented Aug 24, 2015 at 7:06

2 Answers 2

1

Your code:

read confirm
if [ $read -ne '1' ]

You read input to variable confirm and not to variable read. Variable read is empty and you get the error:

./Boot.sh: line 94: [: -ne: unary operator expected

Hint: Use a default value (e.g. 0) to avoid an empty variable:

if [ "${confirm:-0}" -ne '1' ]

From man bash:

${parameter:-word}: Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

${parameter:=word}: Assign Default Values. If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.

Sign up to request clarification or add additional context in comments.

4 Comments

A better solution would be to use the correct parameter $confirm instead of using the default value expansion to produce a constant 0.
Correct :) My hint was for the case if the user simply presses only return.
It doesn't matter what the user types; the code is simply using the wrong parameter name in the check. Maybe you meant to write ${confirm:-0} in the answer. (And it should be quoted since it is used with [ ... ].
@chepner: Now I understand. Thank you. I've updated my answer.
0

Try using double brackets. They are generally safer. Here's a good wiki page comparing [[]] to [].

if [[ $end -gt $start ]]
then
    swapper=$start
    start=$end
    end=$swapper
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.