Node. js 학습 의 길 21 - Node. js 로 간단 한 명령 행 도 구 를 작성 합 니 다.

node 를 이용 하여 간단 한 명령 행 도 구 를 만들다.
운영 체제 필요 Linux1. 목표
  • 명령 행 에 자신 이 쓴 명령 을 입력 하여 목표 퀘 스 트 완성
  • 명령 행 은 전역 적 으로 유효 하도록 요구한다
  • 명령 행 에서 삭제 가능
  • 명령 행 역할, 파일 생 성, 현재 날짜 표시
  • 2. 코드 부분
  • 새 파일 이름 sherryFile
  • 파일 sherryFile 의 내용
  • 소개: 파일 을 만 듭 니 다. 파일 내용 은 현재 날짜 와 작성 자 입 니 다.
    #! /usr/bin/env node
    console.log('command start');
    const fs = require('fs');
    let date = new Date().toLocaleDateString();
    let data = date + '
    \t' + '——create By karuru'; fs.writeFile('./date.txt', data, 'utf8', (err) => { if (err) { console.log('sherryFile command wrong', err); return false; } console.log('writeFile success!!!!'); console.log('command end'); });
  • 이 파일 에 집행 권한 부여 chmod 755 sherryFile
  • 이 파일 이 있 는 파일 경로 에 입력 ./sherryFile
  • 다음 내용 을 출력 하면 명령 실행 성공
  • command start
    writeFile success!!!!
    command end
  • 이 파일 디 렉 터 리 에 새로운 date.txt 파일 이 생 성 됩 니 다. 내용 은 다음 과 같 습 니 다
  • 2/28/2018
        create By karuru
  • 명령 을 전역 적 으로 유효 하 게 수정
  • ln sherryFile /usr/local/bin/sherryFile
  • 명령 삭제
  • rm /usr/local/bin/sherryFile

    좋은 웹페이지 즐겨찾기