我有两个文件调用a_file和b_file。 我试图写a_file,以便它可以更改b_file中的variables。
echo "This is a_file" echo "Enter x to display msg1 or y to display msg2" read input
和
echo "This is b_file" if [ "$var1" = "x" ]; then echo "message 1" else echo "message 2" fi
你能告诉我如何操作variablesvar1
从a_file吗?
这是对哈吉克答案的更新。 这很清楚。 但是如果我想在a_file的第一次执行之后每分钟运行一次b_file? (crontab)我应该把它写成如下。
echo "This is a_file" echo "Enter x to display msg1 or y to display msg2" read x * * * * * ./b_file "$x"
和
echo "This is b_file" if [ "$1" = "x" ]; then echo "message 1" else echo "message 2" fi
最好的方法是从命令行传递参数:
echo "This is a_file" echo "Enter x to display msg1 or y to display msg2" read x ./b_file "$x"
和
echo "This is b_file" if [ "$1" = "x" ] echo "message 1" else echo "message 2" fi
关于修改:你可以使用sed
来修改b_file
。 cat b_file | sed "s/\\\$var/$x/g"
你可以使用环境变量。 在a_file中,导出变量var:
export var="Some_value"
在b_file中,可以像这样使用它:
echo $var
但是,您需要像下面的shell脚本a_file:
source a_file
我假设你在使用shell脚本。
您可以使用函数并在调用函数的文件中包含带有函数定义的文件。 然后这个函数可以带参数。 从阅读本文档开始,在bash中编写函数。