LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-23-2004, 11:33 AM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
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);
 }
 
Old 04-23-2004, 12:04 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
So basically you're just packing your script into an executable by using C? I think you want to use system() instead of popen().
 
Old 04-23-2004, 12:19 PM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
reply

Hi itsme86. Thank you for your response. Regardless of whether I used System or popen, it still complains about the syntax.
 
Old 04-23-2004, 12:52 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
That is because it wants one string. ("blah" "blah") is not a valid constant char*
 
Old 04-23-2004, 01:00 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 04-25-2004, 12:51 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
quick bash script to convert flac to mp3 zuralin Programming 8 08-19-2011 03:50 AM
Convert from shell script to binary? Anon123 Linux - General 4 06-26-2004 05:53 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
script to convert system to Debian cloids Debian 2 01-15-2004 01:40 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration