Reputation: 101
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
}
Upvotes: 0
Views: 2802
Reputation: 88543
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.
Upvotes: 1