LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Enterprise Linux Forums > Linux - Enterprise
User Name
Password
Linux - Enterprise This forum is for all items relating to using Linux in the Enterprise.

Notices


Reply
  Search this Thread
Old 11-15-2014, 05:00 AM   #1
RHCE_ran
Member
 
Registered: Oct 2013
Posts: 90

Rep: Reputation: Disabled
Understanding of statement in Linux shell script


There is a command in one of the script being used in our system (values changed) which is like:

export EMAIL="ABC <abc@example.com>" && /usr/bin/mutt -s "Test subject” -a <attachment> abc@example.com <”Test content”

In the above statement, I am not able to understand below points:

1) export EMAIL
2) How does concatenation like EMAIL="ABC <abc@example.com>" && /usr/bin/mutt work. I understand what

mutt -s "Test subject” -a <attachment> abc@example.com <”Test content”

means, but am not able to understand how the concatenation works.

3) Is a mail going to be sent with the above command?

I hope my question is clear of not understanding above 3 points.

Request a reply to my post.

Regards
 
Old 11-15-2014, 05:17 AM   #2
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
Regarding export EMAIL
this statement exports EMAIL variable from the current process to some "global" level, so this variable may be used by child processes.
Code:
$ a=somevalue
$ echo $a
$ somevalue
$ bash
$ echo $a
$
the last echo $a output will be empty because $a not initialized for the child bash process.
However, if we will set it (export) in global scope, we can use it in child process:
Code:
$ a=somevalue
$ echo $a
$ somevalue
$ export a
$ bash
$ echo $a
$ somevalue
As for concatenation: it isn't ordinary concatenation. "&&" means that the following command will be executed only if previous command exited successfully. Otherwise following command will not be launched

Last edited by Teufel; 11-15-2014 at 05:20 AM.
 
1 members found this post helpful.
Old 11-15-2014, 05:46 AM   #3
RHCE_ran
Member
 
Registered: Oct 2013
Posts: 90

Original Poster
Rep: Reputation: Disabled
Thanks for your answer but the points 2 & 3 are not answered in the context of my question. I have a few add on queries now:

1) Is the export going to be for EMAIL only or the complete concatenation?
2) Could this entire statement not be written as something like,

/usr/bin/mutt -s "Test subject” -a <attachment> abc@example.com <”Test content”

in the script which could also result in a mail being sent?

Regards
 
Old 11-15-2014, 06:19 AM   #4
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
1. export assigns "ABC <abc@example.com>" string to EMAIL variable. This variable will be available for /usr/bin/mutt since it is a child process of your terminal process. I'm not aware of will mutt really use this variable or not, but it will be available for mutt.
2. Of course you can put this string:
/usr/bin/mutt -s "Test subject” -a <attachment> abc@example.com <”Test content”
in the script and run it (don't forget to make script executable).
Will it send email? Do not know. Why wouldn't you test it yourself? I have no mutt installed, so I can't check it. Just replace abc@example.com with real email address and try to send.
 
1 members found this post helpful.
Old 11-15-2014, 09:12 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,705

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by RHCE_ran View Post
There is a command in one of the script being used in our system (values changed) which is like:
export EMAIL="ABC <abc@example.com>" && /usr/bin/mutt -s "Test subject” -a <attachment> abc@example.com <”Test content”

In the above statement, I am not able to understand below points:

1) export EMAIL
2) How does concatenation like EMAIL="ABC <abc@example.com>" && /usr/bin/mutt work. I understand what mutt -s "Test subject” -a <attachment> abc@example.com <”Test content” means, but am not able to understand how the concatenation works.
3) Is a mail going to be sent with the above command?

I hope my question is clear of not understanding above 3 points.
...and putting "how to use command aliases in linux shell script" into Google pulls up close to ONE MILLION HITS. Did you try that?
http://www.computerworld.com/article...-commands.html
http://tldp.org/LDP/abs/html/aliases.html
http://linuxcommand.org/wss0020.php

Out of that many hits, were you not able to find answers? Again, it's odd that someone with an 'RHCE certification', would not know answers to very basic things like this. While they may not have been covered extensively on the exam, any administrator with even a small amount of experience should be familiar with these things.
 
Old 11-23-2014, 09:17 PM   #6
nbritton
Member
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 89

Rep: Reputation: Disabled
Code:
$ true && printf "foo"; echo
foo
$ false && printf "foo"; echo

$ true || printf "foo"; echo

$ false || printf "foo"; echo
foo
From the mutt manual page:
Quote:
from

Type: e-mail address
Default: ""

When set, this variable contains a default from address. It can be overridden using my_hdr (including from send-hooks) and `` $reverse_name''.

Defaults to the EMAIL environment variable's content.

Last edited by nbritton; 11-23-2014 at 09:21 PM.
 
1 members found this post helpful.
Old 11-24-2014, 03:38 AM   #7
RHCE_ran
Member
 
