스창 : 윈도우화면>시작버튼>명령어입력 부분에서 cmd.exe를 실행  --> 까만색의 화면이 뜬다.

나는 이 화면을 편의상 도스창이라고 부르겠다.  이 도스창과 과거 DOS OS와는 상관이 없다. 단지 모양이 비슷할 뿐이다.

 

Batch file과 Shell Script는 같은 용어이다.

단,

Batch file : Microsoft에서 부르는 용어.

Shell Script : Linux에서 부르는 용어.

 

아래의 모든 설명은 MS를 기준으로 설명한다.

배치파일은 텍스트 파일로써, Shell 프로그램에서 실행되는 명령어들과 조건문들의 내용을 담고 있다.

쉘프로그램이 배치파일을 실행시키면, 한줄 한줄씩 배치파일의 내용이 실행이된다.

배치파일의 종류는 확장자가 .bat 파일    :  cmd.exe 또는 command.com으로 실행시킬 수 있다.

                          확장자가 .vbs 파일   :  cscript.exe 또는 wscript.exe로 실행시킬 수 있다.

                          확장자가 .js  파일     :  cscript.exe 또는 wscript.exe로 실행시킬 수 있다.

                          확장자가 .ps1 파일   :  powershell.exe로 실행시킬 수 있다. 

     

bat 파일보다는, vbs 또는 js 파일에서 좀 더 많은 기능을 구현할 수 있으며,

가장 최근에 나온 파워쉘은 가장 많은 기능을 구현할 수 있다.

 

Shell 프로그램들에는, cmd.exe, wscript.exe, cscript.exe, command.com 등의 파일이 있다.

이 프로그램들은 윈도우화면>시작버튼>명령어입력 부분에서 실행시키면 된다. 

         (단 cscript.exe는, 먼저 cmd.exe를 실행시킨 후의 도스창에서 실행시키면 된다.)

cmd.exe : 32비트로 동작하는 프로그램이다.  확장자가 .bat인 파일을 실행시킬 수 있다.

cscript.exe  : wscript.exe와 동일하지만, 도스창에서 실행이 된다.

wscript.exe  : cscript.exe와 동일하지만, 윈도우에서 실행이 된다.

command.com : 16비트로 동작하는 프로그램이다. 윈도우 OS에서 실행시키면, 도스 에뮬레이션상에서 동작이 된다.

                        이 프로그램은 사용하지 않는 것을 추천한다.

 

 

<환경변수에 대하여>

윈도우 OS는 부팅을 하면, 자체적으로 설정된 환경변수들을 가지고 있다.

cmd.exe를 실행시켜 도스창을 열면, 이 도스창은 윈도우OS의 자체 환경변수들을 물려 받는다.

도스창에서 >set 이라고 치면, 윈도우OS로 부터 물려받은 환경변수들을 확인할 수가 있다.

도스창에서 >set A=B 라는 식으로 환경변수들을 추가로 설정할 수가 있는데,

이 추가된 환경변수들이 영향을 미치는 곳은, 현재 이 도스창 뿐이다.

따라서 이 도스창을 종료하면 추가된 환경변수는 undefined 상태가 된다.

도스창에서 배치파일을 실행시키면, 이 배치파일내에서 정의된 환경변수들은, 배치파일이 끝나도 현재의 도스창에서 계속 유효하다.

배치파일내에서, 일부 영역에서만 특정 환경변수들의 정의를 유효화 시키고 싶다면,

   setlocal

      배치파일 내용

   endlocal

이런 식으로 하면 된다.

그러면 setlocal과 endlocal 사이에서 정의된 환경변수들은, 그 안에서만 유효하다.

 

 

아래의 설명은 가장 간단한 배치파일인 .bat 파일에 대해서 설명한다.

명령어 갯수가 많지 않아서 그나마 가장 배우기 쉽지만,

이 명령어들과 조건문들을 어떻게 결합하느냐에 따라서 대단히 다양하고 복잡한 기능들을 구현할 수 있다.

< .BAT 파일의 문법과 예제 >

출처 : http://www.robvanderwoude.com/batchfiles.php

출처 : http://computerhope.com/batch.htm

배치파일에서는, 우리가 도스창에서 즐겨 사용하는 copy, del, dir, cd, format, fdisk 등등 이외에도

