如何在单一引用中使用主机名变量



在名为" input_data"的bash文件中

LA=$1
curl -H 'Content-Type: application/x-ndjson' -XPOST '"'$LA'":9200/human_flow/human_flow/_bulk?pretty' --data-binary @$2.json

运行命令

 ./input_data localhost human_flow

给出错误消息

bash-3.2$ ./input_data localhost human_flow
curl: (6) Could not resolve host: "localhost"

可以解决本地主机

bash-3.2$ ping localhost
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.067 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.156 ms

使用127.0.0.1代替localhost

bash-3.2$ ./input_data 127.0.0.1 human_flow
curl: (6) Could not resolve host: "127.0.0.1"

正确用法看起来像:

curl 
  -H 'Content-Type: application/x-ndjson' 
  -XPOST 
  --data-binary "@$2.json" 
  "$LA:9200/human_flow/human_flow/_bulk?pretty"
  • 所有扩展都是双引号 - 既不是单个引号(这阻止了它们被扩展(,也不是未引用的。
  • 双引号如果期望具有语义含义,则不得在单引号中。
  • 通过POSIX公约,应在所有可选参数之后指定位置参数。GNU约定后的申请并不严格要求此事,但是在整个板上之后避免了惊喜。

最新更新