Error during Accessing Global variable inside function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error during Accessing Global variable inside function
# 1  
Old 02-22-2012
Error during Accessing Global variable inside function

Code:
emailid=myemail@xyz.com
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
./dbbackup.sh: line 6: : : command not found
./dbbackup.sh: line 6: : : command not found

Basically the assignment of variable " subject="$taskName" ": " $* " at `date` " " causes the error because of using the $taskName in it. Because when i remove $taskName from it it gives no error. But this kind of assignment works outside the function.

I am relatively new to functions in shell and any help is appreciated.
Thanks
-Nitiraj
# 2  
Old 02-22-2012
change the subject to

you need to escape the " (double quotes)

Code:
subject="$taskName\" \": \" $* \" at `date` "

# 3  
Old 02-22-2012
Whatever variable you defined outside the function to be exported. Exprted variable can be used inside function

Eg :: export taskName
# 4  
Old 02-22-2012
Try this...
Code:
subject="$taskName :  $*  at `date` "

--ahamed
# 5  
Old 02-22-2012
Sorry forget last comment. Yeah, this is your problem. There is unwanted quote space between $*.

subject="$taskName" ": "$*" at `date` "

Whenever you are using $* and $@ it should be quoted properly.
# 6  
Old 02-22-2012
Further to ahmed101, you also need a pair of escaped quotes on the mutt line.

Code:
        subject="$taskName :  $*  at `date` "
        echo mutt -s \""$subject"\" $emailid < /dev/null


@sanoop etc.
The original problem was that ":" (colon) is a Shell command and it was trying to execute ": " (colon with an argument of a space character).

Last edited by methyl; 02-22-2012 at 10:51 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to call variable inside a function globally?

Hi Gurus, Is there a way to call a variable inside a function anywhere within the script? Thanks. BR, Ernesto (2 Replies)
Discussion started by: ernesto
2 Replies

2. Shell Programming and Scripting

Issue with accessing value inside while loop, outside it

Hi, GetName() { if then echo " Please enter the name: " read Name tempvar=0 while read line do if then tempvar=`expr $tempvar + 1` echo $tempvar ... (10 Replies)
Discussion started by: rituparna_gupta
10 Replies

3. Shell Programming and Scripting

Perl: accessing reference to variable inside hash.

Below is hash which contains reference to variables: my %mandatoryFields = ( 1 => \$msgtype, 2 => \$switchtype, 3 => \$card_nbr, 4 => \$natv_tran_type_code, 5 => \$amt_1 ); This... (0 Replies)
Discussion started by: som.nitk
0 Replies

4. Programming

design query: where to lock mutex for function accessing global value?

Hi, Suppose I have a function that accesses and increments a global variable . This function is run as part of thread. One locks mutex in the function and unlocks it after the processing is done. Is there any alternative way? Thanks in advance. (1 Reply)
Discussion started by: sanjayc
1 Replies

5. Solaris

Accessing global-zone installed application

Hi, Is it possible to access application installed on global-zone from a non-global zone? Is there any configuration to achieve the above requirement? Tried looking up information but unable to find. Thanks in advance. Eugene (3 Replies)
Discussion started by: srage
3 Replies

6. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

7. UNIX for Dummies Questions & Answers

Accessing Global Env Variable

Greetings: I need to remove 'RUBYOPT' env variable to install MacRuby. I see it via $env (tchrc). I checked my (local) .tcshrc, .login, .profile files: not defined there. Apparently, it's not set locally. I know this RUBYOPT is global, since I can see it in another account on my... (4 Replies)
Discussion started by: UncleRic
4 Replies

8. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

9. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

10. UNIX for Dummies Questions & Answers

Accessing redirected file inside script

hi, Is there a way to access the redirected file inside the script. Here is what the command line looks like: $ shar * > archive_file.arc I know I can't access the name of archive_file.arc with positional parameters like $1, $2.. Is there any way to figure out what file the output of the... (3 Replies)
Discussion started by: milhan
3 Replies
Login or Register to Ask a Question