Export command - variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Export command - variable
# 1  
Old 04-01-2019
Export command - variable

Hello,
Please see the script found in my computer below. (Ubuntu 14.04)
Code:
#!/bin/sh
export APP_DIR="/home/appname"
monitorscript="$APP_DIR""/monitor.sh"
ps cax | grep monitor.sh > /dev/null
if [ $? -eq 0 ]; then
echo "monitor.sh is running"
else
"$monitorscript"
fi

My question is regarding EXPORT command
I know everybody has their own style on coding but I wonder why do we use EXPORT to assign/call a variable to monitorscript variable?

Thank you
Boris
# 2  
Old 04-01-2019
Unless the variable APP_DIR is referenced inside /home/appnam/monitor.sh, export is not needed.

So in that case you could just use this instead:
Code:
#!/bin/sh
APP_DIR=/home/appname
[..]

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-01-2019
Quote:
Originally Posted by baris35
Code:
#!/bin/sh
export APP_DIR="/home/appname"
monitorscript="$APP_DIR""/monitor.sh"
ps cax | grep monitor.sh > /dev/null
if [ $? -eq 0 ]; then
echo "monitor.sh is running"
else
"$monitorscript"
fi

My question is regarding EXPORT command
I know everybody has their own style on coding but I wonder why do we use EXPORT to assign/call a variable to monitorscript variable?
The export keyword will make a variable visible in child processes of the process it is set in. Example: processA has a variable X=something. If processA starts a child processB the variable X will evaluate to "" (empty string) because this is a new process. The same goes for a processC started as child of processB and so on. If the variable is exported this makes no difference for processA but it will show up in processBs environment too.

Since you start one process from this process, namely:

Code:
else
"$monitorscript"
fi

The variable APP_DIR will be part of the environment of the started monitor-script because of the export. Without the export it would not. That has nothing to do with "style", maybe the process needs this variable set (i don't know). If not, then the export is superfluous and a simple:
Code:
APP_DIR="/home/appname"

would also suffice.

On a side note, this:
Code:
ps cax | grep monitor.sh > /dev/null
if [ $? -eq 0 ]; then
echo "monitor.sh is running"
else
"$monitorscript"
fi

would be better written this way:

Code:
if ps cax | grep -q 'monitor.sh' ; then
     echo "monitor.sh is running"
else
     "$monitorscript"
fi

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 4  
Old 04-02-2019
@Scrutinizer, Thank you so much.
@Bakunin, Thank you for your detailed explanation. While checking that script, I just wondered that. Got the difference which you implied now.

Kind regards
Boris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need help export variable

Hi, Please find my code below. ps -xef | grep 14766 | awk 'BEGIN{X="java_home=";X="weblogic_home="} {for(i=1;i<=NF;i++){if($i ~ /-Dplatform\.home|java$/){split($i,P,"=");s=P?P:$i;print X""s}}}' echo "java_home="$java_home echo "weblogic_home="$weblogic_home Output: Why does... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Can't export variable

I am relatively new to exporting variables, and I just can't seem to make this count work. What I have is the following: TOTAL=$($IMAGELIST -backupid $IM -U |gawk '{print $5}' |tail -1)|gawk '{print $6}' RESTORED=$($BPDBJOBS -most_columns -jobid $JOBS |cut -f15 -d,) |gawk '{print $6}' export... (7 Replies)
Discussion started by: newbie2010
7 Replies

3. Shell Programming and Scripting

export variable not working after su

I have a requirement to change user inside a shell script and execute group of commands. I have done it many times earlier but never came across the issue with exporting variables. Strangely if a var is exported afetr su, it is not working where as if it is does outside su, it works. Another issue... (8 Replies)
Discussion started by: sasiharitha
8 Replies

4. Shell Programming and Scripting

where does variable export to?

Hi, Unix Gurus, I have a problem need help. I have a script to generate environment variable code same as following: oracle_sid=abcd export oracle_sid when I execute this code with command ./script_nane it succeeded. when I try to find it with env command or echo $oracle_sid, it does not show... (5 Replies)
Discussion started by: ken002
5 Replies

5. Shell Programming and Scripting

Export variable

Hi I have a pass a variable from one script to another. Here are my scripts Script #1 ./profile #!/bin/sh export NAME="Hello" Script #2 ./test #!/bin/sh ./profile echo $NAME when I run ./test .. i am not getting anything .. why is that? (5 Replies)
Discussion started by: arex876
5 Replies

6. Shell Programming and Scripting

Export command variable exporting problem

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)
Discussion started by: shashi792
1 Replies

7. Shell Programming and Scripting

export variable question

simple question: for example if i use: export http_proxy=proxy:8080 and i have this script: while true; do .... lynx Google ;; wget The UNIX and Linux Forums - Learn UNIX and Linux from Experts ;; ... esac done So i must "export http_proxy=proxy:8080" before any lynx and wget... (4 Replies)
Discussion started by: aspire
4 Replies

8. Shell Programming and Scripting

Export Variable

How to export variable from one script to other? Can anybody give me syntax for that? Thanks (2 Replies)
Discussion started by: navi
2 Replies

9. UNIX for Dummies Questions & Answers

How to export the Display variable

I'm trying to open an xwindow on my Sun server. What am I doing wrong? # echo $SHELL /sbin/sh # # export DISPLAY=localhost:0.0 DISPLAY=localhost:0.0: is not an identifier Thank you! (1 Reply)
Discussion started by: FredSmith
1 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question