Bash script Output to file fails from script but works from bash - linux

I have this script that checks if the dcos auth login works, but the file i am redirecting the output to is always zero size, when i run the script from bash shell the file is greater than zero . what am i doing wrong ?? , the two functions i use below:
try_to_login()
{
# first needs to be logged as skyusr
# try to login and log the result to tmp file
# Sometimes the file is empty so we try again to login
# if the second time is OK it jumps to check the output
cd /home/skyusr/scripts/
dcos auth login --username=admin --password=admin > /home/skyusr/scripts/tmp.sal
}
check_login_result()
{
# Checks if the output of the login is "Login Successful!"
# If YES then writes to log file, if not sends mail and writes to log.
#export mail_to="salim.bisharat#amdocs.com,anis.faraj#amdocs.com"
export mail_to="salim.bisharat#amdocs.com"
now=$(date)
text_to_check=$(cat /home/skyusr/scripts/tmp.sal)
if [ -s /home/skyusr/scripts/tmp.sal ]
then
if [ "$text_to_check" = "Login successful!" ]
then
echo "$now - Check Successful" >> /home/skyusr/scripts/logs/login_log.log
else
cat /home/skyusr/scripts/logs/mail_temp.log | mailx -s "!!! CRITITCAL -- Check DCOS login !!!" $mail_to
echo "$now - !! ERROR ! Sent mail !! " >> /home/skyusr/scripts/logs/login_log.log
fi
fi
}

In this script you define, but you do not call the functions. Simply append function calls:
# ...
echo "$now - !! ERROR ! Sent mail !! " >> /home/skyusr/scripts/logs/login_log.log
fi
fi
} # ... the last line of your script here
try_to_login # calls here ...
check_login_result

Related

Bash script with multiline heredoc doesn't output anything

I'm writing a script to send SQL output in mail, but it is not executing successfully and is not generating the output I want.
The query generates two columns with multiple rows. How can I generate the output in table format as below?
Below is my code:
#!/bin/bash
ORACLE_HOME= **PATH
export ORACLE_HOME
PATH=$PATH:$ORACLE_HOME/bin
export PATH
TNS_ADMIN= ** PATH
export TNS_ADMIN
today=$(date +%d-%m-%Y)
output=$(sqlplus -S user/pass#service <<EOF
set heading off;
SELECT distinct list_name ,max(captured_dttm) as Last_received FROM db.table1
group by list_name having max(captured_dttm) <= trunc(sysdate - interval '2' hour);
EOF)
if [ -z "$output" ];
then
echo"its fine"
exit
else
echo "
Dear All,
Kindly check we've not received the list for last 2 hour : $output
Regards,
Team" | mailx -S smtp=XX.XX.X.XX:XX -s "URGENT! Please check list FOR $today" user#abc.com
fi
When using a here document, the closing string can't be followed by anything but a newline. Move the closing parenthesis to the next line:
output=$(sqlplus -S user/pass#service <<EOF
...
EOF
)

Diff command stops the execution of the shell-script

I'm creating a script to compare two directories:
for qa_branch in $QA_BRANCHES
do
exist_px=false
info "1qa: $qa_branch"
for px_branch in $PX_BRANCHES
do
info "2px: $px_branch"
if [ "$qa_branch" = "$px_branch" ]; then
exist_px=true
cd test1
checkout_branch "$qa_branch" || error "branch does not exist"
cd ..
cd test2
checkout_branch "$px_branch" || error "branch does not exist"
cd ..
echo
info "compare_branches QA: $qa_branch with PX: $px_branch"
diff -r --exclude=".*" test1/ test2/
info "########################################################################"
fi
done
info $exist_px
echo
done
When the execution reaches the diff command it stops and the execution of the script does not continue. How can i fix this?
The idea is to capture the differences and send them by email.
Thank you!

Bash output limited to echo only

I am writing a bash script to handle by backups. I have created a message function controller that uses functions to handle email, log and output.
So the structure is as:
message_call(i, "This is the output")
Message Function
-> Pass to email function
--> Build email file
-> Pass to log function
--> Build log file
-> Pass to echo function (custom)
--> Format and echo input dependent on $1 as a switch and $2 as the output message
When I echo I want nice clean output that only consists of messages passed to the echo function, I can point all output /dev/null but I am struggling to limit all output except for the echo command.
Current output sample:
craig#ubuntu:~/backup/functions$ sudo ./echo_function.sh i test
+ SWITCH=i
+ INPUT=test
+ echo_function
+ echo_main
+ echo_controller i test
+ '[' i == i ']'
+ echo_info test
+ echo -e '\e[32m\e[1m[INFO]\e[0m test'
[INFO] test
+ echo test
test
+ '[' i == w ']'
+ '[' i == e ']'
Above I ran the echo function alone and the output I want is on line 10, all other output in the sample I don't want.
If you have the line set -x in your script, comment it out. If not, try adding set +x at the top of your script.
If you want to hide all the output from everything except what you're explicitly doing in your echo function you could do something like this:
exec 7>&1 # save a copy of current stdout
exec >/dev/null # redirect everyone else's stdout to /dev/null
ls # output goes to /dev/null
echo My Message >&7 # output goes to "old" stdout

