
다음과 같은 디렉토리가 있습니다.
/path/to/files/data-file.1
/path/to/files/data-file.2
/path/to/files/data-file.3
/path/to/files/data-file.4
/path/to/files/data-file.5
/path/to/files/data-file.6
다음과 같은 변수의 두 값 사이에 있는 모든 파일을 나열하는 스크립트를 만들고 싶습니다.
#!/bin/bash
fileA="/path/to/files/data-file.2"
fileB="/path/to/files/data-file.5"
ls -v /path/to/files/*-file.[0-9]* | sed -n '/$fileA/,/$fileB/p'
출력 :
/path/to/files/data-file.2
/path/to/files/data-file.3
/path/to/files/data-file.4
/path/to/files/data-file.5
그러나 sed 명령이 작동하지 않는 것 같습니다. 변수의 슬래시 때문에 추측하고 있습니다. 변수를 변경하지 않고 이 문제를 극복할 수 있는 방법이 있습니까? fileA와 fileB가 동적인 스크립트에서 이 코드 비트를 사용하고 싶습니다.
답변1
슬래시가 혼동되지 않도록 처음 나타날 때 이스케이프하는 경우 다른 구분 기호를 사용할 수 있습니다 sed
. 그러나 작은따옴표를 사용하면 bash
변수가 대체되지 않으므로 예제에서는 변수가 확장되지 않으므로 문자 그대로 찾습니다 sed
. 변수를 사용하는 경우 큰따옴표를 사용해야 합니다.$fileA
$fileB
따라서 다음과 같이 작동해야 합니다.
#!/bin/bash
fileA="/path/to/files/data-file.2"
fileB="/path/to/files/data-file.5"
ls -v /path/to/files/*-file.[0-9]* | sed -n "\@$fileA@,\@$fileB@p'