How to execute a shell script from C in Linux?

后端 未结 6 1975
旧巷少年郎
旧巷少年郎 2020-11-28 03:34

How can I execute a shell script from C in Linux?

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 03:59

    A simple way is.....

    #include 
    #include 
    
    
    #define SHELLSCRIPT "\
    #/bin/bash \n\
    echo \"hello\" \n\
    echo \"how are you\" \n\
    echo \"today\" \n\
    "
    /*Also you can write using char array without using MACRO*/
    /*You can do split it with many strings finally concatenate 
      and send to the system(concatenated_string); */
    
    int main()
    {
        puts("Will execute sh with the following script :");
        puts(SHELLSCRIPT);
        puts("Starting now:");
        system(SHELLSCRIPT);    //it will run the script inside the c code. 
        return 0;
    }
    

    Say thanks to
    Yoda @http://www.unix.com/programming/216190-putting-bash-script-c-program.html

提交回复
热议问题