![문자열에 *를 추가하고 준비된 SQL 쿼리를 다른 변수에 할당하는 방법은 무엇입니까? 즉, (...에서 * 선택)](https://linux55.com/image/152083/%EB%AC%B8%EC%9E%90%EC%97%B4%EC%97%90%20*%EB%A5%BC%20%EC%B6%94%EA%B0%80%ED%95%98%EA%B3%A0%20%EC%A4%80%EB%B9%84%EB%90%9C%20SQL%20%EC%BF%BC%EB%A6%AC%EB%A5%BC%20%EB%8B%A4%EB%A5%B8%20%EB%B3%80%EC%88%98%EC%97%90%20%ED%95%A0%EB%8B%B9%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F%20%EC%A6%89%2C%20(...%EC%97%90%EC%84%9C%20*%20%EC%84%A0%ED%83%9D).png)
SQL 쿼리를 생성하여 변수에 할당하려고 하는데 할 수 없습니다.
TABLENAME=foo
QUERY="select * from $TABLENAME"
echo $QUERY
쿼리가 다음과 같을 것으로 예상합니다.select * from foo
대신 내가 얻는 것은select [file names in the directory] from foo
이것이 바로 원래 쿼리 대신 내가 얻은 것입니다.
foo에서 Anaconda3-5.3.1-Linux-x86_64.sh hadoop-2.7.3 hadoop-2.7.3.tar.gz script.sh를 선택하십시오.
답변1
변수를 인용해야 합니다.
echo "$QUERY"
그렇지 않으면 쉘이 *
현재 디렉토리의 파일로 확장됩니다.
쉘에서 시도해 echo "*"
보십시오 echo *
.
이는 *
쉘 기능의 일부입니다(아마도불다) 라고 한다경로명 확장.
Bash 매뉴얼 페이지에서:
특수 패턴 문자의 의미는 다음과 같습니다.
* Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdi- rectories. If followed by a /, two adjacent *s will match only directories and subdirectories.
이를 방지하려면 *
백슬래시 로 이스케이프하거나 큰따옴표 또는 작은따옴표 \
로 묶을 수 있습니다."
'
예:
# no pathname expansion
$ echo \*
*
$ echo "*"
*
$ echo '*'
*
# pathname expansion
$ echo *
file_x file_y file_z_in_this_directory
작은따옴표의 문제점은 변수 확장(매개변수 확장이라고도 함)을 방지한다는 것입니다. 모든 문자는 문자 그대로 해석됩니다.
# no variable expansion
$ echo '$QUERY'
$QUERY
# variable expansion
$ echo "$QUERY"
select * from foo
# variable expansion and pathname expansion
$ echo $QUERY
select file_x file_y file_z_in_this_directory from foo