LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 07:37 AM   #1
NetEng1
Member
 
Registered: Aug 2011
Distribution: ubuntu
Posts: 40

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, 08:04 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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, 09:58 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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, 11:38 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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, 11:47 AM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Code:
bash $ wc <<< "howsit goin?"
 1  2 13
bash $ dash
dash $ wc <<< "howsit goin?"
dash: Syntax error: redirection unexpected
dash $
 
Old 11-10-2015, 04:18 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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



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

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

All times are GMT -5. The time now is 11:46 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