title: How do I create a file using sudo and write into it?
body: <p>I created a bash script file:</p>
  
  <pre><code>#!/bin/bash
  default_card=`head -1 /proc/asound/modules`
  echo $default_card
  
  if [ ! -e /etc/modprobe.d/sound.blacklist.conf ] ; then
     echo "Default sound card(snd_hda_intel) is not added in black list"
  /usr/bin/expect &lt;&lt;delim
  exp_internal 0
  set timeout 20
  spawn sudo sh -c "echo 'blacklist snd_hda_intel' &gt;  /etc/modprobe.d/sound.blacklist.conf"
  expect "password for ubuntu:"
  send "1234\n"      
  expect eof 
  delim
  else
     echo "Default sound cardis already added in black list";
  fi
  </code></pre>
  
  <p>I am creating a black list file in "/etc/modprobe.d". Creating or deleting any file from "/etc" requires <code>sudo</code> access.</p>
  
  <p>I want to implement the same functionality in Ruby using a Rake task. I created the task as:</p>
  
  <pre><code>desc "Check/creates soundcard blacklist"
  task :create_blacklist do
    begin
      if !File.exists?("/etc/modprobe.d/sound.blacklist.conf")
        # code for creating new file and write into it
        ......
        ......
      else
        puts "Sound-card blacklist file is present at /etc/modprobe.d/sound.blacklist.conf"
      end
    rescue Exception =&gt; e
      puts "problem creating file #{e.message}"
    end
  end
  </code></pre>
  
  <p>I don't know how to create  new file using sudo, and write into it.</p>
  
  <p>I am using Ruby 1.9.3 (without RVM).</p>