limits.conf
中附加更改。两种情况:* soft nofile 100000
* hard nofile 150000
$LINE_TO_APPEND
中时,此[[bash脚本会将limits.conf
添加到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
[如果您想使用更纯粹的[[sed]方法,则以下带有硬编码字段的通用性较低的命令将起作用(我陷入尝试使用$
的“转义地狱”中;我将尝试重新访问稍后再回答!):
sed -i 'H;1h;$!d;x;/soft *nofile/!s/$/\n* soft nofile 100000/' limits.conf
对[[sed解决方案的解释:
[H;1h;$!d;x
将整个文件读入模式缓冲区(请参见sed: read whole file into pattern space without failing on single-line input)[
/soft *nofile/!
,如果文件没有
包含soft nofile
(任意间距),
s/$/\n* soft nofile 100000/
然后在末尾加上* soft nofile 100000
-i
更改文件到位