I'm trying to compare a folder size with a size limit. When I try to run the script, it gives me the error 'integer expression expected'.
limit=200
foldersize=$(du /home/user1/testfolder)
if [ "$foldersize" -le "limit" ];
then
echo "Folder size is small"
else
echo "Folder size is big"
fi
CodePudding user response:
The size needs to be read
from the du
< <(command)
stream, with tab and new-line as fields separator and no records delimiter, because the output from dc
contains multiple lines to be read as a single record.
Here is an example of output from du --bytes --summarize --total /home/user1/testfolder
:
4954626 /home/user1/testfolder
4954626 total
Your code modified:
#!/usr/bin/env bash
declare -i limit=200000
declare -- test_folder=/home/user1/testfolder
declare -i folder_size
IFS=$' \t\n' read -r -d '' _ _ folder_size _ < <(
du --bytes --summarize --total "$test_folder"
)
if [[ "$folder_size" -le "$limit" ]];
then
printf 'Folder size is small\n'
else
printf 'Folder size is big\n'
fi
CodePudding user response:
Perhaps something like this:
limit=200
folder_size=$(du ~user1/testfolder|cut -d ' ' -f 1)
if (( folder_size <= limit ))
then
echo Folder size is small
else
echo Folder size is big
fi
or, without variables,
if (( $(du ~user1/testfolder|cut -d ' ' -f 1) <= 200 ))
then
...
else
...
fi
CodePudding user response:
do this:
du -s /home/user1/testfolder |cut -f1
instead of :
du /home/user1/testfolder