This discussion is archived
8 Replies Latest reply: Apr 4, 2011 2:09 AM by 851268 RSS

How to declare global variables in a Shell Script

851268 Newbie
Currently Being Moderated
Hi All,

I have been using export command to declare variables for setting up the log folder path, data directory path, archive folder path, scripts folder path etc. in my shell scripts. E.g.
export datadir=(path1)
export archive=(path2)
export logs=(path3)
However, these variables with the same paths are used in multiple scripts, and with a small change in directory structure I have to reflect the changes across all the shell scripts. I would like to assign these variables as a global variables and want to maintain them at a single place, so that if any change comes I have to make ammendments at a single place rather than across all the files.

For example, I have aliased some routinely used directory path with ALPHABETS in my .profile, and whenever I log on to the session I run . .profile and then just have to type in those aliased alphabets to reach those directory paths. And in case any folder structure changes I have to make a change in .profile file only.

I need a solution something like this, I am litlle new to shell programming. Have googled how to declare kind of global variables in a shell script, but could'n get to the right link.
Also, could these variables be used in the sql*loader control file?

Please help.
Thanks,
Saurabh.
  • 1. Re: How to declare global variables in a Shell Script
    sb92075 Guru
    Currently Being Moderated
    Also, could these variables be used in the sql*loader control file?
    NO!
  • 2. Re: How to declare global variables in a Shell Script
    852297 Newbie
    Currently Being Moderated
    sounds like you want to source the variables (which is exactly what setting up the ALPHABETS in your profile did).

    So, from the script that decides what the paths are, you would call the other shell scripts with a '.' before them (after you have set the paths)

    ( . /my/script/path/srcipt_name)

    that will give the script that is being called the same environment as the one calling it.

    using EXPORT just sets the variable for that session/script.

    of course this will only work if you can use one script to set the paths and call the others from that one.
  • 3. Re: How to declare global variables in a Shell Script
    852297 Newbie
    Currently Being Moderated
    I suppose another solution might be setting those paths in a config file that all the other scripts can use?
  • 4. Re: How to declare global variables in a Shell Script
    orawiss Oracle ACE
    Currently Being Moderated
    You can create a main shell script , call the export environments and sql loader command inside.
  • 5. Re: How to declare global variables in a Shell Script
    sb92075 Guru
    Currently Being Moderated
    using EXPORT just sets the variable for that session/script.
    WRONG!
    "export" provides/allows any child process/script to inherit the variable

    NEWVAL=dumb
    remains local to current script
  • 6. Re: How to declare global variables in a Shell Script
    EdStevens Guru
    Currently Being Moderated
    848265 wrote:
    Hi All,

    I have been using export command to declare variables for setting up the log folder path, data directory path, archive folder path, scripts folder path etc. in my shell scripts. E.g.
    export datadir=(path1)
    export archive=(path2)
    export logs=(path3)
    However, these variables with the same paths are used in multiple scripts, and with a small change in directory structure I have to reflect the changes across all the shell scripts. I would like to assign these variables as a global variables and want to maintain them at a single place, so that if any change comes I have to make ammendments at a single place rather than across all the files.

    For example, I have aliased some routinely used directory path with ALPHABETS in my .profile, and whenever I log on to the session I run . .profile and then just have to type in those aliased alphabets to reach those directory paths. And in case any folder structure changes I have to make a change in .profile file only.

    I need a solution something like this, I am litlle new to shell programming. Have googled how to declare kind of global variables in a shell script, but could'n get to the right link.
    Also, could these variables be used in the sql*loader control file?

    Please help.
    Thanks,
    Saurabh.
    We put all our "global" variables in a script called locvars. Every other script we write has the line
    . /usr/local/bin/locvars
    Note, that is dot, space, /usr/local/bin/locvars
  • 7. Re: How to declare global variables in a Shell Script
    851268 Newbie
    Currently Being Moderated
    Hello,

    I have used another shell script where in I declared all the paths that I need in the current script. Everything is running fine, however, it is failing at the sql*loader calling statement. Below is the code snippet and the error coming.
    sqlldr userid=username@DB/password \
    control=$control_files/nc_cntrol.ctl \
    log=$control_files/nc_cntrol.log \
    discard=$control_files/nc_cntrol.dis \
    errors=50
    I did an echo of the $control_files before this sql*loader calling as well, and its returning correct path.
    But everytime when I run, its failing at this point.

    The error capture in the logs is
    LRM-00116: syntax error at '=' following '='
    SQL*Loader-100: Syntax error on command-line
    Is it that we cannot use variables in sql*loader call statements?

    Thanks.
  • 8. Re: How to declare global variables in a Shell Script
    851268 Newbie
    Currently Being Moderated
    Peoplez,

    It was a typo mistake I made in my shell script where in I declared all the variables.
    As someone said in one of the replies that you cannot use variables in a sql*loader script, please note that YOU CAN use.

    Everything is running fine now.

    Thanks.

Legend

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