다음 bash
구성을 고려하십시오.
ls /usr/include/asm > list-redir.txt
ls /usr/include/asm | tee list-tee.txt
이 경우 list-redir.txt
및 list-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 /usr
foo
답변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
실제로 @terdon의 답변이 더 복잡한 명령 파이프라인에서는 작동하지 않을 수 있다는 것을 깨달았습니다. 그래서 나는 다음 별칭을 생각해 냈습니다( er
@tendon's의 약어로 명명됨 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
별칭에 추가 항목을 추가합니다).