sudo su -
echo ${PATH}
man hier
q
nano or vim or touch (for file creation)
touch script1
nano script1.sh
#!/bin/bash
echo "Hostname is"
echo $(hostname)
echo 'hostname'
:q
./script1 (for execute)
chmod 700 script1.sh
rwx r-- r--
r=4
w=2
x=1
/root/script1 or ./script1
Absolute path or Relative Path
cp script1.sh /usr/local/bin/
exit
#chmod 707 script1.sh
$ sudo chmod 707 script.sh
ll
sudo su- testuser1
visudo (sudo permisssions to test user)
testuser1 ALL=(All) All
sudo su -
usermod -aG wheel testuser2
type -a cat
type -a ifconfig
type -a uptime
type -a ping
type -a useradd
type -a free
typr -a ls
man free
hash
curl http://169.254.169.254/latest/meta-data/
100 lines of code
USER='testuser1'
{
USER
USER
MYPIP=`curl -sL http://169.254.169.254/latest/meta-data/`
or
MYPIP=$(curl -sL http://169.254.169.254/latest/meta-data/)
echo ${MYPIP}
nano script1.sh
#!/bin/bash
MYSG=$(curl -sL http://169.254.169.254/latest/meta-data/security-groups/)
MYPIP=`curl -sL http://169.254.169.254/latest/meta-data/public-ipv4/`
echo "My machine Public ip is "${MYPIP}."
echo "My machine SG is "${MYSG}."
:wq
-------------
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 "Successfully Created user ${USER_NAME} with password as ${PASSWORD}"
ctrl + o and ctrl + x
#./User1.sh
#nano User1.sh (to edit)
-------------
#nano iftest.sh
#!/bin/bash
read -p "Please enter the number: " X
echo
if [[ ${X} -eq 10]]
then
echo " The value of X is ${X}"
else
echo "You have not selected the value as 10"
fi
ctrl + o and ctrl + x
#chmod 700 iftest.sh
#./iftest.sh
-----------------------
#id
#id -un
#!/bin/bash
USER=$(id -un)
if [[ ${USER} == 'root' ]]
then
echo "The script is exeuted as a root user"
else
echo "The script is executed by ${USER}"
fi
ctrl + o and ctrl + x
#chmod 700 iftest1.sh
#./iftest1.sh
-------------------------------
check user exists or not:
#cat /etc/passwd | grep -i ec2-user
#cat /etc/passwd | grep -i ec2-user | cut -d ":" -f 1
#cat /etc/passwd | grep -i ec2-user | cut -d ":" -f 2
#cat /etc/passwd | grep -i ec2-user | cut -d ":" -f 3
#cat /etc/passwd | grep -i ec2-user | cut -d ":" -f 4
-------------------------------
IF Statement:
#nano UserCreate.sh
#!/bin/bash
read -p "Please enter the username: " USER_NAME
SPEC=` !@#$%^&*()_ `
SPECCHAR=$(echo ${SPEC} |fold -w1 | shuf |head -1 )
PASSWORD=India@${Random}${SPECCHAR}
EX_USER=$(cat /etc/passwd | grep -i ${USER_NAME} | cut -d ":" -f 1)
if [[ ${EX_USER} == ${USER_NAME} ]]
then
echo "User already exists , please use different username..!!"
else
echo "Creating new user...!!!"
sleep 3s
useradd -m ${USER_NAME}
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
passwd -e ${USER_NAME}
echo " Username is ${USER_NAME} and password id ${PASSWORD} "
fi
ctrl + o and ctrl + x
#chmod 700 UserCreate.sh
#./UserCreate.sh
------------------------------------------
Enable password:
#nano /etc/ssh/sshd_config
PasswordAuthentication Yes
ctrl + o and ctrl + x
#service sshd restart
------------------------------------------
For Loop:
#for x in 1 2 3 4 5 6 7 8 9 10
>do
>echo "The x value is $x. "
>done
------------------------------------------
# echo "${?}"
# echo "${@}"
-----------------------------------------
For Loop Script:
#nano ForUserCreate.sh
#!/bin/bash
USERS="${@}"
for USER_NAME in ${USERS}
do
SPEC=` !@#$%^&*()_ `
SPECCHAR=$(echo ${SPEC} |fold -w1 | shuf |head -1 )
PASSWORD=India@${Random}${SPECCHAR}
EX_USER=$(cat /etc/passwd | grep -i ${USER_NAME} | cut -d ":" -f 1)
if [[ ${EX_USER} == ${USER_NAME} ]]
then
echo "User already exists , please use different username..!!"
else
echo "Creating new user...!!!"
sleep 3s
useradd -m ${USER_NAME}
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
passwd -e ${USER_NAME}
echo " Username is ${USER_NAME} and password id ${PASSWORD} "
fi
done
ctrl + o and ctrl + x
#chmod 700 ForUserCreate.sh
#./ForUserCreate.sh User1 User2 User3 User4
---------------------------------------------------------
To delete users:
#cp ForUserCreate.sh ForUserDelete.sh
#nano ForUserDelete.sh
#!/bin/bash
USERS="${@}"
for USER_NAME in ${USERS}
do
userdel -r ${USER_NAME}
echo "User ${USER_NAME} is deleted..!!"
done
ctrl + o and ctrl + x
#chmod 700 ForUserDelete.sh
#./ForUserDelete.sh User1 User2 User3 User4
-----------------------------------------------------------------
While Loop:
#nano WhileUserCreate.sh
#!/bin/bash
While:
do
read -p "Please enter the username: " USER_NAME
SPEC=` !@#$%^&*()_ `
SPECCHAR=$(echo ${SPEC} |fold -w1 | shuf |head -1 )
PASSWORD=India@${Random}${SPECCHAR}
EX_USER=$(cat /etc/passwd | grep -i ${USER_NAME} | cut -d ":" -f 1)
if [[ ${EX_USER} == ${USER_NAME} ]]
then
echo "User already exists , please use different username..!!"
else
echo "Creating new user...!!!"
sleep 3s
useradd -m ${USER_NAME}
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
passwd -e ${USER_NAME}
echo " Username is ${USER_NAME} and password id ${PASSWORD} "
fi
done
ctrl + o and ctrl + x
#chmod 700 WhileUserCreate.sh
#./WhileUserCreate.sh
-------------------------------------------------------------
To delete users using While:
#cp WhileUserCreate.sh WhileUserDelete.sh
#nano WhileUserDelete.sh
#!/bin/bash
while :
do
read -p "Please enter the username: " USER_NAME
userdel -r ${USER_NAME}
echo "User ${USER_NAME} is deleted..!!"
done
ctrl + o and ctrl + x
#chmod 700 WhileUserDelete.sh
#./WhileUserDelete.sh
-------------------------------------------------------------
OR Gate - ||
AND Gate - &&
True or True = True
True or False = True
False or True = True
False or False = False
True and True = True
True and False = False
--------------------------------------------------------------
While Loop 2:
#nano WhileUserCreate2.sh
#!/bin/bash
read -p "Do you want to create users(Yes/No): " CHOICE
While [[ ${CHOICE} = "Yes" ]] || [[ ${CHOICE} = "yes" ]]
do
#Ask the user name
read -p "Please enter the username: " USER_NAME
#Check of the username exists
EX_USER=$(cat /etc/passwd | grep -i ${USER_NAME} | cut -d ":" -f 1)
if [[ ${EX_USER} == ${USER_NAME} ]]
then
echo "User already exists , please use different username..!!"
echo "Exit code is ${?}"
else
#Generate a complex password
SPEC=` !@#$%^&*()_ `
SPECCHAR=$(echo ${SPEC} |fold -w1 | shuf |head -1 )
PASSWORD=Capita@${Random}${SPECCHAR}
useradd -m ${USER_NAME}
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
passwd -e ${USER_NAME}
echo " Username is ${USER_NAME} successfully created and password id ${PASSWORD} "
fi
read -p "Do you want to create users (Yes/No) : " CHOICE
done
echo "You have opted for No user creation....!!"
ctrl + o and ctrl + x
#chmod 700 WhileUserCreate2.sh
#./WhileUserCreate2.sh
-----------------------------------------------------------------------
# i=5
# While [[ i>10 ]]
>do
>ping -c3 www.google.com
>done
--------------------------------------------------------------------------
#!/bin/bash
echo -e "1. User Creation \n 2. Group Creation \n 3. File Creation \n 4. Dir
Creation"
echo -n "Please select from above:"
read n
if [[ $n -eq 1 ]]
then
echo "Good Morning"
if [[ $n -eq 2 ]]
then
echo "Good Afternon"
if [[ $n -eq 3 ]]
then
echo "Good Evening"
if [[ $n -eq 4 ]]
then
echo "Good Night"
fi
ctrl + o and ctrl + x
#chmod 700 sample1.sh
#./sample1.sh
--------------------------------------------------------------------
#nano elseifcreation.sh
#!/bin/bash
echo -e "1. User Creation \n 2. Group Creation \n 3. File Creation \n 4. Dir
Creation"
echo -n "Please select from above:"
read n
if [[ $n -eq 1 ]]
then
read -p "Enter username: " USER_NAME
useradd -m ${USER_NAME}
echo "${USER_NAME} created."
elif [[ $n -eq 2 ]]
then
read -p "Enter Group name: " GROUP_NAME
groupadd -m ${GROUP_NAME}
echo "${GROUP_NAME} created."
if [[ $n -eq 3 ]]
then
read -p "Enter username: " USER_NAME
useradd -m ${USER_NAME}
echo "${USER_NAME} created."
if [[ $n -eq 4 ]]
then
read -p "Enter username: " USER_NAME
useradd -m ${USER_NAME}
echo "${USER_NAME} created."
fi
ctrl + o and ctrl + x
#chmod 700 elseif.sh
#./elseif.sh
---------------------------------------------------------
Case:
#!/bin/sh
echo "Please talk to me ..."
while :
do
read INPUT_STRING
case $INPUT_STRING in
hello)
echo "Hello yourself!"
;;
bye)
echo "See you again!"
break
;;
*)
echo "Sorry, I don't understand"
;;
esac
done
echo
echo "That's all folks!"
---------------------------------------------------------------