导出的日期变量未出现在子 shell 中

导出的日期变量未出现在子 shell 中

我目前正在使用两个不同的脚本创建 2 个 bash,这会产生两个使用相同时间戳命名的文件,以便稍后进行比较/处理。日期变量在第一个 sh 中创建,我尝试创建一个环境以将其传递到第二个 sh 中,以便它可以使用相同的时间戳。我在用着暂停因为第二个脚本是一个循环进程,它不断向文件输入数据,直到被杀死。

1.sh

current_date_time="`date +%Y-%m-%d-%H:%M:%S`"
code 
OUTPUT=$current_date_time --ouput-format csv
(export current_date_time; sudo timeout 15 2.sh)

2.sh

echo "The variable is $current_date_time"
call loop
  code
  OUTPUT=$current_date_time.txt
loop

第一个脚本正常运行,而第二个脚本仅产生该短语,The variable is而输出文件无法创建。我的怀疑是超时,但任何想法将不胜感激。

答案1

如中所述man sudoers

   Command environment
     Since environment variables can influence program behavior, sudoers pro‐
     vides a means to restrict which variables from the user's environment are
     inherited by the command to be run.  There are two distinct ways sudoers
     can deal with environment variables.

     By default, the env_reset option is enabled.  This causes commands to be
     executed with a new, minimal environment.

sudo您可以通过使用-E或选项调用来请求保留调用环境--preserve-env

sudo -E timeout 15 2.sh

(尽管这可能会不成功,如果 sudoers 政策禁止的话)。

相关内容