Script to add users to mariadb - database

I am trying to add users into mariadb. Male users use an airline as password and females use a continent as a password all this info is taken form a csv file.
For some reason my script is not working. here is the script:
#!/bin/bash -p
FILENAME=mgarr048.csv
while IFS=: read -r username first last gender dob state municipality season continent elective f1 airline
do
(( gender == "m" )) || continue
mysql -e GRANT INSERT ON *.* to $username#'%' IDENTIFIED by $airline;
done
csv headers
username,first,last,gender,dob,state,municipality,season,continent,elective,f1,airline

Nowhere in your script you tell it to read FILENAME. Modify your script like this:
#!/bin/bash
FILENAME="mgarr048.csv"
while IFS=, read -r username first last gender dob state municipality season continent elective f1 airline
do
if [[ "$gender" != "m" ]]
then
echo "mysql -e GRANT INSERT ON *.* to $username#'%' IDENTIFIED by "$airline""
fi
done < "$FILENAME"
You said you have a CSV file, so I changed IFS=; to IFS=,. C in CSV is "comma".
You can read a full description on bash loops to read files here: https://mywiki.wooledge.org/BashFAQ/001
EDIT
Since I do not have your database to test, I added echo to the mysql line. Remove it to use this on your system. If you get a mysql error, it is not the script, it is related to your mysql command and/or database.
Tested with that data set:
username1,user1,name1,m,1111-11-11,State1,City1,season1,continent1,elective1,f1,airline1
username2,user2,name2,f,2222-22-22,State2,City2,season2,continent2,elective2,f2,airline2
Output:
mysql -e GRANT INSERT ON *.* to username2#'%' IDENTIFIED by airline2

Related

Looping Through a (CSV) in Bash as variables

I'm trying to use a CSV file with 3 column (Name,Date,Manager) and run a script that will read the CSV and use its variables to send an email for every row
The test columns looks like this
Name Date . Manager
user1 11/11/2018 test1
user2 11/12/2018 test2
user3 11/13/2018 test3
What I have so far is this
while IFS="," read Name Date Manager
do
echo $Name, $Date $Manager
sendEmail -o tls=yes -f YourEmail#gmail.com -t $Manager#domain.com -s smtp.gmail.com:587 -xu my#gmail.com -xp YOURPASSWORD -u "Hello $Name" -m "How are you? Testing $Date."
done < test.csv
echo $Name, $Date $Manager only prints out the first row of the column.
output user1 11/11/2018 test which I will think only send one email instead of 3
Any help would be amazing

How to write a query to find all tables in a db that have a specific column name? HIVE

I've got a database with about 400 tables and I need to find all the tables searching by the column names. Basically I need something like:
select <tables> from <database> where table.columnName='tv';
How can I do this?
below shell script will give you desired result:
output=""|
hive -e 'show tables in <database_name>' |
while read line
do
echo "TABLE NAME : $line"
if eval "hive -e 'describe <database_name>.$line'" | grep -q "tv"; then
output+="Required table name: $line"'\n'
else
output+=""
fi
echo -e "$output"
done

How do I use a shell script to look through directories and alter files?

