Linux.com

jo_d

jo_d

  • Linux.com Member
  • Posts: 1
  • Member Since: 17 Apr
  • Last Logged In: 17 Apr

Latest Posts

Posted by
Topic
Post Preview
Posted
  • jo_d
    RE: C take input from Bash Script
    What you did in your bash script is correct, but you might as well write "./test s 2" In fact, when bash encounter '$varname', it replace it [i]first[/i] with the value of the variable varname, [i]before the function call[/i], so the line is converted to "./test s 2" before bash calls the c file, which is the moment when ./test s 2 is converted to an array with tree pointer to "./test\0", "s\0" and "2\0". This array can be acceded by defining main like "int main (int argc, char ** argv)" (argc and argv are named by convention, but there types ca not be changed) argv is the array bash where bash putted the words of the line that called the c file. You can then access to 's' and '2' with argv[1][0] and argv[2][0]. The next thing you might want to do is convert 2 in an integer, so you might find [url=http://stackoverflow.com/questions/3420629/convert-string-to-integer-sscanf-or-atoi]this (http://stackoverflow.com...convert-string-to-integer...)[/url] usefull.
    Link to this post 17 Apr

    What you did in your bash script is correct, but you might as well write "./test s 2"
    In fact, when bash encounter '$varname', it replace it first with the value of the variable varname, before the function call, so the line is converted to "./test s 2" before bash calls the c file, which is the moment when ./test s 2 is converted to an array with tree pointer to "./test\0", "s\0" and "2\0".
    This array can be acceded by defining main like "int main (int argc, char ** argv)"
    (argc and argv are named by convention, but there types ca not be changed)
    argv is the array bash where bash putted the words of the line that called the c file.
    You can then access to 's' and '2' with argv[1][0] and argv[2][0].
    The next thing you might want to do is convert 2 in an integer, so you might find this (http://stackoverflow.com...convert-string-to-integer...) usefull.

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board