Linux – Bash script convert “string” to number

bashlinuxscript

i need to change a string to interger, because i check the size of file (extract with : stat -c%s $filename) with a number, follow the complete script :

#!/bin/bash

# Variable that contain path destination file
path=/sobstitude

# Variable for size
size=1000

# Loop for file scan
for filename in /test/*;
do
    # Take the size of file
    filesize=$(stat -c%s $filename)

# Check if the file is empty
if [ $filesize > $size ]
then

    # Replace file
    mv $filename $path

fi
done

exit 0;

Best Answer

In bash/sh variables contain strings, there is no strict concept of integers. A string may look like an integer and it's enough.

stat -c%s $filename should return a string that looks like an integer.

In your case the main problem is in the redirection operator > (you probably now have files with names that are numbers in the working directory). This snippet:

[ $filesize > $size ]

should be

[ "$filesize" -gt "$size" ]

And use double quotes around variable substitutions and command substitutions.

Related Question