RHCE QUAZI MAHMUDUL HUQ
Bash shell Scripts
Repeat Answer:
When user will execute a command with Dhaka, he will get reply Bangladesh, when execute
a command with Bangladesh, will get reply Dhaka.
[root@localhost ~]# vim /root/script
#!/bin/bash
if [[ $1 == “Dhaka” ]]; then
echo “Bangladesh”
elIf [[ $1 == “Bangladesh” ]]; then
echo “Dhaka”
else
echo “usage: /root/script <Dhaka|Bangladesh>” >&2
fi
[root@localhost ~]# /root/script 2> [Link]
[root@localhost ~]# chmod a+x /root/script
Check:
[root@localhost ~]# /root/script bar
foo
[root@localhost ~]# /root/script foo
bar
[root@localhost ~]# /root/script lkloiiljjbugyfy
usage: /root/script <Dhaka|Bangladesh>
RHCE QUAZI MAHMUDUL HUQ
Dynamically User Creation:
[root@localhost ~]# vim userlist
Kamal
Salam
Robi
Jony
Tomy
Jerry
Shumon
ali
putul
[root@localhost ~]# vim /root/mkaccount
#!/bin/bash
if [ ! $1 ]; then
echo “no output found”
exit 1
fi
if [ ! -e $1 ]; then
echo “usages: /root/mkaccount <FILE NAME>” 2
exit 1
else
users=$(cat $1)
for user in $users
do
useradd $user
echo “abcd1234” | passwd --stdin $user
done
fi
[root@localhost ~]# chmod a+x /root/mkaccount
[root@localhost ~]# /root/mkaccount
no output found
[root@localhost ~]# /root/mkaccount userlist
RHCE QUAZI MAHMUDUL HUQ
Bash shell script to perform a backup of all MariaDB databases on a server
[ root@serverX - ] # yum install -y mariadb-server
[ root@serverx -]# systemctl enable mariadb
[ root@serverx - ] # systemctl start mariadb
[ root@serverX - ] # mkdir /dbbackup
[ root@serverx - ] # mysql -u root -e 'SHOW DATABASES'
+--------------------+
I Database
+--------------------+
information_schema
mysql
performance_schema
test
+--------------------+
Utilize the - - skip - column - names a nd - E formatting options to simplify the output for parsing.
[ root@serverX -]# mysql - - skip-column-names - E - u root - e ' SHOW DATABASES '
* * * * * * * * * * * * * * * * * * * * * * * * * * * 1 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
informat ion_schema
* * * * * * * * * * * * * * * * * * * * * * * * * * * 2 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
mysql
* * * * * * * * * * * * * * * * * * * * * * * * * * * 3 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
perfo rmance_schema
* * * * * * * * * * * * * * * * * * * * * * * * * * * 4 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
test
Exclude the row header lines and the two system databases from the output.
[ root@serverX - ] # mysql - - skip - column - names -E -u root -e 'SHOW DATABASES' | grep -v '^*' | grep -v
'^information_schema$ ' | grep – v ' ^performance_schema$ '
mysql
test
Create your script. Store the MySQL user, the formatting options, the SHOW DATABASES command, and the backup
directory as variables.
[ root@serverX - ] # vim /usr/local/sbin/dbbackup
# ! /bin/bash
# Variables
DBUSER= root
FMTOPTIONS='- - skip-column-names -E'
COMMAND='SHOW DATABASES '
BACKUPDIR=/dbbackup
# Backup non - system databases
for DBNAME in $(mysql $FMTOPTIONS -u $DBUSER -e "$COMMAND" | grep -v "* | grep -v information_schema | grep -v
performance_schema); do
RHCE QUAZI MAHMUDUL HUQ
echo "Backing up \"$DBNAME\""
mysqldump -u $DBUSER $DBNAME > $BACKUPDIR/$[Link]
done
# Add up size of all database dumps
for DBDUMP in $BACKUPDIR/*; do
SIZE=$ (stat - -printf "%s\n " $DBDUMP )
TOTAL=$[ $TOTAL + $SIZE ]
done
# Report name, size, and percentage of total for each database dump
echo
for DBDUMP in $BACKUPDIR/*; do
SIZE=$(stat - -printf "%s\n" $DBDUMP)
echo "$DBDUMP,$SIZE,$[ 100 * $SIZE / $TOTAL ]%"
done
[ r oot@serverX - ] # chmod u+x /usr/local/sbin/dbbackup
[ root@serverX -]# /usr/local/sbin/dbbackup