简体   繁体   English

Shell脚本(分配给变量)不起作用

[英]Shell script (assign to variable) not working

#!/bin/bash
if [ -f "$1" ] #parameter is file name, if i exists...
then
    VARIABLE=`cat "$1"` #assign what's inside into VARIABLE
fi

I've tried bash -x script_01.sh "file" and the tracing works: 我已经尝试了bash -x script_01.sh "file" ,并且跟踪工作正常:

+ '[' -f file ']'
++ cat file
+ VARIABLE='Wednesday, November  4, 2015 04:45:47 PM CET'

But the problem is that when I echo $VARIABLE, it's empty. 但是问题是当我回显$ VARIABLE时,它是空的。 Do you guys have any tips on how to make it work? 你们对如何使其运作有任何提示吗? Thank you. 谢谢。

VARIABLE is set in the process that runs the script, not the process which called the script. VARIABLE是在运行脚本的进程中设置的,而不是在调用脚本的进程中设置的。 If you want to set it in your current environment, you need to source the file instead. 如果要在当前环境中进行设置,则需要提供源文件。

. script_01.sh "file"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM