Unexpected end of file in BASH - string

Writing a simple bash script to do some checks for me in the morning:
One part is pulling down some html files and making sure that they exist.
The other part is ensuring some local files are present, and emailing if they are not.
The problem I am facing is that I am receiving a "syntax error: unexpected end of file" and I can't really understand why it i occuring.
Here is a simplified version of the code:
for myHost in $HOSTS
do
result=$(wget -T $TIMEOUT -t 1 $myHost -O /dev/null -o /dev/stdout)
result2=$(echo $result | grep "awaiting response")
connected=$(echo $result | grep "404");
if [ "$connected" != "" ]; then
for myEMAIL in $EMAIL
do
echo -e "$(date) - $myHost is down! \n This is an automated message." | mailx -r "box.email.com" -s "$SUBJECT" $myEMAIL
done
fi
done
numoffiles=`find . -maxdepth 1 -mtime -1 | grep -i .html | wc -l`
if [ "$numoffiles" -ne 5 ]; then
FILE=$(find . -maxdepth 1 -mtime -1 -print| grep -i .html)
mailx -r "box.email.com" -s "FILE MISSIN" "$EMAIL" << EOF
EOF
fi
from using sh -x I can see that it gets to assigning the number of reports to the var "numoffiles", but then it just believes that is the end of the file. has anyone got any suggestions?

There should not be any space before the end of heredoc label:
EOF
^^^
Change it to
EOF

Related

How Do I not send file if data is not available via linux

I have the following code
for f in *#gmail.com*
do
tmp=$( echo $f | awk -F'[_]' '{print $1}')
tmp1=$( echo $f | awk -F'[_]' '{print $2 ,$3, $4,$5,$6}')
newname1=${tmp1}
newname=${tmp}
echo $newname
echo $newname1
mv "$f" "${newname1}.csv"
mail -a "${newname1}.csv" -s "working" $newname </dev/null
mv "${newname1}.csv" $f
rm -f "$f" "${newname1}.csv"
done
echo "mail -a "$newname1" "working" $newname </dev/null "
This script works even if the CSV file contains no data. But no mail should be send, if that CSV file is empty.
You simply send mail only, if the CSV file has more than one line:
if (( $(wc -l <YOUR_FILE.csv) > 1 ))
then
mail ....
fi
Check if file size is Not zero then send mail
if [ -s ${newname1}.csv ]
then
mail -a "${newname1}.csv" -s "working" $newname </dev/null
fi
If File size is not empty and have some header then you can use this code to calculate no. of rows from file and check if no. of lines greater then one(header)
row=`cat ${newname1}.csv|wc -l`
if [ $row -gt 1 ]
then
mail -a "${newname1}.csv" -s "working" $newname </dev/null
fi

sed is not working for commenting a line in a file using bash script

I have created a bash script that is used to modify the ulimit of open files in the RHEL server.
so i have reading the lines in the file /etc/security/limits.conf and if the soft/hard limit of the open files are less than 10000 for '*' domain i am commenting the line and adding a new line with soft/hard limit as 10000.
The Script is working as designed but the sed command to comment a line in the script is not working.
Please find the full script below :-
#!/bin/sh
#This script would be called by '' to set ulimit values for open files in unix servers.
#
configfile=/etc/security/limits.conf
help(){
echo "usage: $0 <LimitValue>"
echo -e "where\t--LimitValue= No of files you want all the users to open"
exit 1
}
modifyulimit()
{
grep '*\s*hard\s*nofile\s*' $configfile | while read -r line ; do
firstChar="$(echo $line | xargs | cut -c1-1)"
if [ "$firstChar" != "#" ];then
hardValue="$(echo $line | rev | cut -d ' ' -f1 | rev)"
if [[ "$hardValue" -ge "$1" ]]; then
echo ""
else
sed -i -e 's/$line/#$line/g' $configfile
echo "* hard nofile $1" >> $configfile
fi
else
echo ""
fi
done
grep '*\s*soft\s*nofile\s*' $configfile | while read -r line ; do
firstChar="$(echo $line | xargs | cut -c1-1)"
if [ "$firstChar" != "#" ];then
hardValue="$(echo $line | rev | cut -d ' ' -f1 | rev)"
if [[ "$hardValue" -ge "$1" ]]; then
echo ""
else
sed -i -e 's/$line/#$line/g' $configfile
echo "* hard nofile $1" >> $configfile
fi
else
echo ""
fi
done
}
deleteEofTag(){
sed -i "/\b\(End of file\)\b/d" $configfile
}
addEofTag()
{
echo "#################End of file###################" >> $configfile
}
#-------------Execution of the script starts here ----------------------
if [ $# -ne 1 ];
then
help
else
modifyulimit $1
deleteEofTag
addEofTag
fi
The command sed -i -e 's/$line/#$line/g' $configfile when executed from the terminal is working absolutely fine and it is commenting the line but it is not working when i am executing it from the unix shell script.
interpolation does not work in single quote
use double quote and try
sed -i -e 's/$line/#$line/g'
sed -i -e "s/$line/#$line/g"
also you might try:
sed -i -e s/${line}/#${line}/g
as this will tell the script to take the value of the variable instead of variable as such.

Monitor log and send Alert mail using shell script in Linux

I created .sh file to grep specific words in log and will send a mail to particular emailID (Set Cronjob to run this script every 1hour). Its working as expected, But the Problem is if there is no error also I am getting Empty Mail.
Needs to get Mail, if we caught error and DBError file has content. Kindly please help me on this to resolve this issue.
#!/bin/bash
if [ ! -e DBErrors ] ; then
grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > DBErrors
mail -s "ALERT: sqlException" Jayaram.Ponnusamy#gmail.com < DBErrors
else
comm -23 <(grep "sqlException" /opt/apps/cms/logs/cms-runtime.log) DBErrors | mail -s "ALERT: sqlException" Jayaram.Ponnusamy#gmail.com
grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > DBErrors
fi
Thanks
Jayaram
Could you try this;
#!/bin/bash
dbErrors="/tmp/DBErrors"
if [ ! -e "$dbErrors" ]; then
grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > $dbErrors
mailbody=$(grep "sqlException" /opt/apps/cms/logs/cms-runtime.log); [[ -n "$mailbody" ]] && mail -s "ALERT: sqlException" Jayaram.Ponnusamy#gmail.com
else
mailbody=$(comm -23 <(grep "sqlException" /opt/apps/cms/logs/cms-runtime.log| sort -n) <(sort -n "$dbErrors")); [[ -n "$mailbody" ]] && mail -s "ALERT: sqlException" Jayaram.Ponnusamy#gmail.com
grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > $dbErrors
fi

Put grep output inside variable from a loop

I have CentOS and this bash script:
#!/bin/sh
files=$( ls /vps_backups/site )
counter=0
for i in $files ; do
echo $i | grep -o -P '(?<=-).*(?=.tar)'
let counter=$counter+1
done
In the site folder I have compressed backups with the following names :
site-081916.tar.gz
site-082016.tar.gz
site-082116.tar.gz
...
The code above prints :
081916
082016
082116
I want to put each extracted date to a variable so I replaced this line
echo $i | grep -o -P '(?<=-).*(?=.tar)'
with this :
dt=$($i | grep -o -P '(?<=-).*(?=.tar)')
echo $dt
however I get this error :
./test.sh: line 6: site-090316.tar.gz: command not found
Any help please?
Thanks
you still need the echo inside the $(...):
dt=$(echo $i | grep -o -P '(?<=-).*(?=.tar)')
Don't use ls in a script. Use a shell pattern instead. Also, you don't need to use grep; bash has a built-in regular expression operator.
#!/bin/bash
files=$( /vps_backups/site/* )
counter=0
for i in "${files[#]#/vps_backups/site/}" ; do
[[ $i =~ -(.*).tar.gz ]] && dt=${BASH_REMATCH[1]}
counter=$((counter + 1))
done

Unexpected end of file when running

I have a small crash detect script for a teamspeak server. The only issue is that I keep getting a syntaxt error about an unexpected end of file. I am not sure if I am missing something in this from all the guides I have been following. I have also ran dos2unix since I do alot of the coding in sublime text 2 on windows.
#!/bin/bash
TEAMSPEAK=`ps ax | grep ts3server_linux_amd64 | grep -v grep | wc -l`
if [ $TEAMSPEAK -eq 1 ]; then
exit
else
cd /home/ryahn/ts3
if [ -f ts3server.pid ]; then
rm -f ts3server.pid
echo "File here"
. ./home/minecraft/ts3/ts3server_startscript.sh start
fi
You are missing a fi for outer if condition.
Keep your code indented to understand it better:
if [ $TEAMSPEAK -eq 1 ]; then
exit
else
cd /home/ryahn/ts3
if [ -f ts3server.pid ]; then
rm -f ts3server.pid
echo "File here"
. ./home/minecraft/ts3/ts3server_startscript.sh start
fi
fi
btw you can shorten your piped commands using pgrep:
TEAMSPEAK=$(pgrep -f ts3server_linux_amd64|wc -l)

Resources