Registered: Oct 2013
Posts: 90

Original Poster
Rep: Reputation: Disabled
Thanks for your answer but I did not understand the || in the lines,

true || printf "foo"; echo
false || printf "foo"; echo

Request a reply.

Regards
 
Old 11-24-2014, 09:26 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,705

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by RHCE_ran View Post
Thanks for your answer but I did not understand the || in the lines,

true || printf "foo"; echo
false || printf "foo"; echo

Request a reply.
Again, did you try to look this up? From the bash scripting tutorial you've been pointed to in the past:
http://tldp.org/LDP/abs/html/ops.html#ANDOR
 
Old 11-24-2014, 10:09 AM   #9
nbritton
Member
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 89

Rep: Reputation: Disabled
Quote:
Originally Posted by RHCE_ran View Post
Thanks for your answer but I did not understand the || in the lines,

true || printf "foo"; echo
false || printf "foo"; echo

Request a reply.

Regards
http://wiki.bash-hackers.org/syntax/basicgrammar#lists
 
1 members found this post helpful.
Old 11-26-2014, 02:29 AM   #10
RHCE_ran
Member
 
Registered: Oct 2013
Posts: 90

Original Poster
Rep: Reputation: Disabled
The link, http://tldp.org/LDP/abs/html/ops.html#ANDOR says nothing helpful, just that:

The && and || operators also find use in an arithmetic context without explaining how || works.

I been trying to get a good explanation but have not got it.

Regards
 
Old 11-26-2014, 02:33 AM   #11
RHCE_ran
Member
 
Registered: Oct 2013
Posts: 90

Original Poster
Rep: Reputation: Disabled
Thanks, I understood ||. It is opposite of &&.

Regards
 
Old 11-26-2014, 09:02 AM   #12
Soderlund
Member
 
Registered: Aug 2012
Posts: 185

Rep: Reputation: 81
Quote:
Originally Posted by RHCE_ran View Post
Thanks, I understood ||. It is opposite of &&.
Not really. They are logic operators. && means "and", and || means "or".

If (foo && bar), and foo is false, then bar is not checked because the condition is false no matter what bar is.

If (foo || bar), and foo is true, then bar is not checked because the condition is true no matter what bar is.

Therefore a statement like this means to run "upgrade" ONLY if "update" succeeds:

Code:
apt-get update && apt-get upgrade
The following means to print "FAILURE!" ONLY if "update" fails:

Code:
apt-get update || echo "FAILURE!"
Which is demonstrated by the code that nbritton posted.

Last edited by Soderlund; 11-26-2014 at 09:06 AM. Reason: grammar
 
Old 11-26-2014, 10:08 AM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,705

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by RHCE_ran View Post
The link, http://tldp.org/LDP/abs/html/ops.html#ANDOR says nothing helpful, just that:

The && and || operators also find use in an arithmetic context without explaining how || works. I been trying to get a good explanation but have not got it.
We can only give you an explanation...we can't make you understand it. That link tells you EXACTLY what those operators do, and how they function, with examples. If you clicked on that link, and read ANYTHING around where it took you, you'd see the "||" is an OR operator, and the "&&" is AND. It even has examples on how they're used. Soderlund gave you an explanation, but you really need to read documentation, and start thinking about and applying the knowledge you're given.
 
Old 11-26-2014, 10:13 AM   #14
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
&& works like this

Code:
cat whatever && echo "it worked"
explained:

Code:
cat whatever (and if that succeeds, then run this ->) echo "it worked"
|| works like the opposite

Code:
cat whatever || echo "it didn't work"
explained:

Code:
cat whatever (and if that does NOT succeed, then run this ->) echo "it didn't work"
You can combine them as well...

Code:
cat whatever && echo "it worked" || echo "it didn't work"

Last edited by szboardstretcher; 11-26-2014 at 10:15 AM.
 
2 members found this post helpful.
Old 11-27-2014, 06:28 AM   #15
RHCE_ran
Member
 
Registered: Oct 2013
Posts: 90

Original Poster
Rep: Reputation: Disabled
I do not think it is correct, please check the posting of szboardstretcher for the correct explanation.

Regards

---------- Post added 11-27-14 at 07:29 AM ----------

&& and || is not AND or OR, please check the posting of szboardstretcher for the correct explanation.

Regards
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
If-statement in Shell Script SeraphimNL Linux - Newbie 4 01-11-2010 12:30 AM
if-then statement and wildcard: shell script Cyberman Programming 7 12-27-2009 07:49 AM
for statement in bourne shell script bujecas Linux - General 3 07-17-2006 07:32 AM
How do I use an If statement within a case in a Shell script?? crazygyhrous Programming 7 01-03-2006 06:41 AM

LinuxQuestions.org > Forums > Enterprise Linux Forums > Linux - Enterprise

All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration