Ok so I've googled for an hour or more and I still didn't solved my issue.
I have these 3 bash commands:
find . -name "file_name" -exec du -h {} \;
-> gives me the file's sizels -l --time-style='+%d/%m/%Y' "file_name" | awk '{print $6}'
-> last modiffied datels -l "file_name"|cut -d" " -f1
-> file's permissionsAnd 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'
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.
Comments