Control-m JOBNAME in shell script - unix

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).

Related

Assigning commands into variable

What is the right way to assign commands into variable . a.k.a how should i use quote commas and how assign it to variable , so the command is up to date? For example
i am at home/desktop
i assign variable
b=`pwd`
echo "$b" // home/desktop
but when i change the directory
cd games
pwd // home/desktop/games
echo "$b" // home/desktop
it does not update. Also i want to do something like
g="-l"
ls $g
is it possible?
Variables never update automatically, you have to re-assign them, i.e.,
b=`pwd`
echo "$b" // home/desktop
cd games
pwd // home/desktop/games
b=`pwd`
echo "$b" // home/desktop/games
as for your second question, I think that's only possible using eval:
g=" -la"
eval "ls"$g
BE VERY CAREFUL WITH THIS It's very easy to write something that can compromise security and indeed most people (including myself!) would strongly advise you never to use eval unless all else has failed.
Consider the code above for demonstration purposes only, under no circumstances to be used in a production system.
I think you are confusing assigning variables with aliases.
assigning a variable means you store the result of the command in a variable
what this command does
b=`pwd`
is running pwd and stores the answer in a variable b.
and alias means giving some command a different name. so running alias b pwd will make it so whenever you run b, you will actually run pwd
b is only set when you run the first command (assigning the output of pwd to b, it doesn't link those items together (so that b gets set on every invocation). There are ways to do that also (do man bash on your machine) (for example, the shell variable $PWD contains this information.
to your second question, yes, you can do that, but a nice way to so it is to use alias. So, alias ll='ls -l' creates a command on your system that will run ls -l when you type ll

Complex command execution in Makefile

I have a query regarding the execution of a complex command in the makefile of the current system.
I am currently using shell command in the makefile to execute the command. However my command fails as it is a combination of a many commands and execution collects a huge amount of data. The makefile content is something like this:
variable=$(shell ls -lart | grep name | cut -d/ -f2- )
However the make execution fails with execvp failure, since the file listing is huge and I need to parse all of them.
Please suggest me any ways to overcome this issue. Basically I would like to execute a complex command and assign that output to a makefile variable which I want to use later in the program.
(This may take a few iterations.)
This looks like a limitation of the architecture, not a Make limitation. There are several ways to address it, but you must show us how you use variable, otherwise even if you succeed in constructing it, you might not be able to use it as you intend. Please show us the exact operations you intend to perform on variable.
For now I suggest you do a couple of experiments and tell us the results. First, try the assignment with a short list of files (e.g. three) to verify that the assignment does what you intend. Second, in the directory with many files, try:
variable=$(shell ls -lart | grep name)
to see whether the problem is in grep or cut.
Rather than store the list of files in a variable you can easily use shell functionality to get the same result. It's a bit odd that you're flattening a recursive ls to only get the leaves, and then running mkdir -p which is really only useful if the parent directory doesn't exist, but if you know which depths you want to (for example the current directory and all subdirectories one level down) you can do something like this:
directories:
for path in ./*name* ./*/*name*; do \
mkdir "/some/path/$(basename "$path")" || exit 1; \
done
or even
find . -name '*name*' -exec mkdir "/some/path/$(basename {})" \;

Any unix command to get the result of most-recently executed command?

For example,
I executed "pwd" and it shows the current working directory. Then if I want to reuse that result in my another command, it would convenient to get it via a Unix command or built-in variable. Does it exist?
You can get the result, as in return code, using $?. In order to get the output you'll need to explicitly keep it around - e.g. with:
MYVAR=`pwd`
echo $MYVAR
Use $? inorder to get the status of the last executed command. Its value will be zero if the last executed command was successful else non zero.
The internal variable $? holds the return value of the last executed command or program. Example: http://tldp.org/LDP/abs/html/complexfunct.html#MAX.
If you don't need to run one command first, you can also try using pipes | to connect commands. I am constantly piping long directory listings over to more, so I can page through the results, with
ls -al | more
so if you want to use the results of running pwd as input to another program, you can try something like piping the results of pwd over to more with
pwd|more

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

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>

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