Visit Our UNIX and Linux User Community

Accessing variables outside the function

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accessing variables outside the function
# 1  
Old 10-20-2009
Accessing variables outside the function

I have the below code to find yesterdays date, In this I want to make MONTH, DAY and YEAR as global variableand use it outside the {}, but I am unable to do so , please assist:


date '+%m %d %Y' | 
DAY=`expr "$DAY" - $1` 
case "$DAY" in 
MONTH=`expr "$MONTH" - 1` 
case "$MONTH" in 
YEAR=`expr "$YEAR" - 1` 
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1` 
echo "Yesterday was: $MONTH $DAY $YEAR" 
export MONTH = $MONTH
export DAY= $DAY
export YEAR= $YEAR
echo "Yesterday was: $MONTH $DAY $YEAR" 

Output I am getting:
$ ./ 1
Yesterday was: 10 19 2009
Yesterday was:

# 2  
Old 10-20-2009
Hi mohsin.quazi, because you pipe the date command to the curly braces, it is no longer a code block but a subshell.
i.e. compare the difference between
date '+%m %d %Y' | 

date '+%m %d %Y' |

The export of variables does not help you since that only works from shell to subshell, not vice versa.

On a different note if you have GNU date, you can do this:
me@tinybird:~/test$ date '+%m %d %Y'
10 21 2009
me@tinybird:~/test$ date -d yesterday '+%m %d %Y'
10 20 2009

Last edited by Scrutinizer; 10-20-2009 at 08:42 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #296
Difficulty: Easy
Linux Mint 2.0 was based on Ubuntu 6.10 and used the Ubuntu 6.10 package repositories and using it as a codebase.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

GDB problem accessing static variables in C

Hi, Can anyone explain this please..... This is on AIX with GDB (tried a few versions). It only happens when program compiled in 64 bit and only with static variables.... A simple test program... ### snip #include <stdio.h> main() { static int n; n = 6; printf("hello %d\n", n);... (0 Replies)
Discussion started by: bagpussnz
0 Replies

2. Shell Programming and Scripting

Error during Accessing Global variable inside function taskName="DB-Backup" starttime=`date` email() { subject="$taskName" ": " $* " at `date` " mutt -s "$subject" $emailid < /dev/null } email "Starting" #do my stuff email "Finished" The above code gives following error ./ line 6: :... (5 Replies)
Discussion started by: nitiraj.rathore
5 Replies

3. Shell Programming and Scripting

PERL on windows accessing variables from a config file

Folks, I'm a perl moron, so please speak very slowly. : ) I'm modifying a build script that starts up an apache server. Now there is a .config file that hardcodes an old webserver path like this c:\oldWebserver. Now I don't want that hardcoded value, rather wish to use an... (3 Replies)
Discussion started by: MarkoRocko
3 Replies

4. Shell Programming and Scripting

Problem in accessing variables outside shell

Hi, I have a shell script wherein i am doing some file operations and storing the data in some variables. I am exporting these variables as i need to use them outside shell. Then within the shell i am launching GDB session hoping that i will be able to access the exported variables in the GDB... (2 Replies)
Discussion started by: jsantosh
2 Replies

5. Shell Programming and Scripting

Accessing Shell Variables in awk or sed

Hello, I wonder if it is possible to pass and use variables from shell environment into sed or awk. I am trying to achieve something similar to the following using sed or awk: var=some_regular_expression grep "$var" filename # Will extract lines from filename The following code,... (3 Replies)
Discussion started by: nasersh
3 Replies

6. Programming

accessing unix variables in oracle

Hi, consider the following script. ip='***.***.**.**' user='****' pw='******' ftpresults=`ftp -nv $ip<<EOF user $user $pw cd /home/oracle/practice size $1 bye EOF` fname=$1 echo $ftpresults sqlplus -s tms/tms@dev45 <<"EOF" insert into remote_file_sizes (file_name,file_size)... (1 Reply)
Discussion started by: ravi raj kumar
1 Replies

7. Shell Programming and Scripting

Accessing Variables from .conf file

I can't figure out how to access variables that are stored in a separate file. Can someone let me in on the secret? Please, and thank you. -Kevin (7 Replies)
Discussion started by: wayne1411
7 Replies

8. Shell Programming and Scripting

accessing ksh variables from awk

hi everybody! i am running this ksh script for replacing a set of strings by another set of new ones. i am getting both these from a file. also, the strings that i want to replace, are sub-strings(can occur more than once in each chunk) in a big chunk of data that i have bulk-copied(bcp utility)... (1 Reply)
Discussion started by: trupti wagh
1 Replies

9. Shell Programming and Scripting

accessing a function in ksh from perl

is it possible? Because I know we could use open(A, `abc.ksh`); to access a ksh, but is it possible to access just one (or more) function from the ksh script? (2 Replies)
Discussion started by: ahtat99
2 Replies

10. Shell Programming and Scripting

accessing variables declared in another perl script

Hi all, I have a perl script which declares two variables and calls another perl script which accesses those variables. But I am unable to access the variables in the called script. My script is as follows: my $ENV{a}="20"; system("perl"); and my contains: print... (3 Replies)
Discussion started by: gurukottur
3 Replies

Featured Tech Videos