global date for many scripts in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting global date for many scripts in shell
# 1  
Old 07-07-2008
global date for many scripts in shell

Hi,
I am using shell (#!/bin/bash), i am trying to set my date in a script date.sh, i want this dates to be used by another scripts.

I have tried to make:
#!/bin/bash
begin_date =`date +%Y_%m_%d_%H_%M_%S`,

i also tried:
#!/bin/bash
begin_date =20080709 12:14:11, then i write in other script the date as ($begin_date), but it always gives me begin_date : command not found!

I would like to write the date as numbers like date = 20080721 12:14:11 and then the other script should read it.
I think it should be simple, please can you help me how i can assign this date and time and how i should call it from my other scripts?

Thank you in advance.
# 2  
Old 07-07-2008
Try exporting begin_date
# 3  
Old 07-07-2008
You need to export the variable if you want it to be available to the child shells. Change your date.sh script as

Code:
#!/bin/bash
begin_date =`date +%Y_%m_%d_%H_%M_%S`
export begin_date

# 4  
Old 07-07-2008
Bug numerical date?

Hi,
it still giving me begin_date: command not found.
here is how i changed in date.sh :

#!/bin/bash
begin_date =`date +%Y_%m_%d_%H_%M_%S`
export begin_date

i want to change the date from script to script, so i am tryingto make this script to change the date manually, from today to next month etc... i would like to set like 3 variables for example:
begin_date1 = 2008-07-07_11:12:12 (sysdate)
begin_date2 = 2008_08-08 14:13:34 (next month)
etc ...

Last edited by rosalinda; 07-07-2008 at 07:38 AM..
# 5  
Old 07-07-2008
How are you calling the date.sh script from the other scripts? Can you please post that. I tried the following and it worked:

The following is the date.sh script. Please note that there is no export command in this:
Code:
#!/bin/sh
begin_date=`date +%Y_%m_%d_%H_%M_%S`

The following is prog1.sh where I want to use the begin_date variable. I have sourced the date.sh script in the prog1.sh script:

Code:
#!/bin/sh
# Source the date.sh script
. ./date.sh
echo $begin_date

Now when I execute prog1.sh as follows:

Code:
$ ./prog1.sh
2008_07_07_16_18_18

Just for clarification, the leading "." in line no 3 in prog1.sh is for sourcing a script within another script, and the ./date.sh is to specify that the date.sh is in the current directory (the same directory where the prog1.sh is located), if you have placed the date.sh in some other location such as /home/user01/comm_scripts, you can replace the ./ with the full path of the script.
# 6  
Old 07-07-2008
it works for sysdate

Hi,
Thank you very much for your kind explanation. it works now.
but can you tell me how i can decide my own date, if i decide a date and time different than sysdate it gives me command not found. i would like to precise my own special dates and time like next month 2008-08-09 00:00:02 etc.

i am trying like this, but doesn't work:
#!/bin/sh
begin_date=`2008_08_09_11_12_14 +%Y_%m_%d_%H_%M_%S`

or

#!/bin/sh
begin_date=`2008_08_09_11_12_14`

Thanks in advance, i appreciate it.
# 7  
Old 07-07-2008
The back ticks (also known as grave-accent) you are using means that the contents are to be executed and their result placed in its place, so the lines:
Code:
begin_date=`2008_08_09_11_12_14 +%Y_%m_%d_%H_%M_%S`

or
Code:
begin_date=`2008_08_09_11_12_14`

are incorrect as 2008_08_09_11_12_14 +%Y_%m_%d_%H_%M_%S is not a command in unix and neither is 2008_08_09_11_12_14.

If you want to specify your own date then you can do that as follows:

Code:
begin_date="2008_08_09_11_12_14"

HTH.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Date and time change in global and non global zone

Hi, If I change date and time in global zone, then it will affect in non global zones. During this process what files will get affect in non global zones and which mechanism it's using to change. gloabl zone:Solaris 11.3 X86 TIA (1 Reply)
Discussion started by: Sumanthsv
1 Replies

2. Solaris

Global and non-global zone resource sharing - tricky

hi all, Just a simple question but i cant get the answers in the book - In my globalzone , assuming i have 4 cpus (psrinfo -pv = 0-3), if i set dedicated-cpu (ncpus=2) for my local zone Is my globalzone left with 2 cpus or still 4 cpus ? Does localzone "resource reservation.e.g. cpu in... (6 Replies)
Discussion started by: javanoob
6 Replies

3. Shell Programming and Scripting

Date Arithimetic in shell scripts

Hi, I have a file which has a date extension to it. Like <filename>_01012014. I need to rename the file as 12152013 , that is i need to be able to subtract 15 days from the date of the file name. Please advise how this can be handled using shell scripting. Regards, VN (8 Replies)
Discussion started by: narayanv
8 Replies

4. Shell Programming and Scripting

Global declaration of Array in Shell

Hi, Have assigned values in Array and iterating in while loop and would require values outside of the loop. But its returning NULL. could you please help us how to define Global array declaration in Unix shell scripting? i am using Kron shell. Thanks in advance. (2 Replies)
Discussion started by: periyasamycse
2 Replies

5. UNIX for Dummies Questions & Answers

Shell Scripts - Append a filename with date and time....

Hello, I need to create a shell script that appends a filename to create a name with the date and time appended that is guaranteed to not exist. That is, the script insures you will not overwrite a file with the same name. I am lost with this one. I know I need to use date but after that I am... (3 Replies)
Discussion started by: citizencro
3 Replies

6. UNIX for Dummies Questions & Answers

shell scripts - create a filename with the date appended

I am looking to do something where if I created a file named backup,or whatever it would print a name like “backup_Apr_11_2011”. Thanks citizencro (1 Reply)
Discussion started by: citizencro
1 Replies

7. UNIX for Dummies Questions & Answers

Shell Scripts - shows today’s date and time in a better format than ‘date’ (Uses positional paramete

Hello, I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Discussion started by: citizencro
3 Replies

8. Solaris

How to access ENV variables of non global zones in global zone???

Hi Guys, My requirement is I have file called /opt/orahome/.profile in non global zone. PATH=/usr/bin:/usr/ucb:/etc:/usr/sbin:/usr/local/bin:/usr/openwin/bin:. export PATH PS1="\${ORACLE_SID}:`hostname`:\$PWD$ " export PS1 EDITOR=vi export EDITOR ENV=/opt/orahome/.kshrc export ENV... (1 Reply)
Discussion started by: vijaysachin
1 Replies

9. Shell Programming and Scripting

replace old date in scripts with new date

Hiee .... i want an immediate solution for this.... Let replace.cfg file contains a list of files to be replaced the old dates with new dates...... The script must take 3 parameters.... 1) old date 2) new date 3) .config file Can u get me the solution...... I tried with sed but i... (2 Replies)
Discussion started by: prasanna1983
2 Replies

10. Shell Programming and Scripting

Determine date and time the file was created through shell scripts

Can I determine when the particular file was created, in korn-shell. Can please someone help me. If possible please mail the solution to me. my mail id: bharat.surana@gmail.com (1 Reply)
Discussion started by: BharatSurana
1 Replies
Login or Register to Ask a Question