简体   繁体   中英

Bash shell program starts C programs

Is there a way for a bash shell program, that takes a command-line argument x , that will make x (C program) processes start?

.

It's fairly simple:

#!/bin/bash
$1

If you want to pass the rest of the parameters as parameters to the function, do this:

$@

(ie foo.sh echo hi executes echo hi )

If you want to steal some parameters and pass others, use shift :

param1=$1
shift
echo $@ # contains parameters 2+
#!/bin/bash
(( $# != 1 )) && echo "Usage: $0 num" && exit -1
for (( c=1; c<=$1; c++ ))
do
   ./run_c_program &
done
wait
  • $1 represents the first command line argument
  • $# represents the number of arguments
  • $0 is the name of the script
  • run_c_program is the executable of the c program
  • with & the c programs are executed in the background
  • with wait the scripts waits for the c programs to terminate (optional)

您可以尝试使用system功能

system("./script.sh");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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