stdout에서 출력하기 전에 명령줄 텍스트 삽입(파이핑/리디렉션용)

stdout에서 출력하기 전에 명령줄 텍스트 삽입(파이핑/리디렉션용)

다음 bash구조를 고려하십시오.

ls /usr/include/asm > list-redir.txt
ls /usr/include/asm | tee list-tee.txt

이 경우 list-redir.txtlist-tee.txt는 동일하며 예상되는 파일 목록을 포함합니다.

$ head -5 list-redir.txt
a.out.h
auxvec.h
bitsperlong.h
boot.h
bootparam.h [...]

내 질문은 - 어떻게 이런 명령을 작성하고 명령줄 텍스트를 표준 출력의 첫 번째 항목으로 삽입하여 파일이 명령줄로 시작하게 할 수 있습니까? 예를 들어 list-redir.txt이 경우 파일은 다음과 같습니다.

$ head -5 list-redir.txt
# ls /usr/include/asm
a.out.h
auxvec.h
bitsperlong.h
boot.h [...]

...이는 삽입된 명령줄 앞에 # 문자를 추가할 수 있음을 의미합니다.

ls /usr/include/asm > list-redir.txt원래 명령줄( ...) 을 입력하는 측면에서 최소한의 변경만으로 사용할 수 있는 것이 있습니까 ?

답변1

간단하고 추악한 트릭은 다음을 추가하는 것입니다 ~/.bashrc.

echorun(){
    echo "# $@";
    "$@"
}

그런 다음 명령을 실행할 수 있습니다

echorun ls /usr > list-redir.txt

ls /usr >foo이렇게 하면 과 를 구별할 수 없지만 의 시작 부분에 ls /usr | tee foo추가됩니다 .# ls /usrfoo

답변2

다음을 수행할 수 있습니다.

{   cmd="ls /usr/include/asm"
    echo "$cmd" ; $cmd
} >./list-redir.txt

적어도 나는 그것이 당신이 하고 싶은 일이라고 생각합니다. 그러면 다음과 같은 결과가 생성됩니다.

 $ cat <./list-redir.txt

 ###OUTPUT###

   ls /usr/include/asm
 #output of above command#
   ...

답변3

script입력한 내용과 출력을 포함하여 터미널 세션을 기록하는 명령을 볼 수도 있습니다 . 그러나 백스페이스 키 등을 포함하여 입력하는 모든 내용을 기록하므로 때로는 다소 혼란스러울 수 있습니다.

$ script
Script started, file is typescript
$ ls /usr/include/asm
a.out.h          ioctl.h      mtrr.h             setup.h         termios.h
auxvec.h         ioctls.h     param.h            shmbuf.h        types.h
bitsperlong.h    ipcbuf.h     poll.h             sigcontext32.h  ucontext.h
boot.h           ist.h        posix_types_32.h   sigcontext.h    unistd_32.h
bootparam.h      kvm.h        posix_types_64.h   siginfo.h       unistd_64.h
byteorder.h      kvm_para.h   posix_types.h      signal.h        unistd.h
debugreg.h       ldt.h        prctl.h            socket.h        vm86.h
e820.h           mce.h        processor-flags.h  sockios.h       vsyscall.h
errno.h          mman.h       ptrace-abi.h       statfs.h
fcntl.h          msgbuf.h     ptrace.h           stat.h
hw_breakpoint.h  msr.h        resource.h         swab.h
hyperv.h         msr-index.h  sembuf.h           termbits.h
$ exit
exit
Script done, file is typescript
$ cat typescript
Script started on Sat 29 Aug 2015 10:32:52 AM EDT
$ ls /usr/include/asm
a.out.h          ioctl.h      mtrr.h             setup.h         termios.h
auxvec.h         ioctls.h     param.h            shmbuf.h        types.h
bitsperlong.h    ipcbuf.h     poll.h             sigcontext32.h  ucontext.h
boot.h           ist.h        posix_types_32.h   sigcontext.h    unistd_32.h
bootparam.h      kvm.h        posix_types_64.h   siginfo.h       unistd_64.h
byteorder.h      kvm_para.h   posix_types.h      signal.h        unistd.h
debugreg.h       ldt.h        prctl.h            socket.h        vm86.h
e820.h           mce.h        processor-flags.h  sockios.h       vsyscall.h
errno.h          mman.h       ptrace-abi.h       statfs.h
fcntl.h          msgbuf.h     ptrace.h           stat.h
hw_breakpoint.h  msr.h        resource.h         swab.h
hyperv.h         msr-index.h  sembuf.h           termbits.h
$ exit
exit

Script done on Sat 29 Aug 2015 10:33:00 AM EDT

답변4

er실제로 @terdon의 답변이 더 복잡한 명령 파이프라인에서는 작동하지 않을 수 있다는 것을 깨달았으므로 다음 별칭( @tendon의 약어로 명명됨 echorun) 을 생각해냈습니다 .

#alias er=' cat <(echo "# cmd: $(history 1)") - | tee' # last tee is not needed, so:
alias er=' cat <(echo "# cmd: $(history 1)") -'

기본적으로 마지막 파이프 또는 리디렉션 직전에 명령줄에 삽입해야 한다는 아이디어가 있습니다. 이 시점에서 현재 명령줄을 참조하는 것은 |er정말 행운입니다 . history 1따라서 나머지 표준 입력(그 당시의 내용) 이전에 먼저 에코될 수 있습니다 cat. 이제 다음을 수행할 수 있습니다.

$ ls /usr/include/asm | grep 'p*.h' | grep 'osix' |er | tee mylist.txt
# cmd:   125  ls /usr/include/asm | grep 'p*.h' | grep 'osix' |er | tee mylist.txt
posix_types_32.h
posix_types_64.h
posix_types.h
$ ls /usr/include/asm | grep 's*.h' | grep 'ig' |er >> mylist.txt
$ cat mylist.txt 
# cmd:   125  ls /usr/include/asm | grep 'p*.h' | grep 'osix' |er | tee mylist.txt
posix_types_32.h
posix_types_64.h
posix_types.h
# cmd:   126  ls /usr/include/asm | grep 's*.h' | grep 'ig' |er >> mylist.txt
sigcontext32.h
sigcontext.h
siginfo.h
signal.h

따라서 우리는 여러 파이프에 대한 전체 명령줄을 갖고 있으며 어떤 것도 이스케이프 처리하는 것에 대해 걱정할 필요가 없습니다. 기본적으로 er마지막 파이프에 추가하기만 하면 됩니다. 유일한 사소한 문제는 기록 번호 매기기입니다(별로 신경쓰이지 않습니다. 그렇지 않으면 awk별칭에 추가 값을 추가했을 것입니다).

관련 정보