LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-10-2015, 08:37 AM   #1
NetEng1
Member
 
Registered: Aug 2011
Distribution: ubuntu
Posts: 37

Rep: Reputation: Disabled
Bash unexpected redirect.


I am attempting to write a bash script that backs up a mysql database and sends an e-mail stating whether if failed or was successful. I am using the following:

Code:
if [ $check != 0 ]; then
       mail -s "Mysql Backup Successful" User1@domain.com <<< " I do not believe it, Please verify."

else

       mail -s "Mysql Backup FAILED" User1@domain.com <<< " The blasted thing FAILED, Check your stuff."
fi
When I run it, I get "unexpected redirect" error.

I created separate scripts for each case and just copied and pasted the "mail -s ...." lines into them respectively. Then I called the newly create scripts as follows:

Code:
if [ $check != 0 ]; then

        /home/fmadmin/mail_success.sh
else

        /home/fmadmin/mail_fail.sh
fi

Now it works fine. Can someone advise me what is wrong with the first approach?

Thanks,
Keith
 
Old 11-10-2015, 09:04 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,825
Blog Entries: 12

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
Wondering myself, because a modified form of your first rendition does work for me, providing I put in a relevant email address for my machine. The only other difference is that my if test is:
Code:
if [ 1 == 1]; then
merely because I didn't take the time to have a $check variable in my script.

Note that when you type "<<<" it can cause behavior like this, which auto-appears in my editor:
Code:
<<EOF
<
EOF
But if I were to inadvertently not erase the EOF at the bottom it would just tell me that it is an invalid command.
 
Old 11-10-2015, 10:58 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
So to confirm, you have a script with ONLY those 5 lines and you get the error?

Also, have you tried using set -xv in your script to see what is happening?
 
Old 11-10-2015, 12:38 PM   #4
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,692

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
I wonder if your interactive shell is a different shell than the one that executes your scripts. The <<< construct is only supported by bash, I believe.
 
Old 11-10-2015, 12:47 PM   #5
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,692

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Code:
bash $ wc <<< "howsit goin?"
 1  2 13
bash $ dash
dash $ wc <<< "howsit goin?"
dash: Syntax error: redirection unexpected
dash $
 
Old 11-10-2015, 05:18 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
I'd also add a couple of syntax points.

1. use [[ ]] rather than [ ] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
2. use the correct comparison op ( -ne rather than != ) http://tldp.org/LDP/abs/html/comparison-ops.html
 
  


Reply

Tags
bash scripting, redirect


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash unexpected end of file sithrebel15 Programming 4 01-21-2015 11:08 AM
Bash globbing [A-Z] vs. [a-z]: unexpected behaviour kikinovak Slackware 9 12-03-2014 08:40 AM
[SOLVED] Unexpected Bash behaviour in 14.1 thirteen_engines Slackware 4 12-11-2013 10:57 AM
bash -D (or bash --dump-strings) unexpected result Ubx Linux - General 2 02-24-2011 07:41 PM
[bash] unexpected end of file waqasdaar Programming 6 04-03-2009 10:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:55 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration