Unix shell script - printing all the variables available in the scope - unix

Is there any way to find out the variables available in the scope of the shell script.
the scenario is like this, we are using some third party tools and we can customize the output with creating shell scripts with a particular naming convention. we know that certain parameters are being passed to our custom shell scripts but we want to know what else is being passed.
thanks.

The command is set
From the bash manual page
set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
Without options, the name and value of each shell variable are displayed in a
format that can be reused as input.
Do not confuse this with env which will print out the values of environment variables not shell variables. shell variables can be marked for automatic export into the environment of subsequent child processes using the export command.
scope as a programming term, only really applies to shell variables - commands like typeset and local can be used in some shells (ksh and bash) to allow the use of scoped shell variables within functions. environment variables are global to a instance of a processes.

It's very easy ;)
env

It sounds like you want environment variables, so use export -p. The output (which consists of lines of the form export variable=value per POSIX) is quoted in such a way that it can be parsed by the shell. It's also sorted by variable name in most shells.
If you want all shell parameters (for your use case, this would be relevant only if the scripts were sourced rather than called as separate programs), use set (again, it's in POSIX, the output is properly quoted for reparsing, and it's sorted in most shells).

This may be really simplistic but...
grep = <your script>

Related

Can I source ksh and expect in the same script?

I am trying to use environment variables set by ksh and the expect command in the same script. However, if I try to source both of them, it doesnt work. Is there a way to source ksh and expect in the same script?
Do something like
#!/usr/bin/ksh
. /path/to/ksh_stuff.sh
export FOO=bar
# other ksh stuff
expect <<'END_EXPECT'
source /path/to/expect_stuff.exp
send_user "FOO is $env(FOO)\n"
# other expect stuff
END_EXPECT
Adding quotes around the here-doc terminator (<<'END_EXPECT') means that the entire here-doc is single quoted, so ksh will not attempt any parameter substitutions on it. This is a effective way to isolate the expect script's variables from ksh.
In Korn shell you dot in the other script, example:
. ${other_script}
This will run in the same process as the parent script. The other script can see any variables that are defined in the parent script. If you want to sub-shell (to run an external command), then you will need to export any variables first.
If you want to reference environment variables in your expect script, (those that are exported by a ksh script that runs expect in a subshell) then your expect script needs to reference a global array env . For example if your ksh script exports MYPATH variable then subshells to expect, the expect might reference $env(MYPATH)

Control-m JOBNAME in shell script

How can I retrieve control-m JOBNAME in unix shell (ksh) script
From what I read %%JOBNAME should give me the JOBNAME but unix does not support %%JOBNAME. I tried it but not successful
Also I tried using $JOBNAME but it did not work either
First you will have to get your local %%JOBNAME variable into a global variable, you can do that pretty easily from within the job form.
Once you have a global variable, there is a ctmvar utility included with the CTM Server/Agents.
You can use that to read in Control-M global auto edit variables into script-able shell environments like bash as follows:
UNIXVAR=$(ctmvar -action list | grep %%CTMGLOBALVARIABLENAME | awk '{print $2}')
To do the reverse and set a CTM global variable from a unix shell variable do this:
ctmvar -action set -var "%%\CTMGLOBALVARIABLE" -varexpr "$UNIXVAR"
I do this all the time in shell scripts and it works great. The one caveat is that once you set a global variable is it visible to all agents and all servers by the same variable name, so be careful you use unique variable names so you don't step on your own toes, and also that you clean up after yourself or you will have a ton of global variables left lying about.
the easiest way is to define a parameter in the job that submits the unix task. Name the parameter PARM1 (or PARM2) and give it the value %%JOBNAME. in the unix script, you'll access the value with $1 (or $2).

typeset functions location

When I use the command typeset -f in ksh, a list of functions with their definition is displayed in stdout.
I tried to search where those functions are defined, but I couldn't find any hint about them. Can anyone help me finding them?
EDIT
I'm just learning the use of the typeset command, typing man typeset game me nothing (no manual entry for typeset).
In order to define functions that will be displayed using typeset -f, we need to define a function and export it using typeset -xf.
Functions can be declared in the .profile, or files called from .profile or put in a dir that is referenced by the FPATH variable (and proabably other places too). Read your man ksh carefully for the order of files that are processed on startup. Search for the 'Invocation', 'Files', and 'Functions' sections.
Also, there are a group of default functions that ksh sets up. So please edit your question to show the function names that your concerned with.
IHTH
Shells don't keep a record of where functions (or aliases, or variables, etc...) are defined. Conceptually, and notwithstanding interactive usage features like shell history, shells read commands from input one at a time, execute them, and then forget them. Sometimes those commands come from interactive input, sometimes they come from scripts. Sometimes they have side effects like defining a function in the shell's environment, but the shell still doesn't remember the command or its position in the shell's input stream after it's finished executing it.

Setting environment variable in shell script does not make it visible to the shell

I want to use a shell script that I can call to set some environment variables. However, after the execution of the script, I don't see the environment variable using "printenv" in bash.
Here is my script:
#!/bin/bash
echo "Hello!"
export MYVAR=boubou
echo "After setting MYVAR!"
When I do "./test.sh", I see:
Hello!
After setting MYVAR!
When I do "printenv MYVAR", I see nothing.
Can you tell me what I'm doing wrong?
This is how environment variables work. Every process has a copy of the environment. Any changes that the process makes to its copy propagate to the process's children. They do not, however, propagate to the process's parent.
One way to get around this is by using the source command:
source ./test.sh
or
. ./test.sh
(the two forms are synonymous).
When you do this, instead of running the script in a sub-shell, bash will execute each command in the script as if it were typed at the prompt.
Another alternative would be to have the script print the variables you want to set, with echo export VAR=value and do eval "$(./test.sh)" in your main shell. This is the approach used by various programs [e.g. resize, dircolors] that provide environment variables to set.
This only works if the script has no other output (or if any other output appears on stderr, with >&2)

How to create a new environment variable in UNIX....?

How to create a new environment variable in unix and use it in a program??????
You can tell what shell you're running by ps -o comm= -p $$ — I think that's more-or-less universal. So, in bash and certain similar shells...
If you want to create the variable for one specific run, you can do
MYVAR=value the_command_that_needs_myvar
If you want to create it for an entire shell session (ie. until you log out):
export MYVAR=value
...and then you can run:
the_command_that_needs_myvar
...as many times as you like during that session, and it will still see MYVAR as having the value value.
If you want it to be set for yourself, for all your login sessions, put it in ~/.profile.
Please note that bash's initialisation files can be one great big WTF. Depending on whether it is run interactively, over a network, locally, AND depending on whether it is invoked as sh or bash, it will selectively read some combination of ~/.bashrc, ~/.profile and ~/.bash_profile. Read the FILES section of the bash man page for details.
If you want it to be set for every user, every time they log in, put it in the file /etc/profile (although there's also /etc/environment, I'm not sure how widely used that is.).
Check out the question "How to set environment variable for everyone under my linux system?" for some more details, too.
(Beware, some of this advice will vary depending on if you, or other users, use bash, dash, csh, ksh, etc... but it should work for most use cases.)
Depends on the shell. In bash, you can use:
export myvar=xyz
which will set the variable and make it available to other programs.
If you want to set it for one invocation of a program, you can use:
myvar=xyz ./myprog
This will have it set for the myprog process but not after it exits.
See setenv(3) and getenv(3) functions.

Resources