이름에 공백과 대괄호가 있는 파일 읽기 -CLI

이름에 공백과 대괄호가 있는 파일 읽기 -CLI

> abc (1).bin파일 이름 이나 > abc (1).txt파일이 있으면 어떻게 읽는지 알고 싶습니다 . 파일에는 .bin또는 확장자 앞에 공백과 대괄호가 있습니다 .txt.

.bin 파일을 읽으려면 도구가 있는데 파일 이름에서 "공백과 (1)"을 제거하면 쉽게 읽을 수 있습니다. 그런데 이 공백과 괄호 이름이 있으면 (1).bin읽을 수 없습니다.

cat파일 을 사용하면 .txt작동하지만.. 파일에서는 작동하지 않습니다 .bin. 요청된 테스트는 다음과 같습니다.

$ cat full_logs-10.2.0.103-2018.02.07\ \(1\).txt 
hello,
this is a test.

--------xxxxxxx------xxxxxxx------------xxxxxxx--------

$ LogAnalyzeRebirth -p -x ./ full_logs-2018.02.07\ \(1\).bin 
usage: LogAnalyzeRebirth [-h] [-A] [-B] [-C] [-D] [-E] [-F]
                         [-G GRAPH [GRAPH ...]] [-H] [-I [HISTOGRAM]] [-L]
                         [-M] [-N] [-P [PDF]] [-R] [-S] [-T] [-U] [-V] [-b]
                         [-c] [-e] [--moo] [-f] [-g] [-i] [-k] [-l] [-m] [-n]
                         [-o] [-p PATH] [-q] [-r] [-s] [-t] [-v] [-x EXTRACT]
                         [-z]
LogAnalyzeRebirth: error: argument -p/--path: expected 1 argument(s)

--------xxxxxxx------xxxxxxx------------xxxxxxx--------

$ LogAnalyzeRebirth.py -p ./ -x "full_logs-10.2.0.103-2018.02.07 (1).bin" 

(\ /)                      (\ /)
( . .)      LogAnalyzeRebirth     (. . )
c(")(")                  (")(")o

sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
Extract failed.
LogAnalyzeRebirth can't find full_logs-10.2.0.103-2018.02.07 (1).bin
No such file or directory : ./full_logs-10.2.0.103-2018.02.07 (1)/dmesg

---   Firmware_version   ---

No such file or directory : ./full_logs-10.2.0.103-2018.02.07 (1)/version.txt

--------xxxxxxx------xxxxxxx------------xxxxxxx--------

답변1

댓글의 토론에서는 파일 이름을 매개변수로 사용하여 다른 스크립트나 프로그램을 호출하는 스크립트나 프로그램에 이상한 파일 이름을 전달해야 한다는 점을 보여주었습니다. 따라서 파일 이름을 따옴표로 묶는 것만으로는 충분하지 않습니다. 쉘이 이러한 따옴표를 제거하고 다음 호출에서 따옴표 없이 파일 이름을 전송하여 사용할 수 없게 되기 때문입니다.

doLogAnalyze그래서 내 생각은 다음과 같이 LogAnalyzeRebirth.py에 대한 래퍼 스크립트를 사용하는 것입니다 .

#!/bin/bash
tmpfile=$(mktemp /tmp/LogAnalyzeRebirth.XXXXXX) # create temporary file
cp "$1" "$tmpfile"                              # copy to temporay file
LogAnalyzeRebirth.py -p ./ -x "$tmpfile"        # analyze copy
rm "$tmpfile"                                   # delete copy

호출은 ./doLogAnalyze "full_logs-10.2.0.103-2018.02.07 (1).bin"기본 프로그램 내에서 사용되는 다른 프로그램 수에 관계없이 작업을 수행해야 합니다. OP의 희망대로 원본 파일은 변경되지 않습니다.

관련 정보