I want to append changes in limits.conf
if not exists using shell script. Two cases:
- If limit not specified, I have to add using sed.
- If limit exists, it should not change.
* soft nofile 100000
* hard nofile 150000
I want to append changes in limits.conf
if not exists using shell script. Two cases:
* soft nofile 100000
* hard nofile 150000
This bash script will add $LINE_TO_APPEND
to the end of limits.conf
when the given type and item is not already present in limits.conf
:
LINE_TO_APPEND='* hard nofile 512'
read -a line_array <<< "$LINE_TO_APPEND"
line_type=${line_array[1]} # consists of just normal characters
line_item=${line_array[2]} # consists of just normal characters
if [[ -z $(grep "$line_type *$line_item" limits.conf) ]]; then
echo "$LINE_TO_APPEND" >> limits.conf
fi
If you want a more purely sed approach, the following less-generic command with hard-coded fields will work (I got caught up in "escaping hell" trying to use $
; I'll try revisiting this answer later!):
sed -i 'H;1h;$!d;x;/soft *nofile/!s/$/\n* soft nofile 100000/' limits.conf
Explanation of sed solution:
H;1h;$!d;x
read entire file into pattern buffer (see sed: read whole file into pattern space without failing on single-line input)/soft *nofile/!
if file does not contain soft nofile
(arbitrary spacing),s/$/\n* soft nofile 100000/
then add * soft nofile 100000
to the end-i
change file in place