LinuxQuestions.org
Review your favorite Linux distribution.
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 09-27-2007, 10:21 AM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Rep: Reputation: 30
How to pass a c variable to a bash script?


Hi,

I want to pass a variable from a c program to a bash script. Can someone tell me how that can be done, or refer me to more information?

Thanks
 
Old 09-27-2007, 01:49 PM   #2
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
To be more specific, in c you can "pass" a variable to another file by using the extern specifier. I wonder how a variable in a c file can be seen from within another file that is a bash script.

Thanks
 
Old 09-27-2007, 03:25 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by daYz View Post
To be more specific, in c you can "pass" a variable to another file by using the extern specifier. I wonder how a variable in a c file can be seen from within another file that is a bash script.
Well, extern is a specifier telling the linker that a symbol exists in an object file different from the one you compiled with your original source file. Since, a script is not compiled into object code, there is no way to do what you describe (tell a script to look for symbols elsewhere).

There are, however, a few remedies when you have a C program calling a bash script (e.g., using the exec family of functions).
  1. You might pass the variable(s) to bash as command-line parameters. You can then access the positional parameters as $1, $2, $3, etc.
  2. You can pass the variable(s) to bash as environment variables. That way, you can access the variables in the script using whatever identifiers you chose in the C program.
 
Old 09-28-2007, 08:30 AM   #4
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
That is exactly what I needed to know. Thanks osor
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
how to pass a keystroke to a bash script? minike Programming 5 01-21-2007 11:42 AM
[SOLVED] issue with variable in bash script angel115 Programming 4 08-21-2006 01:42 PM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 03:12 PM

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

All times are GMT -5. The time now is 10:37 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