run script inside ksh shell script

I am trying to execute a script ksh that reads a file and execute other script to access the MySQL database. But the second script doesn't return any result. Does anyone know why? please.
#!/bin/ksh
vet=($(cat lasts_tasks.txt))
echo ${vet[#]}
for workunit in ${vet[#]};
do
echo "workunit:$workunit"
exe="/var/www/boinc/m52cc/query_tasks.sh m52cc -workunitResult $workunit;"
echo ""$exe
result=`$exe`
echo $result
done
The results are:
# ./lerArquivo.sh
m52cc_job_5 m52cc_job_6 m52cc_job_7
workunit:m52cc_job_5
/var/www/boinc/m52cc/query_tasks.sh m52cc -workunitResult m52cc_job_5;
workunit:m52cc_job_6
/var/www/boinc/m52cc/query_tasks.sh m52cc -workunitResult m52cc_job_6;
workunit:m52cc_job_7
/var/www/boinc/m52cc/query_tasks.sh m52cc -workunitResult m52cc_job_7;
But when I execute the lines alone, I have the right result:
# /var/www/boinc/m52cc/query_tasks.sh m52cc -workunitResult m52cc_job_7;
105
The ; in your command is interpretted as a literal semicolon, so you're basically running
/var/www/boinc/m52cc/query_tasks.sh m52cc -workunitResult m52cc_job_6\; ;
Just remove the semicolon from your exe variable:
#!/bin/ksh
vet=($(cat lasts_tasks.txt))
echo ${vet[#]}
for workunit in ${vet[#]};
do
echo "workunit:$workunit"
exe="/var/www/boinc/m52cc/query_tasks.sh m52cc -workunitResult $workunit"
echo ""$exe
result=`$exe`
echo $result
done

Run cron job in non-silent mode?

I created a simple linux script that essentially calls sqlplus and puts the results in variable X. I then analyze X and determine whether or not I need to send out a syslog message.
The script works perfectly when I run it from the command line as "oracle"; however when I use crontab as "oracle" and add it to my job, X isn't getting filled.
I could be wrong, but I believe the issue is since cron runs things in silent mode, X isn't actually getting filled, but when I run it manually it is.
Here's my crontab -l result (as oracle):
0,30 * * * * /scripts/isOracleUp.sh syslog
Here's my full script:
#Created by: hatguy
#Created date: May 8, 2012
#File Attributes: Must be executable by "oracle"
#Description: This script is used to determine if Oracle is up
# and running. It does a simple select on dual to check this.
DATE=`date`
USER=$(whoami)
if [ "$USER" != "oracle" ]; then
#note: $0 is the full path of whatever script is being run.
echo "You must run this as oracle. Try \"su - oracle -c $0\" instead"
exit;
fi
X=`sqlplus -s '/ as sysdba'<<eof
set serveroutput on;
set feedback off;
set linesize 1000;
select count(*) as count_col from dual;
EXIT;
eof`
#This COULD be more elegant. The issue I'm having is that I can't figure out
#which hidden characters are getting fed into X, so instead what I did was
#check the string legth (26) and checked that COUNT_COL and 1 were where I
#expected.
if [ ${#X} -eq 26 ] && [ ${X:1:10} = "COUNT_COL" ] && [ ${X:24:3} = "1" ] ; then
echo "Connected"
#log to a text file that we checked and confirmed connection
if [ "$1" == "syslog" ]; then
echo "$DATE: Connected" >> /scripts/log/isOracleUp.log
fi
else
echo "Not Connected"
echo "Details: $X"
if [ "$1" == "syslog" ]; then
echo "Sending this to syslog"
echo "==========================================================" >> /scripts/log/isOracleUp.log
echo "$DATE: Disconnected" >> /scripts/log/isOracleUp.log
echo "Message from sqlplus: $X" >> /scripts/log/isOracleUp.log
/scripts/sendMessageToSyslog.sh "PROD Oracle is DOWN!!!"
/scripts/sendMessageToSyslog.sh "PROD Details: $X"
fi
fi
Here's output when run as oracle from terminal:
Wed May 9 10:03:07 MDT 2012: Disconnected
Message from sqlplus: select count(*) as count_col from dual
*
ERROR at line 1:
ORA-01034: ORACLE not available
Process ID: 0
Session ID: 0 Serial number: 0
Here's my log output when run through oracle's crontab job:
Wed May 9 11:00:04 MDT 2012: Disconnected
Message from sqlplus:
And to syslog:
PROD Details:
PROD Oracle is DOWN!!!
Any help would be appreciated as I'm a new linux user and this is my first linux script.
Thanks!
My Oracle db skills are pretty limited but dont you need to set ORACLE_SID and ORACLE_HOME ?
Check these variables from the command lines and set these variables within cron and retry.

Resources