I want to write a bash script which can update the /etc/security/limits.conf
file.
The file could contain something like:
* soft core 0
* hard rss 10000
@student hard nproc 20
@faculty soft nproc 20
@faculty hard nproc 50
ftp hard nproc 0
@student - maxlogins 4
Let's say I want to run the script like so:
Usage:
update-ulimits <domain> <type> <item> <value>
Examples:
./update-ulimits '*' hard rss 20000
./update-ulimits bob hard nofile 1024
Which would change the file to contain:
* soft core 0
* hard rss 20000
@student hard nproc 20
@faculty soft nproc 20
@faculty hard nproc 50
ftp hard nproc 0
@student - maxlogins 4
bob hard nofile 1024
So if the file already contains a line which has the domain
, type
and item
it would replace the value
, otherwise it will append a new line containing all 4 arguments. The columns do not need to be aligned.
This is a specific example where there are only a couple of meta characters to worry about but I'm curious whether there is a nice solution which could handle all arbitrary meta characters. I'm guessing perl and awk should be able to do it but I am not too familiar with either.