Executing bash from a c program: No math operations.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Executing bash from a c program: No math operations.
I am making a program that multiplies a user input integer. This will convert minutes into seconds(needed for the bash command rtcwake -m mem -s $x, rtcwake is convenient for taking naps). I am taking a C programming course, but I am always tempted to use my newfound C programming skills on my home computer, which uses bash(Lubuntu 14.04). I have been following the following tutorial:
#include<stdio.h>
#include<stdlib.h>
#define SHELLSCRIPT "\
#!/bin/bash \n\
echo \"How many minutes would you like to nap for?\"\n\
read y\n\
let x=60*$y\n\
echo \"That's $x seconds\"\n\
"
int main()
{
puts("Initiating...");
system(SHELLSCRIPT);
return 0;
}
Unfortunately I get the following error message:
Code:
Initiating...a@a-NC210-NC110:~/bin$ gcc nap2.c
a@a-NC210-NC110:~/bin$ ./a.out
How many minutes would you like to nap for?
5
sh: 4: let: not found
That's seconds
Initiating...a@a-NC210-NC110:~/bin$
I have altered the above code with
Code:
#/bin/bash \n\
(w/o the "!") like the tutorial suggests, but still the same response.
Actually it really is cumbersome setting the variables from within the SHELLSCRIPT. Maybe is there a way to pass the variable from the c program into the SHELLSCRIPT?
Why doesn't 'let' work from within SHELLSCRIPT? Does C have a function equivalent to rtcwake(a command that wakes the computer)?
The code is perfectly fine, if I put minutes it returns the number of seconds. It appears that you are missing some of the libraries. Here is what I have in gcc:
#define SHELLSCRIPT "\
echo \"How many minutes would you like to nap for?\"\n \
read y\n \
bash -c \"echo \\\"That's $((60*$y)) seconds\\\"\" \
"
You should use bash for the let.. but let won't display the number, so I just replaced it with the $(( ))
But generally, you should:
1. use only a bash script, and leave C. (just write the original SHELLSCRIPT part to a file, chmod 755 filename, and run it with ./filename )
2. call an external shellscript directly ( you can use parameters, it will use the interpreter you define in the first line, etc )
3. write the whole thing in C.
#define SHELLSCRIPT "\
echo \"How many minutes would you like to nap for?\"\n \
read y\n \
bash -c \"echo \\\"That's $((60*$y)) seconds\\\"\" \
"
You should use bash for the let.. but let won't display the number, so I just replaced it with the $(( ))
I tried this:
Code:
#include<stdio.h>
#include<stdlib.h>
#define SHELLSCRIPT "\
echo \"How many minutes would you like to nap for?\"\n\
read y\n \
bash -c \"echo \\\"That's $((60*$y)) seconds\\\" \" \
"
int main()
{
system(SHELLSCRIPT);
return 0;
}
but I still got the following error:
Code:
a@a-NC210-NC110:~/bin$ gcc nap.c
nap.c:5:21: warning: missing terminating " character [enabled by default]
#define SHELLSCRIPT "\
^
nap.c: In function ‘main’:
nap.c:21:5: error: missing terminating " character
system(SHELLSCRIPT);
^
nap.c:21:5: error: too few arguments to function ‘system’
In file included from nap.c:3:0:
/usr/include/stdlib.h:717:12: note: declared here
extern int system (const char *__command) __wur;
^
Quote:
But generally, you should:
1. use only a bash script, and leave C. (just write the original SHELLSCRIPT part to a file, chmod 755 filename, and run it with ./filename )
2. call an external shellscript directly ( you can use parameters, it will use the interpreter you define in the first line, etc )
3. write the whole thing in C.
I guess you are right. It always seems so annoying being able to do so many things in C but linux only can use 'bash' for scripting. My idea of mixing scripts wasn't so good.
sietch:~# cat > proba.c
#include<stdio.h>
#include<stdlib.h>
#define SHELLSCRIPT "\
echo \"How many minutes would you like to nap for?\"\n\
read y\n \
bash -c \"echo \\\"That's $((60*$y)) seconds\\\" \" \
"
int main()
{
system(SHELLSCRIPT);
return 0;
}
sietch:~# gcc proba.c
sietch:~# ./a.out
How many minutes would you like to nap for?
2
That's 120 seconds
sietch:~#
It's working for me....
gcc version 4.4.7 (Debian 4.4.7-2)
Maybe you missed an " somewhere?
but it's simplier without C.
Code:
sietch:~# cat > nap
#!/bin/bash
echo "How many minutes would you like to nap for?"
read y
echo "That's $((60*$y)) seconds"
sietch:~# chmod 755 nap
sietch:~# ./nap
How many minutes would you like to nap for?
3
That's 180 seconds
It always seems so annoying being able to do so many things in C but linux only can use 'bash' for scripting.
I'm puzzled by that statement. You are free to use many different scripting languages or compilers. You can code in C, C++, go, pascal, fortran, cobal, .... You can write and run bash, ksh, tcsh, csh, zsh .... shell scripts. You can write and run python, perl, ruby,... scripts.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int y, x;
printf ("How many minutes would you like to nap for?\n");
scanf("%d", &y);
x = y * 60 ;
printf ("That's %d seconds\n", x);
return 0;
}
It always seems so annoying being able to do so many things in C but linux only can use 'bash' for scripting. My idea of mixing scripts wasn't so good.
No, you did not mix them, you simply tried to embed a bash script into C code. That script can work without C as it was already demonstrated.
But back to your C code. Probably you missed the \ chars are the very last ones at the end of the lines, there should be no other characters. Based on the error message you posted I think you need to remove all the spaces/tabs after \.
Others are right that you shouldn't be mixing C with bash. As far as library install is concerned you can use either apt-get install <package name> or you can use synaptic. As Lubuntu is debian derivative those should work.
@ All,
I doubt that he is missing any character or quotes in the code as I copy pasted it in vi, saved the file and simply run gcc against it. Though I have tried that on my SuSE box not on Lubuntu.
If it is something specific to distro then I am not aware of.
Last edited by T3RM1NVT0R; 03-04-2015 at 07:22 AM.
I have no problem invoking a bash script from within a program and have in fact done this as part of architectures where I created a bash script to do something and then needed to invoke that from within code logic.
My problem is the typical "don't use system()" issue. Here's a summary and example of what I recommend. Use fork() and one of the forms of exec().
Code:
pid_t pid;
char *args[3] = {
"my-script.sh",
"general-argument",
NULL
};
char *newenv[] = { NULL };
int w_status;
pid = fork();
if ( pid < 0 ) {
printf("fork() error\n");
return;
}
// Child, so run the intended program
else if ( pid == 0 ) {
if ( execve( "/home/myuser/my-script.sh", args, newenv ) < 0 ) {
printf("Error invoking execve\n");
return;
}
}
// Parent, so wait for a signal from the child
waitpid( pid, &w_status, 0 );
// Process w_status using macros, and etc
I have no problem invoking a bash script from within a program and have in fact done this as part of architectures where I created a bash script to do something and then needed to invoke that from within code logic.
Well his problem was, that he didn't call an external program, he defined the shell script in the C code, so the interpreter in the first line is ignored, and the builtin "sh" was used...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.