I have a bash script with a variable for mysql connection details and a variable for the query I want to run...
#!/bin/bash
#listed in the for loop are query numbers = names
declare -a array=(
"1=Unprocessed===\"select count(*) from db where test='unknown'\""
"2=Total===\"SELECT count(*) FROM db\""
)
sqlconnection=$"/usr/local/bin/mysql -u user -passwd -h xx.xx.xx.xx -se"
for i in "${array[#]}";
do
#map s=selection n= name q=== sql query
s=$(echo "$i" | awk -F "=" '{print $1}')
n=$(echo "$i" | awk -F "=" '{print $2}')
q=$(echo "$i" | awk -F "===" '{print $2}')
$sqlconnection \("$query"\)
done
while I can get the sql connection and query to echo out it doesn't actually run the query! - there's gotta be something I'm missing :)
sqlconnection should be a function, not a string. Also, you don't need to quote the entire command in each array element; quoting $q when you call your function is sufficient.
declare -a array=(
"1=Unprocessed===select count(*) from db where test='unknown'"
"2=Total===SELECT count(*) FROM db"
)
sqlconnection () {
/usr/local/bin/mysql -u user -passwd -h xx.xx.xx.xx -se "$#"
}
for i in "${array[#]}"; do
IFS== read s n _ _ q <<< "$i"
sqlconnection "$q"
done
Related
i have a table that i want to query to that and get some values from my bash script and use it in while loop,when i use one column it works as a champ but i cant use more than one i get error like:
read: `IP, PL_Seq': not a valid identifier
here is my SELECT result
and here is may bash script
sql="SELECT IP,PL_Seq FROM mytabel WHERE FLAG=0 AND CIDR =24";
i=0
while IFS=$'\t' read IP, PL_Seq ;do
IP[$i]=$IP
PL_Seq[$i]=$PL_Seq
((i++))
echo $IP
echo $PL_Seq
done < <(mysql TestDB -u $DB_USER --password=$DB_PASSWD -N -e "$sql")
You are nearly there, you just need to remove the comma from the line:
while IFS=$'\t' read IP, PL_Seq ;do
and so:
sql="SELECT IP FROM mytabel WHERE FLAG=0 AND CIDR =24";
i=0
while IFS=$'\t' read IP PL_Seq ;do
IP[$i]=$IP
PL_Seq[$i]=$PL_Seq
((i++))
echo $IP
done < <(mysql TestDB -u $DB_USER --password=$DB_PASSWD -N -e "$sql")
i am trying to monitor my hosts through ping. the hosts information are in a mysql table. i m using fping command the code is as followings
#/bin/sh
id=$(mysql -B --column-names=0 -uroot -pPassword -D monitor -e "SELECT ipv4 FROM nics WHERE icmp=1");
result=$(fping -c 10 $id |grep 'xmt/rcv/%loss');
#echo $result;
for line in $result;
do
echo $line
done
the output is
111.125.140.6 : xmt/rcv/%loss = 10/10/0%, min/avg/max = 234/234/235
123.135.140.7 : xmt/rcv/%loss = 10/0/100%
111.125.140.1 : xmt/rcv/%loss = 10/10/0%, min/avg/max = 230/231/231
111.125.130.2 : xmt/rcv/%loss = 10/10/0%, min/avg/max = 234/234/234
now i want to get IP, loss and average record from each line and input the data in a new table
thanks in advance
Using a while loop (instead of a for loop) with awk:
while read -r line
do declare $(awk '{split($0,a,"[%/ ,]"); print \
"ip="a[1],"send="a[8],"rec="a[9],"loss="a[10],"min="a[17],"avg="a[18],"max="a[19]}' <<< $line);
## variables created: [ip, send, rec, loss, min, avg, max]
## test with: echo $ip, $send, $rec, $loss, $min, $avg, $max
## now you can insert the variables into your new SQL table:
echo "INSERT INTO newtable (IP,SEND,REC,LOSS,MIN,AVG,MAX) \
VALUES ('$ip','$send','$rec','$loss','$min','$avg','$max');"
done< <(printf '%s\n' "$result") | mysql -uroot -pPassword foobar
This will to declare individual variables from your $line strings which you can insert into SQL.
Then i coordinate my script.sh in an Oozie there is nothing in a variables S.
Here is shell script
S=$(hive -S -hiveconf MY_VAR1=$DB -hiveconf MY_VAR2=$avgpay -hiveconf MY_VAR3=$Date_LastDay -hiveconf MY_VAR4=$Date_LastNmonth -f bpxp.hql)
echo $S "S"
S1=( $( for k in $S ; do echo $k ; done ) )
cntn=${#S1[#]}
for (( p=0 ; p<$cntn; p=p+5 ))
do
`mysql -h$mysqlhost -u$mysqluser -p$mysqlpass $mysqldb -e "INSERT INTO weekstat (timeshift, partnerid, avg_value, processdate, weekday) VALUES ('${S1[p]}', '${S1[p+1]}', '${S1[p+2]}', '${S1[p+3]}', '${S1[p+4]}');"`
done
Mysql commands works fine with another variables
Here is bpxp.hql
...
hive -e "select * from ${hiveconf:MY_VAR1}.weekstat;"
Then i run script from shell it works fine. I try to use bpxp.hql without this line hive -e "select * from ${hiveconf:MY_VAR1}.weekstat;" and write it in shell S=hive -e "select * from $DB.weekstat;" but nothing change.
Where is my mistake?
Whatever you done is an efficient way, but minor change as mentioned below:
S=$(hive -S -hiveconf MY_VAR1=$DB -hiveconf MY_VAR2=$avgpay -hiveconf MY_VAR3=$Date_LastDay -hiveconf MY_VAR4=$Date_LastNmonth -f bpxp.hql)
In bpxp.hql keep the query
select * from $DB.weekstat;
You don't need to mention hive -e in the hql script, the command above will automatically executes and gives you the output, and stores into S.
Please let me correct if I'm wrong.
I want to get a count of loop in reverse in output to know how many loop remaining I miss to finish.
#!/bin/bash
datenow=$(date +"%F")
logfile="table-"$datenow".log"
rm -f table.sql
mysql --login-path=myloginpath -h xhost -N -e \
"select count(*) from database.table a where a.field = 0;" | tee -a $logfile
mysqldump --login-path=myloginpath-h xhost database table > table.sql
read -p "Insert Host Separated by comma ',' : " localcs
mysql --login-path=myloginpath -h xhost -N -e \
"select n.ipaddress from database.hosts n where n.hosts in ($localcs);" > ip.txt
for ip in $(cat ip.txt);
do
mysql --login-path=myloginpath -h $ip "database" < "table.sql"
echo $ip | tee -a $logfile && mysql --login-path=myloginpath -h $ip -N -e \
"select count(*) from database.table a where a.field = 0;" | tee -a $logfile
done
Something like this:
100
(mysql output)
99
(mysql output)
98
(mysql output)
.....
You just need to know how many lines are in the stream in advance.
num=$(wc -l < ip.txt)
while read -r ip; do
# Do your work, use $num as needed
# Then decrement num
num=$((num-1))
done < ip.txt
By the way, this shows the recommended way of iterating over a file; don't use a for loop.
please advise how to use select statement result as a variable and pass it further to the script. Simple script:
#!/bin/sh
# --mysql part START--
pwd="pass"
D="db"
mysql -uuser -p$pwd -D$D -s -N -e "SELECT port FROM table where username='$1';"
/usr/bin/mysql -uroot -p$pwd -D$D<< eof
eof
port=$(mysql Select) # declaring select result as a variable , probably wrong
echo "port $port;" >> /tmp/blabla.txt # <- this part is not working
Please advise.
Thanks in advance
You can put it into an a variable, follow this example:
#!/bin/sh
# --mysql part START--
pwd="pass"
D="db"
user="user"
port=$(mysql -u $user -p $pwd -D $D -s -N -e "SELECT port FROM table where username='$1';")
echo "PORT: $port" > /tmp/blabla.txt