2016-07-19 4 views
2

나는 fqdn과 hostname의 변수를 포함하는 텍스트 파일을 가지고있다. 파일 내가 bash는 스크립트에서 컬을 사용하여 일부 데이터를 업데이트해야이파일에서 컬 명령 데이터를 사용하는 방법?

first_fqnd first_hostname 
second_fqdn  second_hostname 
.....  ..... 

처럼 보이지만 나는이 텍스트 파일에서 FQDN 및 호스트 이름을 가지고 모든 쌍의 FQDN 및 호스트 이름의 컬을해야한다.

내 컬은 다음과 같이해야합니다 :

curl -H "Content-Type:application/json" -XPUT "https://pup.pnet.pl/api/hosts/**fqdn from file**" -d '{"host":{"name": "**hostname from file**"}}' --cacert bundle.pem --cert xxx-pem.cer --key xxx-privkey.pem 

가 어떻게 말릴 파일에서 이러한 변수를 전달할 수 있습니다

? 나는 AWK 사용에 대한 생각했지만, 나는

답변

5

두 개의 관련 변수, fqdnhostn으로 공백 문자로 구분 된 매개 변수 파일의 라인을 읽고 넣어 while 구조를 사용하여 curl 명령에서 사용하는 방법을 모른다 :

이 같은
while read fqdn hostn; do 
    curl -H .... -XPUT "https://pup.pnet.pl/api/hosts/${fqdn}" \ 
     -d '{"host":{"name": "'"${hostn}"'"}}' --cacert ....; done <file.txt 
+0

do 다음에 백 슬래시가 필요하지 않습니다. – Barmar

+0

@Barmar oops..typo..fixed .. – heemayl

2

시도 뭔가 :

#!/bin/bash 

while read fqdn hostname; do 
    curl -H "Content-Type:application/json" -XPUT \ 
     "https://pup.pnet.pl/api/hosts/${fqdn}" \ 
     -d '{"host":{"name": "'${hostname}'}}' --cacert bundle.pem \ 
     --cert xxx-pem.cer --key xxx-privkey.pem 
done <input_file.txt 

while read fqdn hostname은 "열"V로 강타의 내부 필드를 구분하여 분할, 표준 입력, 라인별로 라인에서 입력을합니다 ariables $fqdn$hostname입니다. 자세한 내용은 Catching User Input을 참조하십시오.