2011-12-28 7 views
2

シェルスクリプトで/etc/rc.d/rc.localファイルに行(たとえば、'*/data/mod/myservice start some_parameter*'。)を追加します。 '*/data/mod/myservice start*'で始まる行がある場合は、は新しいものでを置き換えます。シェルスクリプトでrc.localを変更するには?

私のスクリプトでは、次のpythonメソッドを実行します。

def excuteCmd(cmd): 
    import commands 
    output = commands.getoutput(cmd) 

def setTask(cmd, installFlag): 
    print cmd, installFlag 
    excuteCmd('cat /etc/rc.d/rc.local > 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 > /etc/rc.d/rc.local') 
    excuteCmd('rm -f oldTask') 
    excuteCmd('rm -f newTask') 

ただし、sudo cat newTask > /etc/rc.d/rc.localを実行すると、次のエラーが発生します。

-bash: /etc/rc.d/rc.local: Permission denied

答えて

2

これは、あなたがへの書き込みやファイルを削除するのいずれかの権限を持っていないことを意味します。また、パスワードを入力せずにsudoコマンドを実行することはできませんので、スクリプト自体はsudo python scriptnameを使用して実行するのが理想的です。

+0

また、私はそうだと思います。sudoコマンドは、>記号の前に実行されるものを指します。 jknuppはおそらくそれを知っているが、私はそれが彼の答えではっきりしていたとは思わなかった:) –

+0

thx、それは解決されている! – zhouzuan2k

2

sudo command > filenameは(root権限で)sudoを使用してcommandを実行しますが、ユーザーの権限(の/ etcへの書き込みが不十分)とfilenameに書き込みます。このようにそれを想像:

(sudo command) > filename 

sudoは括弧で囲ま部分にのみ適用されます。

sudoを使用してスクリプト全体を実行できます。

関連する問題