Loading ...
Sorry, an error occurred while loading the content.

Re: [blug-prog] command line argument + '*'

Expand Messages
  • Balwinder Singh
    ... * on command line is expanded by shell to list of files. Thus when you run program the actual arguments to program are list of files in working directory.
    Message 1 of 8 , Jun 10, 2009
    • 0 Attachment
      On Wednesday 10 June 2009 20:02:31 prabhjot singh wrote:
      > My questions/doubts:
      > 1) What is the reason behind this?
      > 2) Can't I pass * as command line argument?
      >
      > #include <stdio.h>
      > int main(int argc, char *argv[])
      > {
      > int i = 0;
      > printf("argc: [%d]", argc);
      > while (i < 10 && i < argc)
      > printf("%s\n", argv[i++]);
      > }
      > output:-
      >
      > >./a.out *
      >
      > argc: [38]
      > /*here goes list of files*/

      * on command line is expanded by shell to list of files.
      Thus when you run program the actual arguments to program are list of files in
      working directory.
      If you want to pass * to your program, pass it enclosed in double quotes.
      ./a.out "*"

      Regards
      Balwinder


      [Non-text portions of this message have been removed]
    • Nirmalya Lahiri
      ... Yes...you can. Pass the argument as string. Example... a.out * . This is because the asterisk without quote has a special meaning for shell. You can
      Message 2 of 8 , Jun 10, 2009
      • 0 Attachment
        --- On Wed, 6/10/09, prabhjot singh <prabhjotsinghengineer@...> wrote:

        > From: prabhjot singh <prabhjotsinghengineer@...>
        > Subject: [blug-prog] command line argument + '*'
        > To: linux-bangalore-programming@yahoogroups.com
        > Date: Wednesday, June 10, 2009, 8:02 PM
        > Hi,
        >
        > I write a very simple program to print command line
        > arguments.
        > As and when program hits '*' in received arguments It
        > converts it to list of
        > files in current directory.
        >
        > My questions/doubts:
        > 1) What is the reason behind this?
        > 2) Can't I pass * as command line argument?


        Yes...you can. Pass the argument as string. Example... >a.out '*' . This is because the asterisk without quote has a special meaning for shell. You can think of it as a substitution variable; so when ever you will use * , it will be replaced by 'all'. But when ever you will use '*' , it will be treated as string.


        >
        > #include <stdio.h>
        > int main(int argc, char *argv[])
        > {
        >     int i = 0;
        >     printf("argc: [%d]", argc);
        >     while (i < 10 && i < argc)
        >         printf("%s\n", argv[i++]);
        > }
        > output:-
        >
        > >./a.out *
        > argc: [38]
        > /*here goes list of files*/
        >
        > --
        > Platform used: RH Linux - Bash shell | compiler: gcc
        > [egcs-2.91.66 ]
        >
        > --
        > Thanks
        > PS
        >
      • vinod
        Hi, * is one of the metacharacter(special character) in unix shell. * - matchess zero or more characters . for eg: foo* - matches all files that start with
        Message 3 of 8 , Jun 10, 2009
        • 0 Attachment
          Hi,

          * is one of the metacharacter(special character) in unix shell.
          * - matchess zero or more characters .
          for eg: foo* - matches all files that start with foo-
          like foo, foo1, foo2, foo123

          hence when u pass * as command line argument, shell expands it, which
          results in all the files and sub-dir's
          in current directory, hence you got the output so.

          to avoid shell from expanding : try the following options
          $./a.out '*'
          $./a.out "*"
          $./a.out \*


          Regards,
          Vinod

          On Wed, Jun 10, 2009 at 8:02 PM, prabhjot singh <
          prabhjotsinghengineer@...> wrote:

          >
          >
          > Hi,
          >
          > I write a very simple program to print command line arguments.
          > As and when program hits '*' in received arguments It converts it to list
          > of
          > files in current directory.
          >
          > My questions/doubts:
          > 1) What is the reason behind this?
          > 2) Can't I pass * as command line argument?
          >
          > #include <stdio.h>
          > int main(int argc, char *argv[])
          > {
          > int i = 0;
          > printf("argc: [%d]", argc);
          > while (i < 10 && i < argc)
          > printf("%s\n", argv[i++]);
          > }
          > output:-
          >
          > >./a.out *
          > argc: [38]
          > /*here goes list of files*/
          >
          > --
          > Platform used: RH Linux - Bash shell | compiler: gcc [egcs-2.91.66 ]
          >
          > --
          > Thanks
          > PS
          >
          > [Non-text portions of this message have been removed]
          >
          >
          >


          [Non-text portions of this message have been removed]
        • sujay g
          bash is interpreting * and passing it as the list of files in the current directory, you could try passing escape sequence with * and see what is the behavior.
          Message 4 of 8 , Jun 10, 2009
          • 0 Attachment
            bash is interpreting * and passing it as the list of files in the
            current directory, you could try passing escape sequence with * and
            see what is the behavior.
            -sujay

            On Wed, Jun 10, 2009 at 8:02 PM, prabhjot
            singh<prabhjotsinghengineer@...> wrote:
            >
            >
            > Hi,
            >
            > I write a very simple program to print command line arguments.
            > As and when program hits '*' in received arguments It converts it to list of
            > files in current directory.
            >
            > My questions/doubts:
            > 1) What is the reason behind this?
            > 2) Can't I pass * as command line argument?
            >
            > #include <stdio.h>
            > int main(int argc, char *argv[])
            > {
            > int i = 0;
            > printf("argc: [%d]", argc);
            > while (i < 10 && i < argc)
            > printf("%s\n", argv[i++]);
            > }
            > output:-
            >
            >>./a.out *
            > argc: [38]
            > /*here goes list of files*/
            >
            > --
            > Platform used: RH Linux - Bash shell | compiler: gcc [egcs-2.91.66 ]
            >
            > --
            > Thanks
            > PS
            >
            > [Non-text portions of this message have been removed]
            >
            >
          • Ganaraja Ng
            Hi prabhjot, try ./a.out * Use escape sequence to send a * Thanks, Ganaraj ________________________________ From: prabhjot singh
            Message 5 of 8 , Jun 10, 2009
            • 0 Attachment
              Hi prabhjot,

              try ./a.out \*

              Use escape sequence \ to send a *

              Thanks,
              Ganaraj




              ________________________________
              From: prabhjot singh <prabhjotsinghengineer@...>
              To: linux-bangalore-programming@yahoogroups.com
              Sent: Wednesday, June 10, 2009 8:02:31 PM
              Subject: [blug-prog] command line argument + '*'





              Hi,

              I write a very simple program to print command line arguments.
              As and when program hits '*' in received arguments It converts it to list of
              files in current directory.

              My questions/doubts:
              1) What is the reason behind this?
              2) Can't I pass * as command line argument?

              #include <stdio.h>
              int main(int argc, char *argv[])
              {
              int i = 0;
              printf("argc: [%d]", argc);
              while (i < 10 && i < argc)
              printf("%s\n" , argv[i++]);
              }
              output:-

              >./a.out *
              argc: [38]
              /*here goes list of files*/

              --
              Platform used: RH Linux - Bash shell | compiler: gcc [egcs-2.91.66 ]

              --
              Thanks
              PS

              [Non-text portions of this message have been removed]







              [Non-text portions of this message have been removed]
            • prabhjot singh
              Thanks to all! Your suggestion works reply helped. -- Prabhjot ... [Non-text portions of this message have been removed]
              Message 6 of 8 , Jun 11, 2009
              • 0 Attachment
                Thanks to all! Your suggestion works reply helped.
                --
                Prabhjot
                On Thu, Jun 11, 2009 at 10:18 AM, sujay g <mail.sujayg@...> wrote:

                >
                >
                > bash is interpreting * and passing it as the list of files in the
                > current directory, you could try passing escape sequence with * and
                > see what is the behavior.
                > -sujay
                >
                > On Wed, Jun 10, 2009 at 8:02 PM, prabhjot
                > singh<prabhjotsinghengineer@... <prabhjotsinghengineer%40gmail.com>>
                > wrote:
                > >
                > >
                > > Hi,
                > >
                > > I write a very simple program to print command line arguments.
                > > As and when program hits '*' in received arguments It converts it to list
                > of
                > > files in current directory.
                > >
                > > My questions/doubts:
                > > 1) What is the reason behind this?
                > > 2) Can't I pass * as command line argument?
                > >
                > > #include <stdio.h>
                > > int main(int argc, char *argv[])
                > > {
                > > int i = 0;
                > > printf("argc: [%d]", argc);
                > > while (i < 10 && i < argc)
                > > printf("%s\n", argv[i++]);
                > > }
                > > output:-
                > >
                > >>./a.out *
                > > argc: [38]
                > > /*here goes list of files*/
                > >
                > > --
                > > Platform used: RH Linux - Bash shell | compiler: gcc [egcs-2.91.66 ]
                > >
                > > --
                > > Thanks
                > > PS
                > >
                > > [Non-text portions of this message have been removed]
                > >
                > >
                >
                >
                >


                [Non-text portions of this message have been removed]
              Your message has been successfully submitted and would be delivered to recipients shortly.