Add condition related to sysctl value in bash file
I would like to execute something only when sysctl vm.max_map_count value is less than 262144. something like:
if [ vm.max_map_count -lt 262144]
then
sysctl -w vm.max_map_count = 262144
fi
the question is, how to read the value of vm.max_map_count in bash.
Any ideas how to do that?
Solution 1:
The following command:
sysctl -n vm.max_map_count
prints the value without printing the key name. To use the value as an argument to another command, you need command substitution:
if [ "$(sysctl -n vm.max_map_count)" -lt 262144 ]
then …
Note there's a space before ]
. Remember [
is a regular command (like ls
or echo
), not a part of shell syntax; and ]
is just the last argument. The [
command requires its last argument to be ]
. I'm mentioning this because in your original code the last argument is 262144]
, it's syntactically wrong.
Also sysctl -w vm.max_map_count = 262144
will not work. The right syntax is sysctl -w vm.max_map_count=262144
.