title: How to modify rc.local in shell script?
body: <p>I want to add a line(such as <code>'*/data/mod/myservice start some_parameter*'</code>.) to <code>/etc/rc.d/rc.local</code> file in shell script. If there exists a line start with <code>'*/data/mod/myservice start*'</code>, then <strong>replace it</strong> by new one.</p>
  
  <p>In my script, it execute the next python method.</p>
  
  <pre><code>def excuteCmd(cmd):
      import commands
      output = commands.getoutput(cmd)
  
  def setTask(cmd, installFlag):
      print cmd, installFlag
      excuteCmd('cat /etc/rc.d/rc.local &gt;  oldTask')
      input = open('oldTask','r')
      emptyFile = False
      lines = input.readlines()
      input.close()
      taskNum = len(lines)
      output = open('newTask', 'w')
      if (taskNum  == 0):
              if (installFlag):
                      output.write(cmd + '\n')
      else:
              for i in range(taskNum):
                      if (lines[i].find(cmd) == -1):
                              output.write(lines[i])
              if (installFlag):
                      output.write(cmd + '\n')
      output.close()
      excuteCmd('sudo cat newTask &gt; /etc/rc.d/rc.local')
      excuteCmd('rm -f oldTask')
      excuteCmd('rm -f newTask')
  </code></pre>
  
  <p>But when i execute <code>sudo cat newTask &gt; /etc/rc.d/rc.local</code>, it raise the following error.</p>
  
  <p><code>-bash: /etc/rc.d/rc.local: Permission denied</code></p>