Sponsored Content
Top Forums Shell Programming and Scripting How to preserve the value of a variable from being overwritten? Post 302909000 by bakunin on Sunday 13th of July 2014 07:20:31 AM
Old 07-13-2014
I can only emphasize what Don Cragun has already said. You didn't post your script therefore, instead of offering detailed advice, i will only make some general remarks. You are invited to post your complete script if you want to get better and more specific help.


Quote:
Originally Posted by veeresh_15
I am new new to unix.com, I have a question related to shell scripting.
We have a Oracle database backup shell script, which can be used for taking full, incremental & archive log backup based on the parameters passed.

Within the script we export a variable as
Code:
export gv_shell_id=$(date "+%y%m%d_%H%M%S_")$$

So for every execution of backup, we will have a unique gv_shell_id. We store the gv_shell_id along with other backup details in a database table for reporting purpose.
I think we have here a (common) misunderstanding about what "export" does, so let us address that first.

Every process has its own set of variables (and their respective values), which form the so-called "process environment". Processes form hierarchies, where one process (the "child process") is called by the other (the "parent process") and it would be good to have a mechanism which passes the process environment of the parent on to the child.

Exactly this does the "export" keyword. Issuing an "export <variablename>" flags the variable to be inherited by an eventual child process if one is invoked. As long as you stay inside the process there is no difference. You also do not need to "export" the variable again if its value changes. A variable is either "marked for being inherited by the child process" or it is not, but the inheritance takes place at the time the child process starts and with the value the exported variable has by then.


Quote:
Originally Posted by veeresh_15
If we have executed a full backup, it will set a gv_shell_id and if simultaneously some one kicks off archive log backup, it will reset the gv_shell_id with different value.
No. If your script is called twice these two will be (maybe simultaneously running) instances, which will - from an OS point of view - not interfere with each other. They will have two separate environments and will run alongside each other.

Of course, just because they are indenpendent in the eyes of the operating system that doesn't mean they couldn't interfere at all. If they use the same resources - they probably issue commands against the same database, which will cause the DB to change its state - they can cause problems for each other. But this has nothing to do with their process environments, but rather with a competition for common resources, where the "common resource" in this case is the database or some vital part of it.

My suggestion is to make sure the script is executed only once at a time. Usually you do this by creating a PID-file at the begin of the script and removing it at the end. See the following sketch:

Code:
#! /bin/sh

typeset fRun="/var/run/myscript.pid"

if [ -e "$fRun" ] ; then
     echo "ERROR: can only be run once" >&2
     exit 2
fi
echo "$$" > "$fRun"

# ... rest of your script goes here ...

rm -f "$fRun"
exit 0

Depending on your shell you may put the removal of the PID file into a service routine for "trap 0", which will enhance the runtime security, but not change the basic, underlying logic.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

.cshrc and .login overwritten !!

Hi, My account is : abcd I belong to a group: pqrs Some thing straneg happened yesterday. My .cshrc and .login got overwritten into pqrs's .cshrc and .login I obviously did not explicitly overwrite pqrs's .cshrc. Are there any reasons how this could have happened indirectly due to... (5 Replies)
Discussion started by: gjthomas
5 Replies

2. UNIX for Dummies Questions & Answers

Grub Loader entry overwritten

Hello, One of my frend had a problem. He had Windows XP installed on his system. Then he installed Red Hat Linux 8.0 in one of the partitions. After some time his XP got corrupt and then he reinstalled Windows XP. This over wrote the Grub loader entry, and due to this the grub loader is not... (2 Replies)
Discussion started by: rahulrathod
2 Replies

3. AIX

UIDs being overwritten immediately

We have a problem where we delete a user and their associated UID gets dumped back in the UID pool. The if we immediately create a another (new) user, AIX reuses the last UID, the one that was just released. This is causing a problem when reports are being generated because the new users name is... (2 Replies)
Discussion started by: xsys2000
2 Replies

4. HP-UX

Critical files in /etc overwritten EMPTY!

The following files were wiped out - new empty files were left in their place. /etc/inittab, /etc/inetd.conf, and /etc/MANPATH The system is running HP-UX 11i v3 - Mar08. Anyone seen anything like this? Any ideas on a way to figure this out if it happens again or a suggested way to... (9 Replies)
Discussion started by: KEnglander
9 Replies

5. Solaris

overwritten rootdisk?

Hi, The dump device on my system was set to /dev/dsk/c0t0d0s7. I have done a savecore -Lv on the system which worked fine. I'm wondering have I overwritten the rootdisk here by mistake? The system is still up but will need to be rebooted due to an error on it. Will it come back up? ... (8 Replies)
Discussion started by: gwhelan
8 Replies

6. Programming

variables overwritten

Hi, i have some problems with the following code: char *tab_path; char *sep=" \t\n"; char line; char *p; FILE * file; int i = 0; if(fgets(line,MAXLINE,file)!=NULL){ if((p=strtok(line,sep))!=NULL)tab_path=p; while((p=strtok(NULL,sep))!=NULL){ i++; ... (4 Replies)
Discussion started by: littleboyblu
4 Replies

7. Shell Programming and Scripting

file is getting overwritten

Hello All, I am writing a bash script on Solaris O/S. I looping through an array. For each iteration, i connect to the datatabase and use select statement. Output of which is redirected to .CSV file. here is the code for it. output="loop.csv" elements=${#currency_pair} ... (3 Replies)
Discussion started by: arundhati_s
3 Replies

8. Shell Programming and Scripting

Preserve space in variable of AWK

This seems to be a stupid basic question, but I cant get the space to stick in the awk variable. I do use this command to grep a time range of the log file. cat /var/log/daemon.log | awk '$0>=from&&$0<=to' from="$(date +%b" "%e" "%H:%M:%S -d -24hour)" to="$(date +%b" "%e" "%H:%M:%S)" I now... (9 Replies)
Discussion started by: Jotne
9 Replies

9. Shell Programming and Scripting

Preserve trailing whitespace in variable

Hello, I wondering how I can echo a string without having the trailing whitespace removed. For example I have a string str="TESTING123 " that I need to hash using sha1. I get the correct answer when I run the line below from the terminal $ echo -n "TESTING123 " | openssl sha1... (3 Replies)
Discussion started by: colinireland
3 Replies

10. UNIX for Advanced & Expert Users

Ssh2 key has been overwritten, need a way to restore

I had generated a ssh2 key on my AIX box, to receive files from other AIX and Linux systems. Key Name: id_ssh2_server.pub However this ssh2 key (both public and private keys) has been overwritten, while I was generating another ssh2 key. Now the earlier configured target systems are not able... (3 Replies)
Discussion started by: freakygs
3 Replies
All times are GMT -4. The time now is 03:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy