I am writing a UNIX script that reads the size of a text file and if the file is of some size it should print the text file. If it is not an else loop executes and the process continues.
I am using the following command to find the size of that text file.
ls -l ${filepath}/{filename}.lst | awk '{print $5}'
How do I assign it to a variable inside the script and put it in an if condition?
or example the if condition should be like if[$var==461]
does this work?
or is there any other command i can use to find the size of the file?
Simply put it as-
I wouldn't recomment
stat
which is not portable not being specified by POSIX.Here is a safer way to get the file size in a variable.
Assign your file size in a variable and then check equality with -eq
You can use the
stat
command which eliminates the need to useawk
.For example in in linux with bash where myfile is your file path:
Take note of the equality command
-eq
that's the arithmetic binary operator in bash.Alternatively you can use a variable for the file path: