简体   繁体   English

如何从带有一个或多个参数的bash脚本运行C程序?

[英]How to run C program from bash script with one or more arguments?

This is my first time trying to create a bash script and I'm a bit confused. 这是我第一次尝试创建bash脚本,我有点困惑。

I'm trying to run my c program from a bash script where the c program can take one or multiple arguments. 我正在尝试从bash脚本运行我的c程序,其中c程序可以接受一个或多个参数。 If the c program takes 1 argument it will print the first n prime numbers depending on what was typed in. If it takes multiple arguments it will print the first n prime numbers of the largest integer argument and then the prime numbers between all possible ranges of the arguments. 如果c程序使用1个参数,则将根据所键入的内容打印前n个素数。如果使用多个参数,则将打印最大整数参数的前n个素数,然后打印所有可能范围之间的素数。论据。 I'm having issues calling my c program with my bash script. 我在用bash脚本调用c程序时遇到问题。 Right now I'm just trying to have it find the largest integer and then print the first n integers: 现在,我只是想让它找到最大的整数,然后打印前n个整数:

file=$1
max=$2

shift
for arg in "$@"
do
    if [ $arg -gt $max ]; then
      max=$arg
    fi
done

for file in $path
do
    ./app "$file" "$max"
done

When I run the bash script if I type: 当我输入以下命令运行bash脚本时:

bash ./prime.sh ./prime 4 3 1

I should get: 2, 3, 5, 7 我应该得到: 2, 3, 5, 7

I did not include the c code because that's currently working. 我没有包含C代码,因为它正在运行。

Thanks! 谢谢!

Get rid of the for loop, just do: 摆脱for循环,只需执行以下操作:

"$path" "$max"

to run the program in $path with the argument $max . 使用$max参数在$path运行程序。

I don't know what ./app is, but you don't need to run another program to execute a C program. 我不知道什么是./app ,但是您不需要运行其他程序来执行C程序。

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

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