My OS is CentOS 6.3.
I have a bash script that gets mysql slave status and gives user menu with some actions. When an action is made it refreshes itself. Here is a simplified example:
#!/bin/sh
server_status_detect () {
rm -f /root/slave.status
mysql -u${MYSQL_USER} -p${MYSQL_PASS} -e 'show slave status \G' 2>&1 1>/root/slave.status
#some parsing of the file to see if threads are running and so on, done like this
Slave_IO_Running=$(grep '\sSlave_IO_Running:' /root/slave.status | awk '{ print $2 }');
}
themenu () {
clear
server_status_detect
echo -e "Slave running? "$Slave_IO_Running;
echo "Available actions:"
#print actions
}
while true
do
themenu
read answer
case $answer in
e) menu_network_pick;;
d) menu_mount_pick;;
m) menu_master_pick;;
p) menu_repair_replication_pick;;
t) menu_restart_replication_pick;;
r) menu_reboot_pick;;
s) menu_shutdown_pick;;
*) continue;;
esac
done
Everything works as expected, status file is recreated each iteration, it contains actual data. BUT, script shows fist parsed data! Seems like it doesnt parse file every iteration, it only does it first time and after that just shows results it remembered. How could that happen?
Shame on me! I made a stupid mistake!
Just before grepping I had an
ifwhich becomes false for second and concurrent iterations thus greps were even not executing and thus variables remain unchanged. Issue closed, bash is not crazy, it’s all about human 🙂