How to execute multiple commands in system() , also using a variable

maspinu

Ok so I've googled for an hour or more and I still didn't solved my issue.

I have these 3 bash commands:

  1. find . -name "file_name" -exec du -h {} \; -> gives me the file's size
  2. ls -l --time-style='+%d/%m/%Y' "file_name" | awk '{print $6}' -> last modiffied date
  3. ls -l "file_name"|cut -d" " -f1 -> file's permissions

And I want to execute these 3 commands at a time using system();

Output example :

File size : ...

Last modiffied date : ...

File permissions : ...

My attempt :

char command[256];
char file_name[]={"myfile.txt"};
sprintf(command,"find . -name %s -exec du -h {} \; &&
        ls -l --time-style='+%D/%m/%Y' %s | awk '{print $6}' &&
        ls -l %s | cut -d' ' -f1",
        file_name,file_name,file_name);
system((char*)command);

NOTES : I don't have to worry about the file's path because I'm using files from the same directory I execute my c program.

Compilation error : 'Sh: && is not expected'

TonyB

You needed to add a percent sign in front of each percent sign you wanted in your command, otherwise it would be interpreted by sprintf as a print-mask introducer. Additionally, you need to add a back-slash in front of each back-slash you want in your command, otherwise it would be interpreted by sprintf as an introducer (e.g. for \n etc.). Additionally, I modified the sprintf second argument (i.e. the string)... I put ending quotes at the end of each physical line in your code, and at the beginning of the next physical line in your code.

Here is an example that may be what you are looking for (at least it compiles and runs):

#include <stdio.h>

int main(int argc, char **argv)
{

    char command[256];
    char file_name[] = "myfile.txt";


    sprintf(command, "find . -name %s -exec du -h {} \\; && "
            "ls -l --time-style='+%%D/%%m/%%Y' %s | awk '{print $6}' && "
            "ls -l %s | cut -d' ' -f1",
            file_name,
            file_name,
            file_name);
    printf("command: -->%s<--\n\n\n", command);
    system((char*)command);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Using Docker-Compose, how to execute multiple commands

From Dev

How do I execute multiple commands when using find?

From Dev

How do I execute multiple commands using the same argument?

From Dev

execute multiple commands in cmd using java

From Dev

execute multiple commands in cmd using java

From Dev

How to execute multiple CLI commands in a python script?

From Dev

How to execute multiple commands remotely on few servers?

From Dev

How to execute multiple commands in Bash, some in the background

From Dev

How to execute multiple CLI commands in a python script?

From Dev

How can I execute an external commands in C/Linux without using system, popen, fork, exec?

From Dev

Bash - how to write the output of a command into a variable and also get the commands pid

From Dev

Bash - how to write the output of a command into a variable and also get the commands pid

From Dev

How can we execute multiple cmd commands in one session using C++ Lib Function or Windows API?

From Dev

How can I get Ansible to execute multiple commands using with_items?

From Dev

How can I get Ansible to execute multiple commands using with_items?

From Dev

How to execute thousands of commands in parallel using xargs?

From Dev

How to execute commands in gnuplot using shell script?

From Dev

Execute system commands using wmi python on remote computer

From Dev

how to execute (exec) external system commands in Scala actor?

From Dev

A better way to execute multiple MySQL commands using shell script

From Dev

execute multiple commands in Linux using python in the same time

From Dev

Which one is better: using ; or && to execute multiple commands in one line?

From Dev

Which one is better: using ; or && to execute multiple commands in one line?

From Dev

Execute System Commands in Java on Mac

From Dev

How to run a program in background and also using && to execute another command

From Dev

How to run a program in background and also using && to execute another command

From Dev

How to execute multiple commands after opening a new terminal tab

From Dev

How to add multiple commands in series in "Execute Windows Batch Command" in Jenkins

From Dev

How can i execute multiple commands with JAVA code?

Related Related

  1. 1

    Using Docker-Compose, how to execute multiple commands

  2. 2

    How do I execute multiple commands when using find?

  3. 3

    How do I execute multiple commands using the same argument?

  4. 4

    execute multiple commands in cmd using java

  5. 5

    execute multiple commands in cmd using java

  6. 6

    How to execute multiple CLI commands in a python script?

  7. 7

    How to execute multiple commands remotely on few servers?

  8. 8

    How to execute multiple commands in Bash, some in the background

  9. 9

    How to execute multiple CLI commands in a python script?

  10. 10

    How can I execute an external commands in C/Linux without using system, popen, fork, exec?

  11. 11

    Bash - how to write the output of a command into a variable and also get the commands pid

  12. 12

    Bash - how to write the output of a command into a variable and also get the commands pid

  13. 13

    How can we execute multiple cmd commands in one session using C++ Lib Function or Windows API?

  14. 14

    How can I get Ansible to execute multiple commands using with_items?

  15. 15

    How can I get Ansible to execute multiple commands using with_items?

  16. 16

    How to execute thousands of commands in parallel using xargs?

  17. 17

    How to execute commands in gnuplot using shell script?

  18. 18

    Execute system commands using wmi python on remote computer

  19. 19

    how to execute (exec) external system commands in Scala actor?

  20. 20

    A better way to execute multiple MySQL commands using shell script

  21. 21

    execute multiple commands in Linux using python in the same time

  22. 22

    Which one is better: using ; or && to execute multiple commands in one line?

  23. 23

    Which one is better: using ; or && to execute multiple commands in one line?

  24. 24

    Execute System Commands in Java on Mac

  25. 25

    How to run a program in background and also using && to execute another command

  26. 26

    How to run a program in background and also using && to execute another command

  27. 27

    How to execute multiple commands after opening a new terminal tab

  28. 28

    How to add multiple commands in series in "Execute Windows Batch Command" in Jenkins

  29. 29

    How can i execute multiple commands with JAVA code?

HotTag

Archive