다음의 명령어들과 문법들을 사용할 수가 있다.

 

ECHO : 배치파일의 내용은 한 줄씩 실행이된다.

             또한 배치파일이 실행이 될 때, 무조건 echo가 on 상태에서 배치파일의 처음 부분이 실행이 된다. 

             echo on 상태 : 한 줄이 실행될 때 마다, 그 줄의 내용을 화면에 뿌려준다.

             echo off 상태 : 줄이 실행될 때, 그 줄의 내용을 화면에 뿌리지 않는다.

                                   단, 줄의 실행된 내용이 출력이 있는 경우에는 당연히 출력을 화면에 뿌려준다.

      echo on : 이 라인 다음의 라인부터 echo-on 상태가 된다.

      echo off : 이 라인 다음의 라인부터 echo-off 상태가 된다.

      echo 문장 : 문장을 화면에 출력한다.

      echo. :  empty line을 출력한다.

      echo %undefined1%  : 만일 undefined1이라는 변수가 정의되어 있지 않은 상태라면,

                                           화면에 ECHO가 정의되어 있지 않습니다. 라는 메시지가 출력이 된다.

 

 

@ : 이것이 붙어 있는 라인은, echo-off 상태에서 그 라인이 실행이 된다.

      즉 그 줄의 내용 자체는 화면에 표시가 안되고, 실행된 출력만이 화면에 표시가 된다.

 

:LABEL  : 라벨을 표시한다. GOTO와 함께 사용이 되어, 특정 래이블로 제어를 점프하기 위해 사용한다.

 

%숫자  : 도스창의 프롬프트에서 배치파일을 실행시킬 때의 인자들을, 배치파일내에서 사용하기 위함이다.

               첫번째 인자를 %0, 두번째 인자를 %1, ....이런식으로 받아들인다. 따라서 %0~%9 까지 받아들인다. 

               여기서 %0는 파일이름을 받는다.

 

SHIFT : 도스창의 프롬프트에서 배치파일을 실행시킬 때의 인자들은, %0~%9 까지 받아들인다고 설명을 했다.

             그런데 이 shift 명령을 한 번 실행시키면, 명령어 인자들이 전체적으로 하나씩 쉬프트되어서,

             %0=(파일이름을 제외한)첫번째 인자, %1=두번째 인자...이런식으로 된다.

             따라서 인자들이 9개 이상이 되어도, shift 명령을 통해서, 프로그램내에서 모두 사용할 수가 있다.

      shift /n  : 여기서 n은 0~8.  n-1번째 까지는 그대로 두고, n = n+1, n+1 = n+2... 이런식이다.

                      즉, n-1번째 까지는 그대로 두고, n번째 인자가 사라지고, n+1과 그 이후가 한 칸씩 왼쪽으로 쉬프트된다.

 

      모든 인자를 사용하는 더 간단한 방법 :

            FOR %%A IN (%*) DO ( .... Now your batch file handles %%A instead of %1 ....)  : 단, 이 때 %*는 %1부터 시작한다.
           

조건실행 : 

        & :    command1 &  command2  : command1의 실행이 끝나고, command2를 실행한다.

        && :  command1 && command2  : command1의 실행이 성공적으로 끝나는 경우에만, command2를 실행한다.

        || :   command1 || command2  : command1의 실행이 실패로 끝나는 경우에만, command2를 실행한다.

 

CALL : 배치파일내에서 다른 배치파일을 호출하기 위함이다.

 

