Wednesday, December 30, 2020

how to convert from one date format to another format in linux ?

 how to convert from one date format to another format in linux ?

echo 04-11-2021 | { IFS=- read d m y && echo "$y$m$d"; }

20211104


echo 04-11-2021 | { IFS=- read d m y && echo "$y/$m/$d"; }

2021/11/04


 echo 04-11-2021 | { IFS=- read d m y && echo "$d$m$y"; }

04112021


 echo 04-11-2021 | awk -F- '{print $3$2$1}'

20211104


echo 04-11-2021 |gawk -F, '{split($1, a, "-"); print a[3] a[2] a[1]}' 

20211104


echo 04/11/2021 | { IFS=/ read d m y && echo "$y$m$d"; }

20211104


date -d "20211104" "+%Y-%m-%d"

2021-11-04


date -d 04/11/2021 +%F

2021-04-11


 %F   full date; same as %Y-%m-%d


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