unary operator expected error in Bash if condition
If you know you're always going to use Bash, it's much easier to always use the double bracket conditional compound command [[ ... ]]
, instead of the POSIX-compatible single bracket version [ ... ]
. Inside a [[ ... ]]
compound, word-splitting and pathname expansion are not applied to words, so you can rely on
if [[ $aug1 == "and" ]];
to compare the value of $aug1
with the string and
.
If you use [ ... ]
, you always need to remember to double quote variables like this:
if [ "$aug1" = "and" ];
If you don't quote the variable expansion and the variable is undefined or empty, it vanishes from the scene of the crime, leaving only
if [ = "and" ];
which is not a valid syntax. (It would also fail with a different error message if $aug1
included white space or shell metacharacters.)
The modern [[
operator has lots of other nice features, including regular expression matching.
unary operator expected in shell script when comparing null value with string
Since the value of $var
is the empty string, this:
if [ $var == $var1 ]; then
expands to this:
if [ == abcd ]; then
which is a syntax error.
You need to quote the arguments:
if [ "$var" == "$var1" ]; then
You can also use =
rather than ==
; that's the original syntax, and it's a bit more portable.
If you're using bash, you can use the [[
syntax, which doesn't require the quotes:
if [[ $var = $var1 ]]; then
Even then, it doesn't hurt to quote the variable reference, and adding quotes:
if [[ "$var" = "$var1" ]]; then
might save a future reader a moment trying to remember whether [[
... ]]
requires them.
Unary operator expected in Bash
You are using indirection. If the variable ${BLOCK1FRAN}
points to an empty variable, you'll get the error message. Make sure that the variable pointed by ${BLOCK1FRAN}
contains a valid numeric value.
If you want an empty string and nonnumeric values to be evaluated as zero (0
), use the following syntax.
if [[ $(date +%k%M) -ge ${!BLOCK1FRAN} ]]; then whatever ; fi
bash comparing variables that are integers with -gt -lt
Probably your input file contains DOS line endings or other improper formatting. Your code should work for well-formed inputs.
However, the proper way to loop over the lines in a file is
#!/bin/bash
min=$(sed 1q "$1")
max=$min
sum=0
while read -r num; do
if [ "$num" -gt "$max" ]
then
max=$num
elif [ "$num" -lt "$min" ]
then
min=$num
fi
((sum+=num))
done<"$1"
echo "Maximum: $max"
echo "Minimum: $min"
echo "Sum: $sum"
Notice also that backticks and $[[...]]]
use syntax which has been obselescent for decades already.
Linux bash '[: -ge: unary operator expected' error
The source of the problem reported in the question is an empty decvalue
as syme has already stated.
Considering the purpose of the script, it seems you attempted to write a recursive function. For this reason, i suggest you to follow the comment under your question by jww regarding debugging!
Moreover, you are encouraged to search for questions on recursive functions in bash on SO. There are excellent answers for factorial computations in bash.
Think about which quantities are "transported" and which are returned/compiled in the end.
If you are really stuck, you find 90% of the solution below.
#!/bin/bash
romanLetters(){
local decvalue=$1
local roman
declare -i decvalue
if [ $decvalue -ge 1000 ]; then
romanvalue="M$(romanLetters $((decvalue - 1000)))"
echo $romanvalue
elif [ $decvalue -ge 900 ]; then
romanvalue="CM$(romanLetters $((decvalue - 900)))"
echo $romanvalue
# ... and so on ...
fi
}
echo "1900: $(romanLetters 1900)"
Related Topics
Bash - How to Pipe Result from the Which Command to Cd
Setting the Vim Background Colors
Sort Not Sorting as Expected (Space and Locale)
Differencebetween -I and -L in Makefile
How Stable Is S3Fs to Mount an Amazon S3 Bucket as a Local Directory
What Are Libtool's .La File For
Setting Default Permissions for Newly Created Files and Sub-Directories Under a Directory in Linux
Pattern Match Does Not Work in Bash Script
Gem5 Syscall Emulation Arm C Hello World Fails with "Fatal: Syscall Gettid (#224) Unimplemented"
Unit Testing for Shell Scripts
Apache Not Accepting Incoming Connections from Outside of Localhost
How to Set Cron to Display Gui Application
What Is the Equivalent of Solution Files in Linux C++ Ide
Can Read() Function on a Connected Socket Return Zero Bytes