Problems setting or exporting variable when using multiple commands from same line.
I am experimenting with some scripting as a way to learn more about it. I have a simple script that calls two other scripts. Each script echos some stuff to prove it ran and then sets a simple variable and exports it.
I cannot get one of the variables to display back in the main calling script when I use a command form that sources the scripts from the same line:
However, it works like this:
It also works like this:
Maybe you must source each individual script. But, I used as a base for these experiments a shell that used this form, which does not source the individual commands:
and it works fine.
My version does execute but there are issues with the second variable I try to display. It seems to not be getting exported so that it can display properly.
Here are the three simple scripts followed by the output. Any insight would be appreciated. (BTW - I am not a programmer by trade but am rather trying to learn some stuff here.)
I am experimenting with some scripting as a way to learn more about it. I have a simple script that calls two other scripts. Each script echos some stuff to prove it ran and then sets a simple variable and exports it.
You must source them. Nothing else will work.
When you source it, it runs inside your current shell, exporting variables inside it for you to use.
When you run it directly, it executes in a new shell. Variables are set and exported in the new shell, which dies uselessly immediately thereafter, taking all its variables with it, because variables don't travel between parent and child. Children get copies of anything you've exported, but are independent thereafter.
Thanks very much for the explanation. This is subtle behaviour I of which I was not aware. I looked further into the scripts from which I based my test on that "seemed" to work and found that the second script (which was not sourced) does not do anything with variables that need to exist outside of the second temporary shell. That explains that!
Hey everyone,
I am working in an environment where the different users can use ksh or csh. My situation is that I need the same result with one single command line.
I am searching for the real path the file is in.
My ksh input and output
ts2:ts2adm> cd $(dirname $(which sapcontrol)); pwd -P... (2 Replies)
can someone help me with this?
AgentDX=/tmp/tesfinal && cat final.enc | openssl aes-128-cbc -a -d -salt -k hello | sh
the content of final.enc is a code. and in that code, the value of AgentDX is needed. however, i cant seem to find a way to allow the code to recognize that the variable... (4 Replies)
If I have a txt file with
test.txt
somelineoftext
and I want to set that line of text to variable in a script:
so
#!/bin/bash
var=''
becomes
#!/bin/bash
var='somelineoftext' (3 Replies)
Hi There,
I have a cronjob that executes a small script (few lines) that I am certain can be achieved in a single line.
The functional objective is actually really simple;
cmd var1
The '1' in 'var1' is actually derived from date (day of month) but the snag is when working with 1-9 I... (3 Replies)
I have a txt file from which i am assiging a value to a variable
using the code in script1
script1.sh
export f=$(sed -n "/Freq *=/ s/.*= *//p" ${R_path}/output.txt)
echo "$f" --------> this works
in script2 ( which executes the script1)
eval ./script1.sh
if && ; then
echo... (1 Reply)
Hi All;
I m working on a script and came across an issue ,to explain it briefly here is the sample code
echo $HOSTNAME|while read IN; do var=`echo $IN|awk -F "-" '{print $2}'`; export var; echo $var; done
now I get the value of $var but when it is out of the while loop
it does not return... (3 Replies)
Even though the idea "might" not be great I still wrote this piece of code to get practice.. Which means that it is the CODE that matters here.
Anyways;
The intension is to create a program(or do we call it script?) that searches recursively through a folder to find a file - stored in a... (4 Replies)
Hi,
I want to make sed write a part of fileA (first 7 lines) to file1 and the rest of fileA to file2 in a single call and single line in sed. If I do the following:
sed '1,7w file1; 8,$w file2' fileA
I get only one file named file1 plus all the characters following file1. If I try to use curly... (1 Reply)
Hi,
can anyone tell me the difference between the below two examples:
Eg-1:
# name=bravo
# echo $bravo
what would be the o/p
Eg-2:
# name1=jhonny
# export name1
# echo $name1
what would be the o/p
If the o/p's of both examples are the same then what is the use of the cmd export... (3 Replies)
Hi,
I am exporting the environment variable from config file, but when I echo the variable it does not display any value. Here is the snippet of the code
#!/bin/sh
export ENVIRONMENT_ROOT_DIRECTORY="/cb/$ENVIRONMENT"
echo $ENVIRONMENT_ROOT_DIRECTORY
${JAVA_HOME}/bin/java... (2 Replies)