LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2015, 12:58 AM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Question 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:

http://www.unix.com/programming/2161...c-program.html

My code is as follows:
Code:
#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)?
 
Old 02-28-2015, 04:44 AM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
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:

Code:
gcc-locale-4.3-62.198
libgcc43-4.3.3_20081022-11.18
gcc-info-4.3-62.198
gcc43-locale-4.3.3_20081022-11.18
gcc-c++-4.3-62.198
gcc43-c++-4.3.3_20081022-11.18
gcc43-info-4.3.3_20081022-11.18
gcc-4.3-62.198
gcc43-4.3.3_20081022-11.18
 
1 members found this post helpful.
Old 02-28-2015, 05:56 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Function 'system' call /bin/sh. The comment in the first line is only a comment which is ignored by /bin/sh.

Actually, your C program has no function at all; you should forget it, and simply create a bash-script.
 
1 members found this post helpful.
Old 02-28-2015, 06:40 AM   #4
derive
Member
 
Registered: Apr 2014
Distribution: debian
Posts: 42

Rep: Reputation: Disabled
#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.
 
1 members found this post helpful.
Old 03-03-2015, 06:51 AM   #5
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Cool

Quote:
Originally Posted by T3RM1NVT0R View Post
It appears that you are missing some of the libraries.
aCtually, you're right I checked the 9 mentioned libraries you mentioned and I don't have any.

How do you reccommend to install a library?
Any good tutorials on installing missing libraries?

Last edited by andrew.comly; 03-03-2015 at 07:05 AM.
 
Old 03-03-2015, 07:23 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Exactly what 'library' would change the fact that program /bin/sh doesn't support instruction let?
 
Old 03-03-2015, 08:35 PM   #7
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Red face

Quote:
#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.
 
Old 03-03-2015, 08:51 PM   #8
derive
Member
 
Registered: Apr 2014
Distribution: debian
Posts: 42

Rep: Reputation: Disabled
It's nasty....

Code:
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

Last edited by derive; 03-03-2015 at 08:54 PM.
 
Old 03-03-2015, 08:52 PM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,
Quote:
Originally Posted by andrew.comly View Post
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.

Evo2.
 
Old 03-03-2015, 09:02 PM   #10
derive
Member
 
Registered: Apr 2014
Distribution: debian
Posts: 42

Rep: Reputation: Disabled
Or in C:

Code:
#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;
}
 
Old 03-04-2015, 12:26 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,022

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
Quote:
Originally Posted by andrew.comly View Post
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 \.
 
Old 03-04-2015, 07:21 AM   #12
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ andrew.comly,

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.
 
Old 03-04-2015, 07:32 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Which part is problematic on this error message:
Code:
sh: 4: let: not found
 
Old 03-04-2015, 07:50 AM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
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
 
Old 03-05-2015, 04:38 AM   #15
derive
Member
 
Registered: Apr 2014
Distribution: debian
Posts: 42

Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
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...
 
  


Reply

Tags
bash, c programming, math



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - difference between executing program in for loop and typing each command? jlarsen Programming 14 11-10-2008 05:38 PM
bash operations with string alenD Linux - Software 6 10-17-2008 02:43 PM
math program that I can enter math functions ... Four General 5 04-19-2006 08:02 PM
Math operations with PHP linuxfond Linux - Newbie 1 04-20-2005 06:58 AM
file operations in BASH snorky Linux - Newbie 2 11-24-2003 07:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration