Error on Integer Expression and Unary Operator

Error on Integer Expression and Unary Operator

I build a program and execute in in the Ubuntu terminal. Below is the code:

#!/bin/sh
#!/bin/bash

echo "File size you want to archive"
read size

echo "File name to archive"
read name

echo "Path to archive"
read path

echo "Size is: $size filename is: $name path is: $path"

a=99k
b=100k

if [ $size -lt $a] #line 19
then
echo "Refused to archive"
elif [ $size -lt $b] #line 22
then 
find $path -type f -size +$size | tar cvzf ~/$name.tar.gz
else
echo "Wrong input"
fi

when I execute and insert the input, there's some error on line 19 and 22 which saying

./filesize.sh: line 19: [: 100k: integer expression expected

./filesize.sh: line 22: [: 100k: integer expression expected

and when I try to use "=" and ">=" sign also it didn't work with error

./filesize.sh: line 19: [: 100k: unary operator expected

./filesize.sh: line 22: [: 100k: unary operator expected

can someone please help to check on this. Appreciate your help. Thanks in advance.

2 Answers

Use shellcheck: (Read man shellcheck)

walt@bat:~(0)$ cat >foo.sh
#!/bin/sh
#!/bin/bash

echo "File size you want to archive"
read size

echo "File name to archive"
read name

echo "Path to archive"
read path

echo "Size is: $size filename is: $name path is: $path"

a=99k
b=100k

if [ $size -lt $a] #line 19
then
echo "Refused to archive"
elif [ $size -lt $b] #line 22
then 
find $path -type f -size +$size | tar cvzf ~/$name.tar.gz
else
echo "Wrong input"
fi
walt@bat:~(0)$ shellcheck foo.sh

In foo.sh line 18:
if [ $size -lt $a] #line 19
^-- SC1009: The mentioned parser error was in this if expression.
   ^-- SC1073: Couldn't parse this test expression.
                  ^-- SC1020: You need a space before the ].
                  ^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.

walt@bat:~(1)$ 
1

The contents of $a and $b contain strings as defined on line 15 respectively 16. You can only compare integer values with the -lt operator.

Try this instead:

#!/bin/bash

# only enter digits here
echo "File size (in kb) you want to archive" 
read size

echo "File name to archive"
read name

echo "Path to archive"
read path

echo "Size is: $size filename is: $name path is: $path"

a=99
b=100

if [ $size -lt $a ]; then
  echo "Refused to archive"
elif [ $size -lt $b ]; then
  find $path -type f -size +$size | tar cvzf ~/$name.tar.gz
else
  echo "Wrong input"
fi

What I did:

  1. removed duplicated shebang at line one
  2. removed non-digit k from $a and $b
  3. fixed the if-statement. you need a space before closing the square brackets.

Important: $a, $b and $size must all contain nothing but digits to make the comparison work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.