![쉘 스크립트를 사용하여 mysql 쿼리에서 동적 변수를 전달하는 방법](https://linux55.com/image/197523/%EC%89%98%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20mysql%20%EC%BF%BC%EB%A6%AC%EC%97%90%EC%84%9C%20%EB%8F%99%EC%A0%81%20%EB%B3%80%EC%88%98%EB%A5%BC%20%EC%A0%84%EB%8B%AC%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95.png)
이것은 내 코드의 일부입니다.
sample_1=''
sample_1_is_cancelled=''
sample_2=''
sample_2_is_cancelled=''
sample_3=''
sample_3_is_cancelled=''
sample_4=''
sample_4_is_cancelled=''
sample_5=''
sample_5_is_cancelled=''
while read -r insert
do
eval sample_$i=$(echo $insert| awk -F'|' '{print $1}')
eval sample_$i_is_cancelled=$(echo $insert| awk -F'|' '{print $2}')
i=$(( i + 1 ))
done < $logpath/source.txt
mysql -uroot -p -e" insert into ttable(sample_1, sample_1_is_cancelled, sample_2, sample_2_is_cancelled, sample_3, sample_3_is_cancelled, sample_4, sample_4_is_cancelled, sample_5, sample_5_is_cancelled)
values($sample_1, $sample_1_is_cancelled, $sample_2 $sample_2_is_cancelled, $sample_3, $sample_3_is_cancelled, $sample_4, $sample_4_is_cancelled, $sample_5, $sample_5_is_cancelled);"
최대 5개의 값 세트가 있을 수 있습니다. 적어도 한 세트.
아래와 같은 변수를 에코할 수 있습니다.
eval echo \$sample_$i
eval echo \$sample_${i}_is_cancelled
하지만 같은 방식으로 삽입 쿼리 외부로 전달할 수는 없습니다. 어떤 제안이라도... 도와주세요.
답변1
다음은 두 개의 배열("필드"와 "값")을 사용하여 이 작업을 수행하는 방법에 대한 예입니다.
#!/bin/bash
declare -a fields values
infile="./source.txt"
#infile="$logpath/source.txt"
i=0
while read -r insert; do
# split "$insert" into a and b, using | as delimiter
a="${insert%|*}"
b="${insert#*|}"
# create the field names from the loop counter $i
let i++
sfield="sample_$i"
cfield="sample_${i}_is_cancelled"
fields+=("$sfield" "$cfield")
values+=("$a" "$b")
done < "$infile"
# show what's in the arrays:
declare -p fields
echo
declare -p values
# now build the SQL string, in parts:
# field names don't need to be quoted
f=$(printf "%s, " "${fields[@]}" | sed -e 's/, $//')
# this assumes values are strings and need to be quoted
v=$(printf "'%s', " "${values[@]}" | sed -e 's/, $//')
sql="$(printf "insert into ttable(%s) values (%s);" "$f" "$v")"
echo
echo "mysql -uroot -p -e \"$sql\""
다음 sources.txt
파일이 제공됩니다.
$ cat source.txt
one|two
three|four
foo|bar
junk|more junk
스크립트를 실행하면 다음과 같은 출력이 생성됩니다.
declare -a fields=([0]="sample_1" [1]="sample_1_is_cancelled" [2]="sample_2"
[3]="sample_2_is_cancelled" [4]="sample_3" [5]="sample_3_is_cancelled"
[6]="sample_4" [7]="sample_4_is_cancelled")
declare -a values=([0]="one" [1]="two" [2]="three" [3]="four"
[4]="foo" [5]="bar" [6]="junk" [7]="more junk")
mysql -uroot -p -e "insert into ttable(sample_1, sample_1_is_cancelled, sample_2,
sample_2_is_cancelled, sample_3, sample_3_is_cancelled,
sample_4, sample_4_is_cancelled) values ('one', 'two', 'three', 'four',
'foo', 'bar', 'junk', 'more junk');"
(가독성을 높이기 위해 줄바꿈과 들여쓰기를 추가하세요)
참고: 쉘 스크립트 자체의 필드 이름이나 값으로 더 많은 작업을 수행해야 하는 경우(예: SQL 삽입 문에서만 사용하는 것이 아니라) 두 개의 연관 배열(샘플용 하나, 하나는 샘플용)을 사용하는 것이 좋습니다. 취소 샘플의 경우) $sfield 및 $cfield 변수를 이러한 배열의 키로 사용합니다. 나는 이와 같은 스크립트를 작성하기 시작했지만 작업이 너무 복잡하다는 것을 깨달았고(SQL 문자열을 구성하기 위해 필드와 값을 병합하는 데 더 많은 작업이 필요함) 인덱스 배열 $fields 및 $values를 사용하여 단순화했습니다.