如何使用sed命令更改limits.conf中的ulimit值? [重复]

问题描述 投票:-2回答:1
如果不使用Shell脚本,我想在limits.conf中附加更改。两种情况:

    如果未指定限制,则必须使用
  1. sed添加。
  2. 如果存在限制,则不应更改。
  • * soft nofile 100000 * hard nofile 150000
  • linux bash shell sed text-processing
    1个回答
    1
    投票
    当给定的类型和项目尚未出现在$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更改文件到位
  • © www.soinside.com 2019 - 2024. All rights reserved.