![sh의 "${x%% *}"는 무엇을 의미하나요? [복사]](https://linux55.com/image/44760/sh%EC%9D%98%20%22%24%7Bx%25%25%20*%7D%22%EB%8A%94%20%EB%AC%B4%EC%97%87%EC%9D%84%20%EC%9D%98%EB%AF%B8%ED%95%98%EB%82%98%EC%9A%94%3F%20%5B%EB%B3%B5%EC%82%AC%5D.png)
방금 makefile에서 "$${x%% *}"를 보았습니다. 이는 sh에서 "${x%% *}"를 의미합니다. 왜 이런 글을 쓰나요?
makefile은 로컬 컴퓨터에서 명령을 사용할 수 있는지 어떻게 감지합니까?
determine_sum = \
sum=; \
for x in sha1sum sha1 shasum 'openssl dgst -sha1'; do \
if type "$${x%% *}" >/dev/null 2>/dev/null; then sum=$$x; break; fi; \
done; \
if [ -z "$$sum" ]; then echo 1>&2 "Unable to find a SHA1 utility"; exit 2; fi
checksums.dat: FORCE
$(determine_sum); \
$$sum *.org
답변1
이것은 POSIX 쉘 변수 대체 함수입니다:
${var%Pattern} Remove from $var the shortest part of $Pattern that matches the back end of $var.
${var%%Pattern} Remove from $var the longest part of $Pattern that matches the back end of $var.
그러므로 만일var="abc def ghi jkl"
echo "${var% *}" # will echo "abc def ghi"
echo "${var%% *}" # will echo "abc"