Monday, December 28, 2020

how to add user using shell script?

 #!/bin/bash -x 


if [ $(id -u) -eq 0 ]; then

read -p "Enter username : " username

read -s -p "Enter password : " password

egrep "^$username" /etc/passwd >/dev/null

if [ $? -eq 0 ]; then

echo "$username exists!"

exit 1

else

if [ -f /usr/bin/mkpasswd ]; then


pass=$(mkpasswd -m sha-512 $password)

#mkpasswd will create encrypted password.

useradd -m -p "$pass" "$username"

[ $? -eq 0 ] && echo "User has been added to system!" && chage -d0 $username  || echo "Failed to add a user!"

else

echo "install package apt-get install whois"

       fi

fi

else

echo "Only root may add a user to the system."

exit 2

fi

Monday, November 23, 2020

how to check If the file Is Empty or not using shell script?

 how to check If the file Is Empty or not using shell script?


-s FILE

              FILE exists and has a size greater than zero


Example 1:


touch /$HOME/f1


echo "data" >/$HOME/f2

ls -l /$HOME/f{1,2}

[ -s /$HOME/f1 ] 

echo $?


Sample outputs:


1



The non-zero output indicates that the file is empty.


[ -s /$HOME/f2 ]

echo $?

Sample outputs:


0


$? is the return code of the last executed command.


Example 2:


$ ls

$ touch file_1

$ dd if=/dev/zero of=$HOME/file_2 bs=1 count=100

100+0 records in

100+0 records out

$ ls -l file_1 file_2 file_3

ls: file_3: No such file or directory

-rw-rw-r--  1 foo bar   0 Nov 20 20:28 file_1

-rw-rw-r--  1 foo bar 100 Nov 20 20:28 file_2

$ [[ -s file_1 ]]

$ echo $?

1

$ [[ -s file_2 ]]

$ echo $?

0

$ [[ -s file_3 ]]

$ echo $?

1


Thursday, October 1, 2020

Input/Output Error How to Reboot or shutdown the Linux server?

 Input/Output Error : Bad Blocks - How to Reboot or shutdown the Linux server?  


Input/output error while running the command mostly due to it could be bad blocks on the disk. 

In this situation, first suggestion would be to check /var/log/messages for any disk-related alerts (might see some sense key alerts).


tail -n 100 /var/log/messages or tail -n 100 /var/log/syslog

tail -f /var/log/messages


#du

bash: /usr/bin/du: Input/output error




Now if try to reboot,it can also give the same output. You can try to init 6.



# reboot or init 6

bash: /sbin/reboot: Input/output error


# shutdown -r now

bash: /sbin/shutdown: Input/output error



If the above reboot commands don’t work try either forced reboot or shutdown


echo "number" >/proc/sys/kernel/sysrq


The number may be written here either as decimal or as hexadecimal with the 0x prefix. CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE must always be written in hexadecimal.


Note that the value of /proc/sys/kernel/sysrq influences only the invocation via a keyboard. Invocation of any operation via /proc/sysrq-trigger is always allowed (by a user with admin privileges).



0 - disable sysrq completely


1 - enable all functions of sysrq


Where  b Will immediately reboot the system without syncing or unmounting your disks.

              o Will shut your system off



Forced Reboot


echo 1 > /proc/sys/kernel/sysrq

echo b > /proc/sysrq-trigger


Forced Shutdown

echo 1 > /proc/sys/kernel/sysrq

echo o > /proc/sysrq-trigger




Monday, September 21, 2020

how to install R in ubuntu ?

R is an open-source programming language and free environment that specialises in statistical computing and graphical representation. It is mainly used by statisticians and data miners for developing statistical software and performing data analysis

add to /etc/apt/sources.list

deb https://cloud.r-project.org/bin/linux/ubuntu trusty/


Installation

To obtain the latest R 4.0 packages, add an entry like


deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/

or


deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/

or


deb https://cloud.r-project.org/bin/linux/ubuntu xenial-cran40/




To obtain the latest R 3.6 packages, use:


deb https://cloud.r-project.org/bin/linux/ubuntu eoan-cran35/

or


deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/

or


deb https://cloud.r-project.org/bin/linux/ubuntu xenial-cran35/

or


deb https://cloud.r-project.org/bin/linux/ubuntu trusty-cran35/

To obtain the latest R 3.4 packages, use:


deb https://cloud.r-project.org/bin/linux/ubuntu xenial/

or


deb https://cloud.r-project.org/bin/linux/ubuntu trusty/



sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/'

sudo apt-get update

sudo apt-get install r-base

sudo apt-get install r-base-dev xml2 libxml2-dev libssl-dev libcurl4-openssl-dev unixodbc-dev


If we want to upgrade to the latest version 

sudo apt-get install r-base-dev --reinstall

To install any package under R Example Below 

sudo R

install.packages("tidyverse")


after installation


R


library(tidyverse)

── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──

✔ ggplot2 3.2.1     ✔ purrr   0.3.3

✔ tibble  2.1.3     ✔ dplyr   0.8.3

✔ tidyr   1.0.0     ✔ stringr 1.4.0

✔ readr   1.3.1     ✔ forcats 0.4.0

── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──

✖ dplyr::filter() masks stats::filter()

✖ dplyr::lag()    masks stats::lag()


Reference :

 https://cran.r-project.org/bin/linux/ubuntu/README.html

Monday, June 15, 2020

How to dump the database with triggers and procedures?




Stored procedures and Triggers are first introduced with MySQL 5.0. So if you are still using MySQL older version’s upgrade it to MySQL 5.0 or higher version to use these features.




What is Stored Procedure ?


A stored procedure, by definition, is a segment of declarative SQL code which is stored in the database catalog and can be invoked later by a program, a trigger or even a stored procedure.


What is Triggers ?


Triggers are event-driven specialized procedures, they are stored in and managed by the database. A trigger is a SQL procedure that initiates an action on an event (Like INSERT, DELETE or UPDATE) occurs.



mysqldump will backup by default all the triggers but NOT the stored procedures/functions. There are 2 mysqldump parameters that control this behavior:


--routines - FALSE by default

--triggers - TRUE by default


This means that if you want to include in an existing backup script also the triggers and stored procedures you only need to add the --routines command line parameter:


Backup Stored Procedures and Routines


We need to specify --routines to take backup of stored procedures with data and tables.


The following command will take backup of entire database including stored procedures. For example, your database name is “mydb”.


mysqldump -u root -p --routines mydb > mydb.sql


To take backup of only Stored Procedures and Triggers (Exclude table and data ) use the following command.


mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt mydb > mydb.sql



To Take backup without triggers use following command

mysqldump -uroot -p --skip-triggers mydb> mydb.sql

Restore Procedures


To restore stored procedures in the database simply use the following command, But make sure you have taken backup properly before restoring it to avoid any data loss.


mysql -u root -p mydb < mydb.sql