This discussion is archived
12 Replies Latest reply: May 11, 2012 1:32 AM by 913199 RSS

Shell script: How can i get the parameters of a command line?

635105 Explorer
Currently Being Moderated
Im a newbie. Help please!
For example my mini script test.sh
#!/bin/bash
echo all parameters: $*
and now i run some test:
$ ./test.sh aa bb cc
all parameters: aa bb cc
ok!


$ ./test.sh aa bb $cc
all parameters: aa bb
where is here $cc ?


$ ./test.sh aa $bb cc
all parameters: aa cc
where is $bb ?



My question: How can i become 'aa $bb cc'? But without using of \  or ' or "  like this:
 ./test.sh aa \$bb cc
or 
 ./test.sh 'aa $bb cc'
Many thanks!
  • 1. Re: Shell script: How can i get the parameters of a command line?
    Dude! Guru
    Currently Being Moderated
    Bash expands positional and special parameters on the command line before executing the command. To disable parameter expansion would require to break fundamental Unix shell standards, and as far as I know you cannot disable it and need to quote or escape these meta-characters.
  • 2. Re: Shell script: How can i get the parameters of a command line?
    635105 Explorer
    Currently Being Moderated
    Mission impossible ? I can not believe it!

    Has anyone else other ideas what I can make?
  • 3. Re: Shell script: How can i get the parameters of a command line?
    bobthesungeek76036 Pro
    Currently Being Moderated
    I'm not sure what you want here. As Dude explained, the "$" character means something very specific to the shell. Once you prepend it to text, it means to interpret it as a variable. There's no way around it. That's why they <b>have</b> the ability to escape it with the \ character or enclose it in single-quotes...
  • 4. Re: Shell script: How can i get the parameters of a command line?
    bobthesungeek76036 Pro
    Currently Being Moderated
    I guess if you are really bent on doing this:

    <pre>
    $ declare aa='$aa'
    $ declare bb='$bb'
    $ declare cc='$cc'
    $ ./test.sh aa $bb cc
    all parameters: aa $bb cc
    $ ./test.sh aa bb $cc
    all parameters: aa bb $cc
    $
    </pre>
  • 5. Re: Shell script: How can i get the parameters of a command line?
    Dude! Guru
    Currently Being Moderated
    To my experience, impossible requirements often turn out to be a mistake in the underlying logic or concept. This may not be the answer you are looking for, but there is probably another more appropriate solution in case you are trying to accomplish something more complex than your script example.
  • 6. Re: Shell script: How can i get the parameters of a command line?
    Dude! Guru
    Currently Being Moderated
    For what it's worth, the following might do:

    <pre>
    # do not use hash bang (#!/bin/bash)
    # convert last command to variable array
    var=(`history | tail -1`)
    echo "${var[2]} ${var[3]} ${var[4]}"
    </pre>
    ./testme aa $bb cc
    aa $bb cc
  • 7. Re: Shell script: How can i get the parameters of a command line?
    635105 Explorer
    Currently Being Moderated
    Many thanks for replies!

    i know the $ is a special character in shell script.
    bash see it as the begin of variable bla..bla..


    But the requirement is:
    i want to write a script, which take input as a SQL-Select string from SQLPlus,
    execute it.. and the result output goto to a file...



    Now when we want make a Select, wen say in SQLPlus for example:
    SQL> select * from v$datafile;
    but not:

    SQL> select \* from v\$datafile;
    
    
    or
    
    SQL> 'select * from v$datafile';
    
    
    or
    
    SQL> select * from 'v$datafile';
  • 8. Re: Shell script: How can i get the parameters of a command line?
    635105 Explorer
    Currently Being Moderated
    Hi Dude!

    the idea with last command from history is good!!

    First i can not unterstand the sha-bang/bash-bang. Why should it be on comment?


    And it run correct when called direct in Shell (Terminal), but not when called in SQLPlus

    run direct from shell:
    [oracle@oel57 bin]$ ./testme a b $c
    a b $c
    [oracle@oel57 bin]$ 
    run from SQLPlus:
    SQL> !testme a b $c
      
    
    SQL> 
    it's great when you have a idea for me!
    Thanks
  • 9. Re: Shell script: How can i get the parameters of a command line?
    Dude! Guru
    Currently Being Moderated
    Sqlplus and bash are two different programs with a different command syntax and environment.
    SQL> !testme a b $c
    What are you trying to accomplish? Why would you run a bash script inside SQLplus that takes a SQL statement as a parameter?
  • 10. Re: Shell script: How can i get the parameters of a command line?
    635105 Explorer
    Currently Being Moderated
    This is exactly what i want! Why not?

    It is possible to run a Linux command or bash script from SQLPlus.
    It begin with "!" and the script can have parameter too!
    You can say:
    SQL>!ls
    SQL> !ls -l /u01/app
    ...


    It is possible, also i do it!

    My script should have following functions:
    - take the SQL statement from SQLPlus as paraameter,
    - execute the SQL
    - spool the result to a temp file.
    - format the select result: elimnate unwanted the column spacing
    - present again in SQLPlus

    It look like this:
    SQL>!do_select_format select * from v$logfile;
    do_select_format is the name of my script!

    and i see somthing like this:
    GROUP# STATUS TYPE   MEMBER                                     IS_ 
    ====== ====== ====== ========================================== === 
    3             ONLINE +DATA/orcl/onlinelog/group_3.266.780357305 NO  
    3             ONLINE +DATA/orcl/onlinelog/group_3.267.780357305 YES 
    2             ONLINE +DATA/orcl/onlinelog/group_2.264.780357303 NO  
    2             ONLINE +DATA/orcl/onlinelog/group_2.265.780357303 YES 
    1             ONLINE +DATA/orcl/onlinelog/group_1.262.780357301 NO  
    1             ONLINE +DATA/orcl/onlinelog/group_1.263.780357303 YES 
                                                                        
    6 rows,  5 columns found!
    Note: i dont want to say: col format xxxx xxx ... before!

    Edited by: DBA(t home) on May 8, 2012 4:44 AM

    Edited by: DBA(t home) on May 8, 2012 4:46 AM
  • 11. Re: Shell script: How can i get the parameters of a command line?
    806917 Explorer
    Currently Being Moderated
    Seems like a lot of work for a simple report.

    I would recommend:
    1) SQL*Plus formatting commands for text output, OR
    2) SQL*Plus html output. Example:
    set heading off
    set echo off
    set feedback off
    spool output.html
    set markup html on
    select * from v$logfile;
    set markup html off
    spool off
    exit
  • 12. Re: Shell script: How can i get the parameters of a command line?
    913199 Newbie
    Currently Being Moderated
    Note that you would want to do:

    ./test.sh aa '$bb' cc

    If you enclose all of the parameters in the single quotes then they will be read as one variable.

    I think that you have to accept that that is the way that the shell is written, and so you have to use quotes (or backslash) to stop certain characters being interpreted; this includes '$' (denotes a variable), '#' (start of comment), and ' ' (space, eg. in filenames).

    You can turn off filename expansion with noglob (so that '?' and '*' are taken literally and don't have wildcard meanings), but that doesn't work for '$'...

    [paul@vmlxoel64 Desktop]$ ls -l
    total 0
    -rw-rw-r--. 1 paul paul 0 May 11 09:27 blah
    -rw-rw-r--. 1 paul paul 0 May 11 09:28 z
    [paul@vmlxoel64 Desktop]$ echo * ?
    blah z z
    [paul@vmlxoel64 Desktop]$ set -f; echo * ?; set +f
    * ?
    [paul@vmlxoel64 Desktop]$

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points