Find the answer to your Linux question:
Results 1 to 3 of 3
Here is my test code because my other code is too long: Code: #!/bin/bash rdstr=`./rdchn` let "rdval = $rdstr" echo -e "rdval=$rdval" exit and this is the C code it ...
  1. #1
    Linux User
    Join Date
    Mar 2008
    Posts
    287

    Passing parameters from C code to bash script

    Here is my test code because my other code is too long:
    Code:
    #!/bin/bash
    rdstr=`./rdchn`
    let "rdval = $rdstr"
    echo -e "rdval=$rdval"
    exit
    and this is the C code it calls:
    val = val + val + ... ;  //This line does work used elsewhere
    printf("From RDCHN>> %d\n", val);
    sprintf(str, "%s", val);                                 //converts to string
    printf("From rdchn>> %s\n", str);
    there is a printf cmd to output str with a \n following it for the benefit of the rdstr=`./rdchn` bash script, and I am aware that the other print statements are now in the way of it.
    I get no print to stdout on screen from the C code.
    Does bash somehow block or mask it?

    I get print from bash. I get this error in the non-test bash script like: let "rdval = $rdstr"
    syntax error: operand expected (error token is " ")
    The let command prints rdval= but I presume this is due to the printf test statements getting in the way.
    I am getting no compile errors. Why does the C code not print the values specified?
    Last edited by MikeTbob; 04-16-2011 at 12:44 PM. Reason: Added Code Tags

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    I made several changes in your posting, including making a complete script and c program, and using CODE tags for easier reading:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate capture of results into shell variable.
    
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && . $C gcc
    
    FILE=${1-c1.c}
    pl " Amended c code:"
    cat $FILE
    
    pl " Compile and identify c code:"
    rm -f c1
    gcc -o c1 $FILE
    file c1
    
    pl " Results from amended script and code:"
    # rdstr=`./rdchn`
    rdstr=`./c1`
    # let "rdval = $rdstr"
    rdval=$rdstr
    echo -e "rdval=$rdval"
    rm -f c1
    
    exit
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
    GNU bash 3.2.39
    gcc (Debian 4.3.2-1.1) 4.3.2
    
    -----
     Amended c code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define VERSION  "1.0"
    #define PROGRAM  "main"
    
    int
    main (int argc, char *argv[])
    {
    
      printf (" Hello, world from c: %s, %s.\n", PROGRAM, VERSION);
    
      int val = 2;
      char *str;
      str = malloc (80);
      val = val + val;		//This line does work used elsewhere
      printf ("From RDCHN>> %d\n", val);
      // sprintf(str, "%s", val); //converts to string
      sprintf (str, "%d", val);	//converts to string
      printf ("From rdchn>> %s\n", str);
    
    }
    
    -----
     Compile and identify c code:
    c1: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, not stripped
    
    -----
     Results from amended script and code:
    rdval= Hello, world from c: main, 1.0.
    From RDCHN>> 4
    From rdchn>> 4
    See man pages and google for details ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Linux User
    Join Date
    Mar 2008
    Posts
    287

    Passing parameters from C code to bash script [SOLVED]

    TNX drl. Turns out I needed to fix was include string.h and fix my sprintf. Learned your neat trick as well. Yeh yeh on CODE tags, can never remember how to do those.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...