다음과 같은 하위 디렉토리 이름이 있습니다.
Test라는 더 큰 디렉토리 내에 있습니다.
"ABCDEF _ ** _ "또는 Test_ 이후의 모든 항목을 효율적으로 꺼내서 다음을 얻으려면 어떻게해야합니까?
위의 내용은 차이가있는 경우 파일이 아니라 폴더입니다.
하위 디렉토리의 경로 이름을 반복하고 매개 변수 대체를 사용하여 초기 부분을 삭제합니다.
#!/bin/sh
for dirpath in Test/Test_*/; do
# Because of the pattern used above, we know that
# "$dirpath" starts with the string "Test/Test_",
# and this means we know we can delete that:
# But first delete that trailing /
dirpath=${dirpath%/}
shortname=${dirpath#Test/Test_}
printf 'The shortened name for "%s" is "%s"\n' "$dirpath" "$shortname"
done
대체 ${variable#pattern}
는 값 pattern
의 시작 부분 에서 (가장 짧은) 일치를 삭제하는 표준 대체입니다 $variable
.
마찬가지로 코드 ${variable%pattern}
에서 후행을 삭제하는 데 사용 /
되는는 값 pattern
의 끝 에서 가장 짧은 일치 항목을 삭제합니다 $variable
.
다음 디렉터리 트리에서 테스트 :
.
`-- Test/
|-- Test_ABCDEF_406_1/
|-- Test_ABCDEF_AOH_1/
`-- Test_ABCDEF_AUQ_1/
코드는
The shortened name for "Test/Test_ABCDEF_406_1" is "ABCDEF_406_1"
The shortened name for "Test/Test_ABCDEF_AOH_1" is "ABCDEF_AOH_1"
The shortened name for "Test/Test_ABCDEF_AUQ_1" is "ABCDEF_AUQ_1"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다