ディレクトリを作成、権限を設定、削除

Contents

出力結果

mkdir
chmod
rmdir

コード例

JavaScript

class MkdirChmodRmdir {
  constructor(opt) {
    this.dir = 'js_dir';
    this.fs = require('fs')
  }

  run() {
    if(!this.fs.existsSync(this.dir)) { 
      console.log("mkdir");
      this.fs.mkdirSync(this.dir, { recursive: true }, (err) => {
        if (err) {
          throw err;
        }
      });
    }

    if(this.fs.existsSync(this.dir)) { 
      console.log("chmod");
      this.fs.chmodSync(this.dir, 0o777, (err) => {if(err){throw err;}});

      console.log("rmdir");
      this.fs.rmdirSync(this.dir, (err) => {if(err){throw err;}});
    }
  }
};

module.exports = MkdirChmodRmdir;

if(!module.parent) {
  let mcr = new MkdirChmodRmdir();
  mcr.run();
}

PHP

<?php
class MkdirChmodRmdir {
  public function __construct($opt = []){
    $this->dir = 'php_dir';
  }

  public function run() {
    if(!file_exists($this->dir)) {
      print "mkdir\n";
      mkdir($this->dir);
    }

    if(file_exists($this->dir)) {
      print "chmod\n";
      chmod($this->dir, 0777);

      print "rmdir\n";
      rmdir($this->dir);
    }
  }
}

if ( !isset(debug_backtrace()[0]) ) {
  $mcr = new MkdirChmodRmdir();
  $mcr->run();
}

Perl

package MkdirChmodRmdir;
use strict;

sub new(){
  my $class = shift;
  my $self = {
    'dir' => 'perl_dir'
  };
  return bless($self);
}

sub run(){
  my $self = shift;
  if(!-d $self->{'dir'}) {
    print "mkdir\n";
    mkdir($self->{'dir'});
  }

  if(-d $self->{'dir'}) {
    print "chmod\n";
    chmod(0777, $self->{'dir'});

    print "rmdir\n";
    rmdir($self->{'dir'});
  }
}

if ($0 eq __FILE__) {
  my $mcr = new MkdirChmodRmdir();
  $mcr->run();
}
else{
  1;
}

Python

import os
class MkdirChmodRmdir:
  def __init__(self):
    self.dir = "python_dir"

  def run(self):
    if(not os.path.exists(self.dir)):
      os.mkdir(self.dir)
      print("mkdir")

    if(os.path.exists(self.dir)):
      print("chmod")
      os.chmod(self.dir, 0o777)

      print("rmdir")
      os.rmdir(self.dir)

if __name__ == '__main__':
  mcr = MkdirChmodRmdir()
  mcr.run()

Ruby

require 'fileutils'

class MkdirChmodRmdir
  def initialize(opt = {})
      @dir = "ruby_dir"
  end

  def run
    if not Dir.exist?(@dir)
      puts("mkdir")
      FileUtils.mkdir_p(@dir)
    end

    if Dir.exist?(@dir)
      puts("chmod")
      FileUtils.chmod(0o777, @dir)

      puts("rmdir")
      FileUtils.rmdir(@dir)
    end
  end
end

if $0 == __FILE__
  mcr = MkdirChmodRmdir.new()
  mcr.run()
end

Rust

use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;

fn main() {
  let dir = "rust_dir";
  if !Path::new(dir).exists() {
    println!("mkdir");
    match fs::create_dir(dir) {
      Err(why) => println!("! {:?}", why.kind()),
      Ok(_) => {},
    }
  }

  if Path::new(dir).exists() {
    println!("chmod");
    match fs::set_permissions(dir, PermissionsExt::from_mode(0o777)) {
      Err(why) => println!("! {:?}", why.kind()),
      Ok(_) => {},
    }

    println!("rmdir");
    match fs::remove_dir(dir) {
      Err(why) => println!("! {:?}", why.kind()),
      Ok(_) => {},
    }
  }
}

Shell

#!/bin/sh
DIR="shell_dir"
 
if [ -d $DIR ]; then
  :
else
  echo "mkdir";
  mkdir $DIR;
fi
 
if [ -d $DIR ]
then
  echo "chmod";
  chmod 777 $DIR;
 
  echo "rmdir";
  rmdir $DIR;
fi