Find the answer to your Linux question:
Results 1 to 4 of 4
I am trying to write a ksh script that will generate commands. I will be passing variables to the script of the script will read lines from a file. I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2013
    Posts
    2

    ksh variable substitution


    I am trying to write a ksh script that will generate commands. I will be passing variables to the script of the script will read lines from a file. I then want to substitute these variables into a command which I run with ``. First of all can variables be substituted into a line being executed with `` surrounding it. I cannot easily tell. Here is a piece of the code. I echo it first (substitutions are made here).
    echo rman target / restore controlfile to $DATAGROUP2 from $line
    rman target / restore controlfile to +DATAGROUP1 from /u12/oracle/EBSSTAGE/db/apps_st/data/cntrl03.dbf

    Then I run the command
    var1=`rman target / restore controlfile to $DATAGROUP2 from $line;`

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hello and welcome!

    I don't quite understand what you are after. I can tell you that you can capture the output of a command using either backticks:
    Code:
    VAR=`command`
    or using the nest-friendly dollar/parenthesis alternative:
    Code:
    VAR=$(command)
    in both instances, the STDOUT of the command will be saved to the variable VAR, instead of being sent to the terminal.

    Perhaps you can post all of your code, or just go into more specifics about what it is you are trying to accomplish.

  3. #3
    Just Joined!
    Join Date
    Jan 2013
    Posts
    2
    This issue is somewhere between an rman issue and a korn shell issue. I am reading a file (controlfiole.txt) and I am generating an rman command and executing it. I have the reading and generating done and I am executing with ``. Doing it this way all of the rman variables have to be on one line. So I don't think I can split it up. This is for ASM implementation so '$DATAGROUP is my group name and $line2 is the control file path and name which I read in as $line. If I split up as you recommend, subsequent rman input lines are not aware that I have started an RMAN session.
    This is the result of the echo;
    rman target / restore controlfile to +DATAGROUP1 from /u12/oracle/EBSSTAGE/db/apps_st/data/cntrl03.dbf


    #!/bin/ksh
    DATAGROUP=\'+DATAGROUP1\'
    file='controlfile.txt'
    # while loop
    while read line
    do
    if [ -d ${line} ];
    then
    # display line or do somthing on $line
    echo 'yep'
    else
    line2=\'$line\';
    echo rman target system/xxxxxxxx restore controlfile to $DATAGROUP from $line;
    var1=`rman target system\/xxxxxxxx restore controlfile to $DATAGROUP from $line2;`
    print $var1
    fi
    done <"$file"

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer
    Join Date
    Apr 2012
    Location
    Virginia, USA
    Posts
    917
    What's the point of using var1 at all?

    If you want to execute the full statement of var1 later on, just use double quotes
    var1="ls -a"

    Then when you want to run whatever command you stored:
    $var1

    Using var1=`ls -a`
    would store the output of ls -a as var1, not the instructions to run the command.

Posting Permissions

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