简体   繁体   English

具有环境变量的Shell脚本

[英]Shell Script with environment variable

I have a C program that has the following code at the end: 我有一个C程序,结尾处有以下代码:

    memcpy(buff,"TEST=",4)
    putenv(buff)
    system("/bin/bash")

I put a new environment variable in a new shell. 我在新的shell中放入了新的环境变量。 I want to then echo test into a text file which i can easily do. 然后,我想将测试回显到我可以轻松完成的文本文件中。 I then want to exit the shell and run it again so running this would look as follows 然后,我想退出外壳并再次运行它,因此运行如下

   ./cprog "some arg"
   echo $TEST > test.txt
   exit
   ./cprog "some arg"
   echo $TEST > test.txt
   exit

I have to keep exiting so I can change the TEST variable. 我必须继续退出,以便可以更改TEST变量。 How could I make this work in a shell script so i could just keep looping and running the c program so I could change the argument but never exiting the shell script itself. 我如何才能在shell脚本中完成此工作,以便我可以继续循环并运行c程序,以便可以更改参数,而从不退出shell脚本本身。

Environment variables are not an appropriate way of passing information from a process to the user or an invoking process. 环境变量不是将信息从流程传递给用户或调用流程的适当方法。 You should be writing this information to stdout (with any other info to stderr) and redirecting it appropriately. 您应该将此信息写入stdout(将其他任何信息写入stderr)并适当地重定向。

If you insist on this solution, though, you can use 如果您坚持使用此解决方案,则可以使用

for p in "one arg" "another arg" "more"
do 
    echo 'printf "%s\n" "$TEST" >> test.txt' | ./cprog "$p"
done

This will input the command to write the variable to the file to your program, and then exit the shell (because input ends). 这将输入命令以将变量写入文件到程序中,然后退出外壳程序(因为输入结束)。

At the end, you'll have all the values of $TEST concatenated in the file test.txt . 最后,您将在文件test.txt连接所有$TEST的值。

It isn't possible for your program to change the environment of the parent process, which is what you seem to be trying to do. 您的程序不可能更改父进程的环境,这似乎是您试图做的。

What you can do instead, however, is to have your program simply print the desired output, and call it using eval . 但是,您可以做的是让您的程序仅打印所需的输出,然后使用eval调用。

 $ ./cprog "wibble" TEST=wibble $ eval `./cprog "wibble"` $ echo ""TEST\\" is now equal to \\"$TEST\\"" "TEST" is now equal to "wibble" $ 

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

相关问题 如果从Shell脚本调用,则使文件环境变量不生效 - Make file environment variable not taking effect if called from shell script 为脚本设置临时环境变量 - Setting temporary environment variable for a script 在c中使用popen执行的shell脚本中看不到环境变量 - Environment variables not visible in shell script executed using popen in c 在c函数中导出的环境变量是否在父shell中可用? - whether the environment variable exported in c function will be available in parent shell? 外壳环境变量的全局与局部静态变量 - Global vs. local static variable for shell environment variables 如何在ksh脚本中为子进程导出环境变量? - How to export an environment variable for children processes inside a ksh script? 如何导出变量(在Makefile,shell脚本中,…),以便将其视为C语言中的定义(作为宏)? - How to export variable [in Makefile, shell script, …] to be seen as defined in C (as macro)? 将值从C变量传递给嵌入式shell脚本? - Pass a value from a C variable to an embedded shell script? 将变量从C程序传递到Shell脚本作为参数 - Passing variable from c program to shell script as argument 如何在shell中执行C程序中的C程序更改环境变量? - How to change environment variable in shell executing a C program from that C program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM