LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 09-30-2014, 12:19 PM   #1
Cleveland_Steve
LQ Newbie
 
Registered: Sep 2014
Posts: 4

Rep: Reputation: Disabled
Test Within Here Document


I've written the following Bash script, but it always returns files found even when the directory is empty. Can someone point out what I'm doing wrong?

Code:
#!/bin/bash

ssh node-prod-2 <<EOF
echo
hostname
echo
cd /opt/data/db1/Logs
pwd
if [ "$(ls -A)" ]; then
echo Files found.
else
echo Directory is empty.
fi
exit
EOF

exit 0
 
Old 09-30-2014, 12:43 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,614

Rep: Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703
To avoid aliases, change the command to $(/bin/ls -A)
 
Old 09-30-2014, 12:57 PM   #3
Cleveland_Steve
LQ Newbie
 
Registered: Sep 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Nope, same result. The script returns files found when the directory is empty.
 
Old 09-30-2014, 02:04 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,836

Rep: Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360
This worked for me, empty and not-empty on Centos 6.
Code:
#!/bin/bash

ssh my-server <<EOF
echo
hostname
echo
pwd
[ "$(ls -A /tmp/not-empty)" ] && echo "Not Empty" || echo "Empty"
[ "$(ls -A /tmp/empty)" ] && echo "Not Empty" || echo "Empty"
exit
EOF

exit 0
Logic test output:
Code:
[root@dev]# mkdir /tmp/empty
[root@dev]# mkdir /tmp/not-empty
[root@dev]# touch /tmp/not-empty/FILE
[root@dev]# [ "$(ls -A /tmp/not-empty)" ] && echo "Not Empty" || echo "Empty"
Not Empty
[root@dev]# [ "$(ls -A /tmp/empty)" ] && echo "Not Empty" || echo "Empty"
Empty
 
Old 09-30-2014, 03:00 PM   #5
Cleveland_Steve
LQ Newbie
 
Registered: Sep 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
That did not working either. The script is getting executed from node-prod-1. In both cases it looks like the (ls -A) command is executing in the directory on node-prod-1, when it should execute in the directory in node-prod-2 via the "ssh node-prod-2 <<EOF"
 
Old 09-30-2014, 05:17 PM   #6
Cleveland_Steve
LQ Newbie
 
Registered: Sep 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
I just figured out my problem. For anyone who comes across this in the future, I needed single quotes around my EOF. Thanks to all who took a look.

Quote:
#!/bin/bash

ssh node-prod-2 << 'EOF'
echo
hostname
echo
cd /opt/data/db1/Logs
pwd
if [ "$(ls -A)" ]; then
echo Files found.
else
echo Directory is empty.
fi
exit
EOF

exit 0
 
  


Reply


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
Load test, boundary test & stress test for USB EHCI/xHCI driver rama_toshiba Linux - Kernel 5 02-29-2012 03:43 PM
html; character encoding per document tag, not whole document TheLinuxDuck Programming 0 08-14-2008 12:12 PM
how do i create a document, then a directory and save the document in it. jtdodge Linux - Newbie 9 06-15-2005 12:03 PM
Convert openoffice document into ms document through cli? bruno buys Linux - Software 0 07-05-2004 10:59 AM


All times are GMT -5. The time now is 05:07 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration