1834722 Members
2574 Online
110069 Solutions
New Discussion

about Shell Variable

 
SOLVED
Go to solution
yyghp
Super Advisor

about Shell Variable

I tried to run the following command with shell variable:

# ( uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh" ) | mailx -m -s "test" abc@domain.com

I used shell variable "$test":

# test='uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh"'
# echo $test
uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh"

But I failed to run the following:
# ( ${test} ) | mailx -m -s "test" abc@domain.com
"hhawmail.sh": No such file or directory
Null message body; hope that's ok

So, how should I embed "$test" to that command line to let it work exactly like:
# ( uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh" ) | mailx -m -s "test" abc@domain.com

Thanks!
21 REPLIES 21
RAC_1
Honored Contributor

Re: about Shell Variable

test="uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh""

Double quote whole thing.

Also
test='(uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh")'
should also work.
There is no substitute to HARDWORK
Raj D.
Honored Contributor

Re: about Shell Variable

Hi Yyghp ,

1. test="uuencode hhawmail.sh hhawmail.sh; uuencode test_hhawmail.sh test_hhawmail.sh"

2. ( ${test} ) | mailx -m -s "test" abc@domain.com

But not sure about the attachment , need to try out..
Cheers ,

Raj.




" If u think u can , If u think u cannot , - You are always Right . "
TwoProc
Honored Contributor

Re: about Shell Variable

I found that error in quoting - you've got more than you need.

First off, it's a bad idea in general to use variable names that are commands. "test" is actually a command. It's not your problem here, but I'm just letting you know.

runcmd="uuencode hhawmail.sh hhawmail.sh; uuencode hhawmail.sh hhawmail.sh"
$runcmd | mailx -m -s "test" abc@domain.com

now works.

So to assign your command string to the runcmd variable, you just need to enclose the whole command in a single set of quotes. If you're intent on using the quotes around the filename names, the you should put a "\" in front of each quote " character that surrounds each filename.

We are the people our parents warned us about --Jimmy Buffett
yyghp
Super Advisor

Re: about Shell Variable

Thanks RAC, but both ways don't work:

1. double quote:
# test="uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh""
# echo $test
uuencode hhawmail.sh hhawmail.sh; uuencode test_hhawmail.sh test_hhawmail.sh
# ( ${test} ) | mailx -m -s "test" abc@domain.com

But I got an email without attachment, and with email body:
Usage: uuencode [ source ] remotedest

