How to define multiple environment variables in a shell script?
Hi All,
I am running a shell script on centos 7 to get some variable values and want to declare them as environment variables so that i will use them in another shell script.
I am using below script i am able to define one environment variable but it's not allowing me to do for another variable.
.
.
.
Can someone please help me on this issue.
.
.
.
What exactly is the issue? As always: pls describe the error meticulously, add error messages and misbehaviour.
What I can say from looking at your script is that shell variables are not expanded within single quotes, so neither $user_name nor $passwd values will be passed to the .*profile files. If that is by intention, make sure the vaiables are defined in those scripts.
~/.bash_profile is for a bash login shell.
At login ~/.profile is for all sh-style shells (sh,ash,dash,jsh,psh,ksh,bash,...), but bash runs ~/.bash_profile instead if present.
Some graphical desktops provide a login but do not run a login shell, then bash runs ~/.bashrc.
Some Linux distros might run ~/.bash_profile (or ~/.profile) from /etc/bash.bashrc that is run prior to ~/.bashrc.
I am trying to set multiple environment variables using shell script.
I tried below commands to set the environment variables in the shell script(as example i shared one variable info):
when i executed the shell script it's executed successfully but when i am trying to print the environment variable it's returning empty value echo $NAME -- is giving empty value.
Can you please let me know what i need to change to set the environment variable ?.
Moderator's Comments:
please wrap your samples/codes in CODE TAGS as per forums rules.
Last edited by RavinderSingh13; 04-17-2020 at 12:22 AM..
EXPORT allows shell variables to be passed onto sub processes of the current shell not back up to the calling process.
If you want a child script to set environment variables in the parent (calling) script source the child script. Example:
See how the second instance sc_1 is sourced into the current shell by using . ./sc_1 (or source ./sc_1 if you prefer the clearer long-hand form). Any variables set will now be in the current shell. Think of this as reading the commands in the child script into the current process not spawning a new shell to run sc_1 which loses any environment vars after it finishes.
This User Gave Thanks to Chubler_XL For This Post:
.
.
.
when i executed the shell script it's executed successfully but when i am trying to print the environment variable it's returning empty value echo $NAME -- is giving empty value.
$NAME being empty is a strong indicator that $user_name is empty and the "command substitution" fails / results in an empty string. I guess its because the file called $abc doesn't exist. and the cat cats nothing.
Quote:
Can you please let me know what i need to change to set the environment variable ?
It would help if people knew the entire scenario. Variables in which environment do you want to set? Your current session? Future sessions? Why the echoing into the varying .profiles?
You're using find to search directories, nice.
But then you're using the variable without checking if it's has one , none or more results.
Further, I assume that you want the variable $user_name to actualy contain (just) the "<username>", and not the string "username + <username>".
This said, you might want to take care of that, afterwhich you might want to use something like this:
As the string might become empty after removing the found string pattern from it's content ("username + <username>"), if it even finds anything - as in your current case.
This said, you might want to make sure where your find starts from.
I would also suggest to take care of multiple finds (tempfiles) within a a for loop.
This is my suggested handling for all dynamic (find, locate, *, ls, which, etc) variables.
Dear Shell script Experts,
I am working on shell script which is defined here, qsub_seq.csh . The purpose of this script is to read few input files (with defined starting index and last index) and make processing faster over server.
For some task, I had 1064 of input files, so I wrote another... (8 Replies)
how to store the count of queries in variables inside a filein shell script
my output :
filename
-------
variable1=result from 1st query
variable2=result from 2nd query
.
.
.
. (3 Replies)
I have tried with the following:
csh -c 'source ~/.cshrc; exec bash' # works perfectly
(cat ~/.cshrc; echo exec bash) | csh # not working
And, using sed, I successfully retrieved the environment variables from ~/.cshrc
sed -rn 's/setenv\s+(\S+)\s+(.*)$/export \1=\2/p' ~/.cshrc
but now... (6 Replies)
I'm pretty new to scripting in Korn shell so please forgive me...
What I'm trying to do is to create a script that calls multiple other ksh scripts and defines variables for text files.
I need it to define my user defined variables (file paths, date & time stamps, etc that are currently in... (1 Reply)
Hi All,
I am using ETL tool Datastage and is installed on Linux environment. Few environment variables are set in datastage. Now my requirement is to use those environment variables in a unix script.
Is there any option I can do it?
Sugeestions from people working on datastage and linux... (1 Reply)
Hello Everyone
I need to create a script file which must append some lines to a target text file, I'm using sed for windows, the script file look like this:
{
a\
STRINGTABLE DISCARDABLE\
BEGIN\
5, 150 {a\
#define RC_SHELL, "%ID_SHELL%"\
#define RC_NAME, "%ID_NAME%"\
END
}
... (1 Reply)
oopps! I Meant "Define Variables within a UNIX Script"
What would be the best way to define a variable in a unix shell script so anyone who views this script doesn't know what value is assigned to that variable.
some other location...
a="/usr/lib/fileA"
Unix script...
sed... (5 Replies)
Hi all!
I know that environment variables can be set on the .bashrc file, but I need to set them from a sh script. I saw a lot of websites that teach this but it doesn't work for me.
#!/bin/sh
DEKTOP=$DESKTOP=:/home/rrodrigues/Desktop
export DESKTOP
if I do echo $DESKTOP returns me... (10 Replies)
First: me == noob. Whats a good resource for shell script info cause I'm having trouble finding good info.
I'm writing a shell script to automate the setup of a flash 'page flip'.
My current code is below.
the page flip takes an xml file of format
<content>
<pages... (1 Reply)