Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-23-2004, 11:33 AM   #1
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
Convert a Bash script to C

The short Bash script below works.

#!/bin/bash -p

while true

lastcheckin=`date "+%s" -r /var/webofc/webofc.conf`
now=`date "+%s"`

if [[ $lastcheckin+$inc -lt $now ]]
   for i in `ps -ef |grep |awk '{print $2}'`
      lvi=`kill -9 $i`
   /etc/ > /dev/null &

sleep 300


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.

#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 |awk '{print $2}'`; "
              "do "
              "lvi=`kill -9 $i` "
              "done "
              "/etc/ > /dev/null & "
              "fi ",
              "sleep 300 ",
              "done", "r");

Old 04-23-2004, 12:04 PM   #2
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
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30

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
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
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

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

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
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,633

Rep: Reputation: 390Reputation: 390Reputation: 390Reputation: 390
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
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 07:20 AM.

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