Running two commands using C and Linux environment variables - c

I'm trying to run two commands using a C program and a Linux environment variable:
#Program name is execute
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char cmd[256] = "/home/username/hello.sh $USER";
execl("/bin/bash", "bash", "-p", "-c", cmd, NULL);
return 0;
}
However, when running the program with the $USER environment variable set to a second command, the second command will not run.
env USER=";cat /home/username/hello.txt" ./execute
Hello from shell script
Hard coding the second command into the C program works:
char cmd[256] = "/home/username/hello.sh ;cat /home/username/hello.txt";
./execute
Hello from shell script
Hello from text file
I want my C program to return:
env USER=";cat /home/username/hello.txt" ./execute
Hello from shell script
Hello from text file
How can I get it to work, without changing the C program?

The only processing that's done on the result of expanding variables is word splitting and globbing. It doesn't process characters like ; to separate commands, > for output redirection, $ for further variable expansions, etc.
If you want to force a full parse of the command, you have to use eval.
char cmd[256] = "eval /home/username/hello.sh $USER";
execl("/bin/bash", "bash", "-p", "-c", cmd, (char*)NULL);

Related

Program linux command with pipe in C

I´m trying to execute the next linux command
cat file_a file_b file_c | wc –l > result.txt
in a C program, but I´m not able to do it properly. I have very low level of C programming, and I would like to see how to make that command works in a C program.
This is the code I developed without success:
void main() {
execlp("/bin/sh", "/bin/sh", "-c", "cat file1 file2 fileN | wc –l > lines.txt", 0);
}
I follow your example.
Its results:
implicit declaration of function 'execlp' is invalid in C99
And some other warning errors.
But I think it would be better if you use the system() C-function, here is what I've done:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
system("cat filea fileb filec | wc -l > result.txt");
return EXIT_SUCCESS;
}
After compilation that works!
If you want to run just a command without needing to read the resulting output, you could use the C-function system(), but if you want to run it getting its results, you should use popen().
system()
popen()

Relation between PATH env and argv[0]

Generally argv[0] is as same as exec file name. For example:
If I execute program with ./my_program then argv[0] is ./my_program
If I execute program with /home/username/my_program then argv[0] is /home/username/my_program.
My question is, if PATH=/home/username why I can't see argv[0] value?
This is my real situation
PATH=/home/knight/bin:/home/knight/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/knight
My test program source is:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%s\n", argv[0]);
}
My home directory is /home/knight so I can execute program directly.
knight#knight-desktop:~$ test
knight#knight-desktop:~$ ./test
./test
I can't understand, why doesn't the knight#knight-desktop:~$ test command print any result?
Because test is a shell builtin command.
And there is a big difference between ./test(it is an executable file) while test is a command passed direct to the shell of which if typed incorrect, it could have been not recognised for example lets say you use the command tst the result will be -bash: tst: command not found
To check if any word is a builtin command/reserved keyword for shell,use command type.
on terminal,
$type test
test is a shell builtin
$type if
if is a shell keyword

How can I put bash script in C program

I have shell scripts and I need to run that continuous work in background.
For example:
#include <stdio.h>
int main(int argc, char **argv)
{
for (; ;) {
system("./dup -r /root/duptest/");
sleep(60);
}
return 0;
}
It's working and run every minute.
First question: How can I run this background(like & --> ./dup ... &) without put &.
Second question: How can I put shell codes in C source codes?
I found this, Do I need to put \n\ for all lines? It's so hard for edit.
#include <stdio.h>
#include <stdlib.h>
#define SHELLSCRIPT "\
#/bin/bash \n\
echo \"hello\" \n\
echo \"how are you\" \n\
echo \"today\" \n\
"
int main()
{
system(SHELLSCRIPT);
return 0;
}
Third question: How can I use shell parameter in C, like this:
./dup.exe -r /blablabla...
mean
system("./dup -r /blablabla");
I need to use $1 $2 parameter with compiled C program.
Question 1: Look for "how to make a process as deamon process in UNIX" Although daemon process is a overkill for your requirement, you can perform steps until the process is running according to your requirements
Question 3: You need to have command line arguments, check about that. Your main should look like main(int arg_count, char *args_vector[]){...} and in that you can access each command line argument as an array element
Q1: use fork() and don't wait on the child's PID.
Q2: C and C++ will concatenate adjacent string literals, like so:
static const char script[] =
"echo hello\n"
"echo how are you\n"
"echo today"
;
int main(int argc, char* argv[])
{
puts(script); // so you can see what it looks like
// system(script); // <-- uncomment this line to actually run it.
return 0;
}
Q3: use the argc and argv parameters to main() to build the command line you want to execute.

how to run execlp with arguments from command

I am trying to create simple program which will run shell commands from arguments, for example
./run date +"%r"
07:56:05 PM
but I cant figure how. I try this, but it not working. I am pretty confused and absolutely cant figure how exec works..
#include <unistd.h>
#include <stdio.h>
int main (int argc, char *argv[]){
execlp("bash","bash", "argv[1]", (char*)0);
return 0;
}
You probably meant (note the lack of quotes around argv[1]):
execlp("bash", "myprogram", argv[1], NULL);
Note that I assume here that myprogram is a shell script. In case it's a binary, you should remove the preceding "bash" parameter.
One good troubleshooting technique could be replacing bash with echo to confirm the command line.

How to display the output of a program both to a file and to the console(stdout)?

Say I have the following program that simply outputs "Hello World":
//DEMO.c
#include<stdio.h>
int main()
{
printf("HELLO World");
}
Now I want to display it both to the screen and to a file output.txt.So I enter the following command in the command prompt(I use CodeBlocks on Windows XP and have configured it to work on command prompt as well):
demo.exe>>output.txt>>stdout
It doesn't work!!! Please tell me how to do it,ie how to output the same thing that I see on my screen(When i run the program),simultaneously to a text file?
You will need to download a tee command for Windows. tee is a UNIX/Linux command that copies the standard input to standard output and also outputs to a file. Then, you can do this:
demo.exe | tee output.txt
Here is one port of tee for Windows.
#include <stdio.h>
#define my_fprintf(fp,...) do{fprintf(fp, __VA_ARGS__);fprintf(stdout, __VA_ARGS__);}while(0)
int main(int argc, char **argv){
FILE *fp;
fp=fopen("output.txt","w");//or filename from argv[1]
my_fprintf(fp, "hello world by %s\n", argv[0]);
fclose(fp);
return 0;
}

Resources