I've crafted a script that does 90% of what I'm looking to do. It goes into a directory (based on the entered date) and it changes the files I feed into the array. However, I want to alter this script to also contain an array of dates (which are the directory names). It will cycle through the directories, when it finds one of the files from the file name array, it corrects it and moves on until all the files have been corrected. I've tried a few different versions of this, but I am not sure how to implement a second array to continue looking through directories after a file has been corrected.
Currently, my script looks like this:
debug=false
## *****Put file name in quotes******
declare -a arr=("UF19905217" "UG19905218" )
##Put date in DDMMYYYY format for the date the message was original processed.
DATE="25082015"
## now loop through the above array
for i in "${arr[#]}"
do
#if "$debug; then
echo "Fix file named: Inbound_$i.msg"
MSG="Inbound_$i.msg"
#fi
if [ ! -d "$MSG" ]; then
# Enter what you would like changed here. You can copy and paste this command for multiple changes
#DATATYPE
printf "%s\n" ',s/<DataType>EDI<\/DataType>/<DataType>830<\/DataType>/g' wq | ed -s /data1/Inbound/$DATE/$MSG
echo "Complete"
else
echo "Message not found or errored!"
fi
done
I appreciate any help you can provide. Thank you.
I believe you just want to enclose the loop you have in a loop that iterates over the desired directories:
debug=false
## *****Put file name in quotes******
declare -a arr=("UF19905217" "UG19905218" )
##Put date in DDMMYYYY format for the date the message was original processed.
dates=( 25082015 26082015 )
for DATE in "${dates[#]}"; do
for i in "${arr[#]}"; do
MSG="Inbound_$i.msg"
if $debug; then
echo "Fix file named: $MSG"
fi
if [ ! -d "$MSG" ]; then
printf "%s\n" ',s/<DataType>EDI<\/DataType>/<DataType>830<\/DataType>/g' wq | ed -s /data1/Inbound/$DATE/$MSG
echo "Complete"
else
echo "Message not found or errored!"
fi
done
done

mysqldump puts CREATE on the first line with mysqldump

Here's my full bash script:
#!/bin/bash
logs="$HOME/sitedb_backups/log"
mysql_user="user"
mysql_password="pass"
mysql=/usr/bin/mysql
mysqldump=/usr/bin/mysqldump
tbackups="$HOME/sitedb_backups/today"
ybackups="$HOME/sitedb_backups/yesterday"
echo "`date`" > $logs/backups.log
rm $ybackups/* >> $logs/backups.log
mv $tbackups/* $ybackups/ >> $logs/backups.log
databases=`$mysql --user=$mysql_user -p$mysql_password -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`
for db in $databases ; do
$mysqldump --force --opt --user=$mysql_user -p$mysql_password --databases $db | gzip > "$tbackups/$db.gz"
echo -e "\r\nBackup of $db successfull" >> $logs/backups.log
done
mail -s "Your DB backups is ready!" yourmail#gmail.com <<< "Today: "`date`"
DB backups of every site is ready."
exit 0
Problem is when i try to import it with mysql i am gettint error 1044 error connecting to oldname_db. When i opened sql file i have noticed on the first line CREATE command so it tries to create that database with the old name. How can i solve that problem?
SOLVED.
Using --databases parameter in my case is not necessary and because of --databases it was generating CREATE and USE action in the beginning of the sql file, hope it helps somebody else.
Use the --no-create-db option of mysqldump.
From man mysqldump:
--no-create-db, -n
This option suppresses the CREATE DATABASE statements that are
otherwise included in the output if the --databases or --all-databases
option is given.

Add commas to dsquery output

So I'm writing a batch file that will be run by a scheduled task. This file executes a DSQuery for certain attributes of users in AD.
Here's my query:
dsquery user ou=org_unit,dc=company,dc=local -name * -limit 0 | dsget user -dn -email -tel >a.txt
So my question is how can I separate the output by commas? It shows in the text file in nice columns, but it's going to a place to be parsed out by something else for input into a database, and the parsing rules rely on commas.
I need to turn this:
CN=USER NAME,OU=ORG_UNIT,OU=ANOTHER_ORG_UNIT,DC=DOMAINROOT,DC=LOCAL (111)111-1111 email#email.com
Into this:
CN=USER NAME,OU=ORG_UNIT,OU=ANOTHER_ORG_UNIT,DC=DOMAINROOT,DC=LOCAL, (111)111-1111, email#email.com
So how can I edit my query to make it output what I need as shown? Is there something else I can run that'll do it for me?
Never mind. Figured out a CSVDE query to run it.
csvde -m -f test.txt -d "ou=org_unit,dc=domainroot,dc=local" -r objectCategory=Person -l mail,telephoneNumber
Returns what I need, in the format I need.

Resources