相关疑难解决方法(0)

如何使子shell中的变量在父shell中可用

我编写了一个快速而肮脏的脚本来计时来自 Web 服务的一些报告:

BASE_URL='http://example.com/json/webservice/'
FIRST=1
FINAL=10000

for report_code in $(seq 1 $FINAL); do
  (time -p response=$(curl --write-out %{http_code} --silent -O ${BASE_URL}/${report_code}) ) 2> ${report_code}.time

  echo $response  # <------- this is out of scope!  How do I fix that?
  if [[ $response = '404' ]]; then
    echo "Deleting report # ${report_code}!"
    rm ${report_code}
  else
    echo "${report_code} seems to be good!"
  fi
done
Run Code Online (Sandbox Code Playgroud)

我需要将time命令包装在一个子 shell 中,以便我可以重定向它的输出,但这使得$response父 shell的值不可用。我该如何解决这个问题?

shell bash io-redirection subshell

13
推荐指数
1
解决办法
8968
查看次数

标签 统计

bash ×1

io-redirection ×1

shell ×1

subshell ×1