Write to file and read content of file

JavaScript

const fs = require('fs');
let output_file = "/tmp/output_js.txt";
fs.writeFile(output_file, "Hello World!\n", (err, resp) => {
    if (err) throw err;
    fs.readFile(output_file, (err, data) => {
	if (err) throw err;
        process.stdout.write(data.toString());
    });
});

PHP

<?php
$output_file = "/tmp/output_php.txt";
$fh = fopen($output_file, "w") or die("Failed to open ".$output_file);
fwrite($fh, "Hello World!\n");
fclose($fh);

$fh = fopen($output_file, "r") or die("Failed to open ".$output_file);
while(!feof($fh)) {
  print fgets($fh);
}
fclose($fh);

Perl

use strict;
use FileHandle;
my $output_file = "/tmp/output_perl.txt";
if (my $fh = new FileHandle('> '.$output_file)) {
  print $fh "Hello World!\n";
  $fh->close();
}

if (my $fh = new FileHandle($output_file)) {
  local $/ = undef;
  print <$fh>;
  $fh->close();
}

Python

import sys
output_file = "/tmp/output_python.txt"
try:
  fh = open(output_file, "w") or die("Failed to write to " + output_file)
except IOError:
  print("Failed to write to " + output_file)
  sys.exit(1)

fh.write("Hello World!\n")
fh.close()

fh = open(output_file, "r")
print(fh.read(), end="")
fh.close()

Ruby

output_file = "/tmp/output_ruby.txt"
File.open(output_file, "w") do |wfh|
    wfh.puts("Hello World!\n")
end

File.open(output_file, "r") do |rfh|
    print(rfh.read())
end

Shell

#!/bin/sh
output_file="/tmp/output_shell.txt";
echo "Hello World!\n" > $output_file;
CONTENT=$(<$output_file)
echo $CONTENT;