-1

Keep in mind that i am very new to bash(about 3 days in), I'm trying to count the amount of files in a specific directory and check if there are more than 5 files, But when I run the code below I get back a value type mismatch error and I can't figure out how to either fix it or run different code that accomplishes the same thing. Any Tips or fixes?

I tried to convert the variable by re-assigning it through a math equation I also tried to search for something like this but couldn't find anything

cd /mnt/Backlog/MSFB #Change into directory to count files

backupCount= find . -type f | wc -l #Count files and store in a variable

if [ backupCount -ge 5 ];
then
    echo "More than 5 archives found!"
else
    echo "Testing"
fi

What I want is the if statement to check the value of backupCount and if it's higher than 5 to display a message (for testing purposes) What I got was an error that said: backupCount: integer expression expected

Claudio
  • 10,614
  • 4
  • 31
  • 71
WaffleGod54
  • 1
  • 1
  • 2

2 Answers2

1

Well, you are getting there. In order to save the result of a command as the contents of a variable you use command substitution which has the form var=$(command). So in your case:

backupCount=$(find . -type f | wc -l) #Count files and store in a variable

In order to reference the value stored in a variable, you must dereference the variable by placing the '$' in front of it, e.g.

if [ "$backupCount" -ge 5 ];

Also note, when using [ ... ] you must double-quote your variables.

(note: you receive the error "integer expression expected" because you had backCount within [ ... ] using the -ge comparison, and bash didn't know what backupCount was because it had not been dereferenced, same as using dogs -ge 5, bash doesn't know what "dogs" is so it throws the error "integer expression expected")

You can also use the POSIX arithmetic operators (( ... )) for comparison, e.g.

if (( backupCount > 5 ));

Within ((...)) you do NOT need the '$' sign to dereference the variable.

You also want to include validations at any critical step in your script. For example, when you change directory, you want to know that you made it. You can simply do:

cd /mnt/Backlog/MSFB || exit 1

To immediately exit on failure, or you can add an error message to tell you what failed, e.g.

cd /mnt/Backlog/MSFB || {
    printf "error: failed to change to /mnt/Backlog/MSFB\n" >&2
    exit 1
}

The >&2 directs the output of printf to stderr rather than stdout. Error messages are normally displayed on stderr. (and if we need to backup 1 step further, you have 3 primary streams, stdin (input - 0), stdout (output - 1) and stderr (error - 2)).

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

There should be no spaces between variable name and commands.

Also commands need to be surrounded with `` or put in $()

backupCount=`find . -type f | wc -l`
# same as
backupCount=$(find . -type f | wc -l)

As for accessing variables you need to put $ before the variable name.

if [ $backupCount -ge 5 ];

Optionally (although it is a good practice) you can surround your variable call with double quotes.

if [ "$backupCount" -ge 5 ];

The reason for that is when your variable is empty the expession evaluates to if [ -ge 5 ]; which gives an error and the quotes help avoid this type of error.

The final script would look like this

#!/bin/bash

backupCount=`find . -type f | wc -l` #Count files and store in a variable

if [ "$backupCount" -ge 5 ];
then
    echo "More than 5 archives found!"
else
    echo "Testing"
fi

Blank
  • 423
  • 1
  • 5
  • 16