1.Shell-Scripting-Class
1.Shell-Scripting-Class
/bin/bash
#If the is below 18, inform they are not eligible to drink.
if [ $age -lt 18 ]
then
echo "You are underage, please try after 18 years."
elif ! [ $age -lt 25 ] && [ $age -gt 18 ]
then
echo "Pleae show your ID card."
else
echo "Happy Liver Failure."
fi
#!/bin/bash
read -p "Please Enter The Username:" USERNAME
read -s -p "Please Enter The Password:" PASSWORD
useradd $USERNAME
echo $PASSWORD | passwd --stdin ${USERNAME}
usermod -aG wheel $USERNAME
passwd -e $USERNAME
User1.sh
#!/bin/bash
set -xe
read -p "Enter the Username:" USER_NAME
SPEC='!@#$%^&*()_'
SPECCHAR=$(echo ${SPEC} | fold -w1 | shuf | head -1)
PASSWORD=${RANDOM}$(date +%s%N)${SPECCHAR}
useradd -m ${USER_NAME}
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
echo "Sucessfully Created user ${USER_NAME} with password as ${PASSWORD}"
Test1.sh
#!/bin/bash
#set -x -e
read -p "Please enter the user name:" USER_NAME
PASSWORD=$(curl -sL https://helloacm.com/api/random/?n=14)
#read -s -p "Please enter the password:" USER_PASS
#useradd -m ${USER_NAME} -p ${PASSWORD}
useradd -m ${USER_NAME}
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
echo "The Username is ${USER_NAME} & Password is ${PASSWORD}"
IF STATEMENT:
=================================================
if [[ ${EXUSER} == ${USER_NAME} ]]
then
echo "User aleady exists. Please use a diffrent username..!!"
else
echo "Creating the new user...!!"
sleep 3s
useradd -m ${USER_NAME}
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
passwd -e ${USER_NAME}
#Print the USername and Password.
echo "Username is ${USER_NAME} Password is ${PASSWORD} "
Fi
FOR LOOP:
=================================================
END=5
for ((i=1;i<=END;i++)); do
echo $i
Done
WHILE LOOP-1
=================================================
while [ "$stats" -gt 300 -o "$stats" -eq 0 ]
while [ "$stats" -gt 300 ] || [ "$stats" -eq 0 ]
WHILE LOOP -2
=========================================================
#!/bin/bash
read -p "Do you want to create users(Yes/No):" CHOICE
while [[ ${CHOICE} = "Yes" ]] || [[ ${CHOICE} = "yes" ]]
do
#Ask for the username
read -p "Please enter the username:" USER_NAME
#Check of the Username Exists
EXUSER=$(cat /etc/passwd | cut -d ":" -f 1 | grep -i ${USER_NAME})
if [[ ${EXUSER} = ${USER_NAME} ]]
then
echo "User ${USER_NAME} already exists."
echo "Exit Code is ${?}."
else
#Generate a complex password
SPEC='!@#$%^&*()'
SPECHAR=$(echo $SPEC | fold -w1 | shuf | head -1)
PASSWORD=Capita${RANDOM}${SPECHAR}
useradd -m ${USER_NAME}
echo $PASSWORD |passwd --stdin ${USER_NAME}
echo "${USER_NAME} is successfully created.Password is ${PASSWORD}"
fi
read -p "Do you want to create users(Yes/No):" CHOICE
done
echo "You have opted for no...!!"
==========================================================
Otherway of WHILE:
END=5
i=1 ; while [[ $i -le $END ]] ; do
echo $i
((i = i + 1))
done
==========================================================
echo -e "1.Morning\n2.Afternoon\n3.Evening\n4.Night"
echo -n "Please select from above:"
read n
if [[ $n -eq 1 ]]
then
echo "Good Morning"
elif [[ $n -eq 2 ]]
then
echo "Good Afternoon"
elif [[ $n -eq 3 ]]
then
echo "Good Evening"
elif [[ $n -eq 4 ]]
then
echo "Good Night"
Fi
FUNCTIONS:
--------------------
#!/bin/bash
myuser(){
read -p "Please enter the UserName:" USER_NAME
PASSWORD=$(curl -sL https://helloacm.com/api/random/?n=14)
EXUSER=$(cat /etc/passwd|cut -d ":" -f 1|grep -i ${USER_NAME})
if [[ "${EXUSER}" == "${USER_NAME}" ]]
then
echo "UserName Already Exists..!!"
# exit
else
useradd -m "${USER_NAME}"
echo "${PASSWORD}" | passwd --stdin "${USER_NAME}"
echo "Created User "${USER_NAME}" & Password will be "${PASSWORD}""
fi
echo "User "${USER_NAME}" created....!!"
}
myuser
newuser testuser1
newuser testuser2
newuser testuser3
newuser testuser4
newuser testuser5
newuser testuser6
RETURN:
------
#!/bin/bash
function testing () {
read -p "Enter the first number:" NUM1
read -p "Enter the second number:" NUM2
NEWNUM=$(($NUM1+$NUM2))
#echo "The New Number is $NEWNUM""
return ${NEWNUM}
}
testing
GETVAL=${?}
ostechnix
Ostechnix
o$technix
linux
linus
unix
technology
hello world
HELLO world
CUT:
----
cat /etc/passwd | cut -c1-4
cat /etc/passwd | cut -c4
cat /etc/passwd | cut -c4-
cat /etc/passwd | cut -d ":" -f1
cat /etc/passwd | cut -d ":" -f1,2
GREP:
-----
cat /etc/passwd | grep -i tes
cat /etc/passwd | grep -v test
cat /etc/passwd | grep -i 't$'