[英]Pass parameter from shell script to c program
gcc (GCC) 4.7.2
GNU bash, version 4.2.37
Hello, 你好,
I have the following bash script that I want to pass string parameter to my c program. 我有以下bash脚本,我想将字符串参数传递给我的c程序。 I tried using pipes but failed. 我尝试使用管道,但失败了。
The c program will need to get an input from the shell script. c程序需要从shell脚本获取输入。 I am not sure to to read in an input from a shell script. 我不确定要从Shell脚本中读取输入。
My bash script is below. 我的bash脚本如下。
#!/usr/bash
# About on any errors
set -e
RUN_WITH_VALGRIND=""
if [ "$1" == "valgrind" ]; then
RUN_WITH_VALGRIND="valgrind"
echo "START TESTING WITH VALGRIND"
fi
$RUN_WITH_VALGRIND ./c_program
echo "Hello" | ./c_program
And my sample c program is here: 我的示例C程序在这里:
char str_input[16];
printf("Get input: ");
scanf("%s", str_input);
printf("Input [ %s ]\n", str_input);
I am trying to get the scanf to read the input from the shell script. 我试图让scanf从外壳脚本中读取输入。
Many thanks for any advice, 非常感谢您的任何建议,
echo "Hello" | $RUN_WITH_VALGRIND ./c_program
echo "Hello" | $RUN_WITH_VALGRIND ./c_program
, it's that simple. echo "Hello" | $RUN_WITH_VALGRIND ./c_program
,就这么简单。
But in your script, c_program will run twice as you re-call it after the run_with_valgrind
call (I don't know if its intend or not) 但是在您的脚本中,当您在run_with_valgrind
调用之后重新调用它时,c_program将运行两次(我不知道它是否打算这样做)
I like to use 'here docs' for that: 我喜欢为此使用“这里文档”:
$RUN_WITH_VALGRIND ./c_program <<EOF
first
second
EOF
cf. cf. http://tldp.org/LDP/abs/html/here-docs.html http://tldp.org/LDP/abs/html/here-docs.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.