SET : http://computerhope.com/sethlp.htm

        SET : 현재 설정되어 있는 모든 환경변수들과 그 값들을 보여준다.

        SET variable :  현재 설정되어 있는 variable의 값을 보여준다.   화면에 variable=설정값 형태로 출력이 된다. 

                                 만일 variable이 설정되어 있지 않다면, "variable가 설정되어있지 않다"라는 메시지를 보여준다.

        SET variable=string  :  variable의 값을 string으로 설정한다.

              예) SET var1=%var2%    :  var2가 undefined 상태라고 하자. 그러면 var1도 undefined상태가 된다.

                    SET var3=     :  var3는 undefined 상태가 된다.

        SET /A variable=numerical expression : string위치에 오는 것은 문자열이 아니라, 수식이다.   

                  수식의 우선순위는 다음과 같다. (위쪽일 수록 높은 우선순위)

                  ()                 grouping

                  * / %            arithmetic operators

                  + -                arithmetic operators

                  << >>           logical shift

                  &                  bitwise and

                  ^                  bitwise exclusive or

                  |                  bitwise or

                   = *= /= %= += -=              assignment
                   &= ^= |= <<= >>=

                   ,                                   expression separator

 

        SET /P variable=[promptString] : 사용자가 값을 입력할것을 요구를 하고, 입력된 값은 variable로 들어간다.

              예) SET /P tempvar1=[원하는 수식을 입력하세요:]    : 사용자가 3+4엔터 를 입력했다고 하자.
                    SET /A tempvar2=%tempvar1%                         : 3+4가 계산이 되어 tempvar2에 입력이 된다.
                    Echo 계산된 값은

                    SET tempvar2                                                : 7이 출력이 된다. (주의! tempvar2 다음에 바로 줄바꿈이 와야 한다.)

 

 

IF 문 : 

IF[NOT] ERRORLEVEL number command
IF[NOT] string1==string2 command
IF[NOT] EXIST filename command

NOTSpecifies that Windows should carry out the command only if the condition is FALSE
ERRORLEVEL  number  

Specifies a TRUE condition

if the last program run returned an exit code equal to or greater than the number specified.

string1==string2Specifies a TRUE condition if the specified text strings match.
EXIST filenameSpecifies a TRUE condition if the specified filename exists.
commandSpecifies the command to carry out if the condition is met.
command can be followed by ELSE command2 which will execute command2 if the specified condition is FALSE

 

IF [/I] string1 compare-op string2 command                ( /I 를 붙이면  case-insensitive 문자열 비교가 된다.) 
where compare-op may be one of :

       EQU -equal          (or   string1 == string2 )  
       NEQ-not equal
       LSS-less than
       LEQ-less than or equal
       GTR-greater than
       GEQ-greater than or equal

 

IF DEFINED variable command    : variable이 정의가 되어 있다면, command를 실행한다.

 

 

EXIT : exit를 실행시키면, cmd.exe까지 모두 종료시켜버린다.

           만일 배치파일만 종료를 시키고 싶다면, Exit /B를 하면 된다.

  

Comments 처리 : 배치파일내에, 코드가 아닌, 단순한 설명문을 넣고자 할 때 다음과 같은 여러가지 방법이 있다.

         ( http://www.robvanderwoude.com/comments.php)

         아래와 같은 방법으로 사용하면 된다.

         REM comments   

         :unused_label comments    :  사용하지 않는 label을 지정하고, 그 뒤에 설명을 달면 된다.    

         :: comments                        :  사용하지 않는 label로써, :를 사용한 것 뿐이다.

         (위의 3가지 방법들은 모두, leading spaces를 허용한다. 그 외의 것은 위의 문장들 앞에 올 수가 없다.)

         그런데  :unused_label ::는 조심을 해야 할 점이 있다. 그것은 For문의 command블록 또는  IF문의 command블록안에서

         이 것들을 사용하게 되면 에러가 발생을 하기 때문이다.

         이럴때는 이 것들을 아예 For문 또는 IF문 밖으로 빼 내거나  또는  REM문을 사용하면 된다.

 

 

 

DEL : 지정한 파일들을 삭제한다.

DEL[/P] [/F] [/S] [/Q] [/A[[:]attributes]] [[drive:][path]filename
ERASE[/P] [/F] [/S] [/Q] [/A[[:]attributes]] [[drive:][path]filename

[[drive:][path]filename    Specifies the file(s) to delete.  Specify multiple files by using wildcards.
/PPrompts for confirmation before deleting each file.
/FForce deleting of read-only files.
/SDelete specified files from all subdirectories.
/QQuiet mode, do not ask if ok to delete on global wildcard
/ASelects files to delete based on attributes
attributes   AFiles ready for archiving
 HHidden files
 RRead-only files
 SSystem files
 -Prefix meaning not

del 다음에 파일이름이 아니고, 디렉토리이름만 지정해 주면, 그 디렉토리안의 모든 파일들을 삭제시킨다.

 

 

Redirection : 프로그램은 기본적으로 stdin(0)은 키보드, stdout(1)은 화면, stderr(2)은 화면으로 설정이 된다.

                      stdout과 stderr의 차이는, stdout은 버퍼를 거쳐서 화면에 뿌려진다.

                      따라서 버퍼가 다 차기 전까지는 화면에 뿌려지지 않는다.

                      반면에 stderr은 버퍼를 사용하지 않기 때문에, 출력할 데이터는 바로 바로 화면에 뿌려진다.

                      이렇게 기본적으로 설정된 것들을 사용하지 않고, 다르게 사용하고 싶을때 재지향 기호 > 를 사용한다.

              command1  >  filename  :  command1의 stdout출력을 화면이 아니라,  filename으로 보낸다.

                                                         command1의 stderr은 기존처럼 화면에 뿌려진다.

              command1  <  filename  :  command1의 입력을 키보드로부터 받는것이 아니라, filename으로 부터 받는다.

              command1  <  infile > outfile : 키보드가 아니라 infile로 부터 입력을 받고, stdout을 화면이 아니라 outfile로 출력을 한다.

                                                                command1의 stderr은 기존처럼 화면에 뿌려진다. 

              command1 2> file  :  command1의 stdout은 화면으로, stderr은 file로 보낸다.

              command1 > file 2>&1     :    command1의 stdout은 file로, stderr은 stdout으로 보낸다.

                                                          즉 stdout과 stderr모두 file로 보낸다.

                       이 문장을 간단하게 표기하는 또 다른 방법은 command1 &>file  command1 >&file이다.

              command1 >nul : command1의 stdout출력을, 화면에 뿌리는 것이 아니라, 그냥 버린다.

 

        파이프 :   command1 | command2     : command1의 stdout출력을 command2의 입력으로 보낸다. 

                      이것은  command1 > tempfile,   command2 < tempfile, del tempfile 과 동일하다.

                      command1, command2의 stderr은 기존처럼 화면에 뿌려진다.

              command1   2>& | command2   :    command1의 stdout과 stderr을 모두, command2의 stdin으로 보낸다.

 

 

RMDIR [/S] [/Q] [drive:]path
RD       [/S] [/Q] [drive:]path
    /S      Removes all directories and files in the specified directory in addition to the directory itself. 

             Used to remove a directory tree.
    /Q      Quiet mode, do not ask if ok to remove a directory tree with /S

 

 

 

EXIT : exit를 실행시키면, cmd.exe까지 모두 종료시켜버린다.

           만일 배치파일만 종료를 시키고 싶다면, Exit /B를 하면 된다.

 

 

 

 

 

XCOPY : 

XCOPY

 

source [destination] [/A | /M] [/C] [/D[:date]] [/F] [/H] [/I] [/K] [/L] [/N] [/P] [/Q] [/R] [/S [/E]] [/T] [/U] [/V] [/W] [/Z] [/EXCLUDE:filename]

source
Specifies the file(s) to copy.
destination
Specifies the location and/or name of new files.
/A
Copies files with the archive attribute set, doesn't change the attribute.
/C
Continues copying even if errors occur.
/D[:m-d-y]
Copies files changed on or after the specified date.
If no date is given, copies only those files whose source time is newer than the destination time.
/E
Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/F
Displays full source and destination file names while copying.
/H
Copies hidden and system files also.
/I
If destination does not exist and copying more than one file, assumes that destination must be a directory.
/K
Copies attributes. Normal Xcopy will reset read-only attributes.
/L
Displays files that would be copied (list only).
/M
Copies files with the archive attribute set, turns off the archive attribute.
/N
Copies using the generated short names.
/P
Prompts you before creating each destination file.
/Q
Does not display file names while copying.
/R
Overwrites read-only files.
/S
Copies directories and subdirectories except empty ones.
/T
Creates directory structure, but does not copy files.
Does not include empty directories or subdirectories.
/T /E includes empty directories and subdirectories.
/U
Copies only files that already exist in destination.
/V
Verifies each new file.
/W
Prompts you to press a key before copying.
/Z
Copies networked files in restartable mode.
/EXCLUDE:filename
Excludes the files listed in the specified file from the copy operation. The exclusion file can have a list of exclusion patterns (one per line, no wild card characters are supported). If any exclusion pattern in the file matches any part of the path of a subject file, that file is not copied.

 

 

  [출처] 배치파일 : Batch file (or Shell Script) |작성자 hypalgesia3

Posted by 요지
,