2.
# test='( uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh" )'
# echo $test
( uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh" )
# $test | mailx -m -s "test" abc@domain.com
ksh: (: not found
Null message body; hope that's ok
# ( ${test} ) | mailx -m -s "test" abc@domain.com
ksh: (: not found
Null message body; hope that's ok

But why I can use the following to get two attachments correctly:
# ( uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh" ) | mailx -m -s "test" abc@domain.com

Raj D.
Honored Contributor

Re: about Shell Variable

Hi ,

You have to use runcmd like this , else give error,

(${runcmd})| mailx -m -s "test" abc@domain.com

Cheers ,
Raj
" If u think u can , If u think u cannot , - You are always Right . "
RAC_1
Honored Contributor

Re: about Shell Variable

test='\( uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh" \)'

WILL WORK
There is no substitute to HARDWORK
yyghp
Super Advisor

Re: about Shell Variable

Thanks John, I tried what you said, but failed again, please help:

# runcmd="uuencode hhawmail.sh hhawmail.sh; uuencode hhawmail.sh hhawmail.sh"
# echo $runcmd
uuencode hhawmail.sh hhawmail.sh; uuencode hhawmail.sh hhawmail.sh
# $runcmd | mailx -m -s "test" abc@domain.com

I got an email without attachment, and with email body:
Usage: uuencode [ source ] remotedest

Still not the same as I ran directly:
# ( uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh" ) | mailx -m -s "test" abc@domain.com
Raj D.
Honored Contributor

Re: about Shell Variable

Hi Yyghp ,

Because uuencode syntax you are writing is worng .

it should be like that :

# uuencode file1 file1 | mailx -s 'new program' friends@email.com

And you will get the attachment ,
---------------------------------

For getting dual attachment , do this :

$ (uuencode file1 file1 ; uuencode file2 file2 ) | mailx -m -s "Dual Attachment Test" abc@domain.com
--------------------------------

Hope you got the solution ,

Cheers ,
Raj.

" If u think u can , If u think u cannot , - You are always Right . "
yyghp
Super Advisor

Re: about Shell Variable

Thanks Raj D.
I tried:
# ( ${runcmd} ) | mailx -m -s "test" abc@domain.com

But same error
I got an email without attachment, and with email body:
Usage: uuencode [ source ] remotedest

Still not the same as I ran directly:
# ( uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh" ) | mailx -m -s "test" abc@domain.com
yyghp
Super Advisor

Re: about Shell Variable

Hi RAC

# runcmd='\( uuencode hhawmail.sh hhawmail.sh; uuencode hhawmail.sh hhawmail.sh \)'
# echo $runcmd
\( uuencode hhawmail.sh hhawmail.sh; uuencode hhawmail.sh hhawmail.sh \)
# $runcmd | mailx -m -s "test" abc@domain.com
ksh: \(: not found
Null message body; hope that's ok

yyghp
Super Advisor

Re: about Shell Variable

Thanks Raj D.
Now I am going to mail two attachments, I don't have problem to run:
# ( uuencode "hhawmail.sh" "hhawmail.sh"; uuencode "test_hhawmail.sh" "test_hhawmail.sh" ) | mailx -m -s "test" abc@domain.com
or
# ( uuencode hhawmail.sh hhawmail.sh; uuencode test_hhawmail.sh test_hhawmail.sh ) | mailx -m -s "test" abc@domain.com

But when I used shell variable, then failed.
I have to use shell variable in the shell script, how can I make it?
Thanks!
yyghp
Super Advisor

Re: about Shell Variable

Can anyone try to run the command and give me a solution?
Thanks!
Raj D.
Honored Contributor
Solution

Re: about Shell Variable

Hi Yyghp ,

Here is the solution,

For taking it in single variable its giving error at "(" , hence you have to take in 2 variable , one for Command , one for file(s).

So here we go :

---------------------------------------------------------------------------------------

#!/usr/bin/ksh
############################################
# Storing Variables,
UU_COM=uuencode ; FILE1=hhawmail.sh ; FILE2=test_hhawmail.sh

($UU_COM $FILE $FILE; $UU_COM $FILE2 $FILE2) | mailx -m -s "test" abc@domain.com
############################################

Do you get the email, with attachment. Pls update.

Cheers ,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
yyghp
Super Advisor

Re: about Shell Variable

Great! It works! Thanks Raj!

# uu_com=uuencode
# file1=hhawmail.sh
# file2=test_hhawmail.sh
# echo $uu_com
uuencode
# echo $file1
hhawmail.sh
# echo $file2
test_hhawmail.sh
# ( $uu_com $file1 $file1; $uu_com $file2 $file2 ) | mailx -m -s "test" abc@domain.com

Now I can get two attachemnts without error.
Raj D.
Honored Contributor

Re: about Shell Variable

Hi Yyghp ,

Happy to know that its working ..and enjoy this Forum.

Cheers ,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
TwoProc
Honored Contributor

Re: about Shell Variable

Sorry YY - I didn't actually test it with the mailx part of the command. Since the problem seemed to me to be hung on the $command to do the uuencoding, when I got uuencoded output to standard out from the $cmdrun variable - I assumed that the mailx part would work as written. Didn't think that we were fighting two problems.
We are the people our parents warned us about --Jimmy Buffett
Sandman!
Honored Contributor

Re: about Shell Variable

YY,

You could always do the following within your shell script:

============================================================
# test="uuencode hhawmail.sh hhawmail.sh; uuencode test_hhawmail.sh test_hhawmail.sh"

# (echo $test | /usr/bin/sh ) | mailx -m -s "test" abc@domain.com
============================================================

cheers!
Sandman!
Honored Contributor

Re: about Shell Variable

You can pipe the output of echo to any shell of your choice and for compact code you can always use "sh" instead of "/usr/bin/sh" i.e.

============================================================
# test="uuencode hhawmail.sh hhawmail.sh; uuencode test_hhawmail.sh test_hhawmail.sh"

# (echo $test | sh ) | mailx -m -s "test" abc@domain.com
============================================================

regards!
RAC_1
Honored Contributor

Re: about Shell Variable

Great Sandman. Please elaborate. I was spinning my head for getting this to work.

I tried what I could imagine.

There is no substitute to HARDWORK
Sandman!
Honored Contributor

Re: about Shell Variable

Thank you very much RAC. Matter of fact I'ave learnt immensely from your postings.

:)
cheers!
yyghp
Super Advisor

Re: about Shell Variable

Thank you very much Sandman!
You gave me the final solution for my case, which is exactly what I want!
Thanks!

Points added!