相关疑难解决方法(0)

采购('.' 或 'source')和在 bash 中执行文件有什么区别?

执行这样的脚本有什么区别:

./test.sh

并执行这样的脚本:

. test.sh?

我尝试了一个简单的两行脚本,看看是否能找到不同之处:

#!/bin/bash
ls
Run Code Online (Sandbox Code Playgroud)

但两者. test.sh./test.sh返回相同的信息。

shell bash

88
推荐指数
2
解决办法
2万
查看次数

如何使子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 ×2

shell ×2

io-redirection ×1

subshell ×1