BATsh Bang Cheat Sheet [VI] Tieng Viet
=======================================

----------------------------------------------------------------------
TOM TAT
  BATsh la mot trinh thong dich shell song ngu chay cu phap cmd.exe
  va bash/sh trong cung mot tep lenh, tu dong chuyen che do tung dong.
  Khong can shell ngoai -- thuan Perl.

VI DU HON HOP
  :: Phan CMD (token dau la chu hoa)
  @ECHO OFF
  SET LANG=BATsh

  # Phan SH (token dau chua chu thuong)
  greet() { echo "Xin chao $1 (che do bash)"; }
  greet $LANG
  result=$(echo $LANG | perl -e 'while(<STDIN>){chomp;print uc}')
  echo "Chu hoa: $result"

  :: Tro lai CMD (doc ket qua SH)
  ECHO Tro lai CMD: %result%

  # Chay: perl lib/BATsh.pm script.batsh
----------------------------------------------------------------------


BATsh la mot shell song ngu chay ca cu phap cmd.exe va bash/sh
trong cung mot script. Token dau tien cua moi dong xac dinh che do.

1. Phat hien che do
--------------------
  ECHO hello          -> che do cmd.exe (token dau tien toan chu hoa)
  echo hello          -> che do bash/sh (token dau tien co chu thuong)
  # binh luan         -> binh luan (bo qua)
  :: binh luan        -> binh luan (kieu CMD, bo qua)
  REM binh luan       -> binh luan (kieu CMD, bo qua)

2. Khoi dong Shell
-------------------
  perl lib/BATsh.pm                    # REPL tuong tac
  perl lib/BATsh.pm script.batsh       # chay file script
  perl lib/BATsh.pm -e "echo xin chao" # lenh mot dong

  Tu Perl API:
    use BATsh;
    BATsh->run('script.batsh');
    BATsh->run_string("echo xin chao");
    BATsh->repl();

3. Cau noi bien moi truong
----------------------------
  Cac khoi CMD va SH chia se bien qua %ENV cua Perl.

  export FOO=xin_chao # SH dat FOO
  ECHO %FOO%          # CMD doc FOO qua cau noi (Windows)

  SET BAR=the_gioi    # CMD dat BAR
  echo $BAR           # SH doc BAR qua cau noi

4. SETLOCAL / ENDLOCAL
-----------------------
  SETLOCAL           # luu anh chup %ENV
  SET TMP=gia_tri_cuc_bo
  ECHO %TMP%
  ENDLOCAL           # khoi phuc %ENV (TMP bien mat)

  Pham vi co the long nhau.

5. Long nhau (Che do hon hop)
------------------------------
  Cac cau truc khoi phai duoc ghep cap dung.

  for x in 1 2; do   # SH mo khoi
      ECHO item $x   # dong CMD trong khoi SH
  done               # SH dong khoi

  Tu khoa CMD: IF/ENDIF  FOR/NEXT  WHILE/WEND  DO/LOOP
  Tu khoa SH:  if/fi  for/done  while/done  until/done  case/esac  { }

6. Dinh nghia Chương trinh con
-------------------------------
  :GREET
  echo "Xin chao $BATSH_ARG1"
  RET

7. CALL va source
------------------
  CALL :GREET the_gioi   # goi chương trinh con voi tham so
  CALL khac.batsh        # them/chay file .batsh khac (CMD)
  source khac.batsh      # them/chay file .batsh khac (SH)
  . khac.batsh           # ky hieu cham POSIX

  Tham so: $BATSH_ARG1 .. $BATSH_ARGn  (%BATSH_ARG1% trong CMD)
  So luong: $BATSH_ARGC

8. Perl API
-----------
  BATsh->run($file)            # chay file .batsh
  BATsh->run_string($source)   # chay chuoi nguon
  BATsh->run_lines(@lines)     # chay mang dong
  BATsh->repl()                # REPL tuong tac
  BATsh->classify_token($tok)  # tra ve 'CMD' hoac 'SH'
  BATsh->setlocal()            # anh chup %ENV
  BATsh->endlocal()            # khoi phuc %ENV
  BATsh->call_sub($lbl, @args) # goi chương trinh con
  BATsh->source_file($file)    # them file .batsh
  BATsh->version()             # chuoi phien ban

9. Ghi chu nen tang
--------------------
  Windows: Ca hai khoi CMD va SH chay bang Perl thuan tuy -- khong can cmd.exe/bash/sh ngoai.
  UNIX:    Ca hai khoi CMD va SH chay bang Perl thuan tuy -- khong can cmd.exe/bash/sh ngoai.

10. Yeu cau
-----------
  Perl 5.005_03 tro len. Chi module loi (File::Spec, Carp).
  Khong phu thuoc CPAN.

Xem them: https://metacpan.org/dist/BATsh

11. Duong ong CMD va cac bien the tham so
-------------------------------------------
  cmd1 | cmd2              # Duong ong qua file tam thoi (Pure Perl)
  SET /P VAR=Nhac:         # Doc mot dong tu STDIN vao VAR
  %~dp0                    # O dia+thu muc cua %0 (dung nhieu nhat)
  %~n1, %~x1, %~nx1       # Ten file, phan mo rong, ca hai

12. Ham SH va mo rong bien
----------------------------
  f() { echo $1; }         # Dinh nghia ham
  ${var%.*}  ${var%%.*}    # Xoa hau to (ngan nhat/dai nhat)
  ${var#*.}  ${var##*.}    # Xoa tien to (ngan nhat/dai nhat)
  ${var/a/b} ${var//a/b}   # Thay the (dau tien/tat ca)
  ${var^^} ${var,,}        # Chu hoa/chu thuong
  cmd > f  cmd >> f  2>&1  # Chuyen huong
  cmd1 && cmd2             # Noi AND
  cmd1 || cmd2             # Noi OR
