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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-23-2004, 11:33 AM
|
#1
|
|
Member
Registered: Apr 2003
Posts: 178
Rep:
|
Convert a Bash script to C
The short Bash script below works.
Code:
#!/bin/bash -p
inc=700
while true
do
lastcheckin=`date "+%s" -r /var/webofc/webofc.conf`
now=`date "+%s"`
if [[ $lastcheckin+$inc -lt $now ]]
then
for i in `ps -ef |grep checkin2.sh |awk '{print $2}'`
do
lvi=`kill -9 $i`
done
/etc/checkin2.sh > /dev/null &
fi
sleep 300
done
/////////////////////////////////////////////////
The script above is being converted to C crudely. I got syntax error
probably because of where the semi-colon is suppose to be inserted.
Code:
#include <stdio.h> /* standard library. a minimum requirement */
#include <stdlib.h> /* for oct_long = strtoul (six_oct_char, NULL, 8) */
main ()
{
FILE *f;
f = popen ("inc=700; "
"while true "
"do "
"lastcheckin=`date \"+%s\" -r /var/webofc/webofc.conf` "
"now=`date \"+%s\"` "
"if [[ $lastcheckin + $inc < $now ]]; "
"then for i in `ps -ef |grep checkin2.sh |awk '{print $2}'`; "
"do "
"lvi=`kill -9 $i` "
"done "
"/etc/checkin2.sh > /dev/null & "
"fi ",
"sleep 300 ",
"done", "r");
pclose(f);
}
|
|
|
|
04-23-2004, 12:04 PM
|
#2
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
So basically you're just packing your script into an executable by using C? I think you want to use system() instead of popen().
|
|
|
|
04-23-2004, 12:19 PM
|
#3
|
|
Member
Registered: Apr 2003
Posts: 178
Original Poster
Rep:
|
reply
Hi itsme86. Thank you for your response. Regardless of whether I used System or popen, it still complains about the syntax.
|
|
|
|
04-23-2004, 12:52 PM
|
#4
|
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
That is because it wants one string. ("blah" "blah") is not a valid constant char*
|
|
|
|
04-23-2004, 01:00 PM
|
#5
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Quote:
Originally posted by jtshaw
That is because it wants one string. ("blah" "blah") is not a valid constant char*
|
You can append a \ at the end of the line to split strings like you're doing:
Code:
itsme@dreams:~/C$ cat splitstring.c
#include <stdio.h>
int main(void)
{
puts("this is a " \
"split string.");
return 0;
}
itsme@dreams:~/C$ ./splitstring
this is a split string.
|
|
|
|
04-25-2004, 12:51 PM
|
#6
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
Linh,
some remarks on your C program:
- popen expects two arguments and you give it 4, remove the colons after "fi" and after "sleep ..."
- you use "r" as last argument, but fail to read the pipe output, so the script ends before having doing any jobs, I suggest you to use "w" instead.
- you must separate statements with semicolons, otherwise all your lines will be appended together without separators
- as it is, I see no advantage in front of the shell script it replaces ...
btw, "blah" "blah" is a valid constant equals to "blahblah" contrarily to what was answered previously..
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:23 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|