Shell Scripts
Shell Scripts
echo ${PATH}
man hier
q
touch script1
nano script1.sh
#!/bin/bash
echo "Hostname is"
echo $(hostname)
echo 'hostname'
:q
r=4
w=2
x=1
/root/script1 or ./script1
cp script1.sh /usr/local/bin/
exit
sudo su -
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/
USER='testuser1'
{
USER
USER
or
echo ${MYPIP}
nano script1.sh
#!/bin/bash
MYSG=$(curl -sL http://169.254.169.254/latest/meta-data/security-groups/)
: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}"
#./User1.sh
#!/bin/bash
read -p "Please enter the number: " X
echo
#!/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
-------------------------------
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}
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}
fi
------------------------------------------
Enable password:
#nano /etc/ssh/sshd_config
PasswordAuthentication Yes
------------------------------------------
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}
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}
fi
done
---------------------------------------------------------
To delete users:
#nano ForUserDelete.sh
#!/bin/bash
USERS="${@}"
for USER_NAME in ${USERS}
do
userdel -r ${USER_NAME}
echo "User ${USER_NAME} is deleted..!!"
done
-----------------------------------------------------------------
While Loop:
#nano WhileUserCreate.sh
#!/bin/bash
While:
do
SPEC=` !@#$%^&*()_ `
SPECCHAR=$(echo ${SPEC} |fold -w1 | shuf |head -1 )
PASSWORD=India@${Random}${SPECCHAR}
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}
fi
done
-------------------------------------------------------------
#nano WhileUserDelete.sh
#!/bin/bash
while :
do
-------------------------------------------------------------
OR Gate - ||
--------------------------------------------------------------
While Loop 2:
#nano WhileUserCreate2.sh
#!/bin/bash
if [[ ${EX_USER} == ${USER_NAME} ]]
then
echo "User already exists , please use different username..!!"
echo "Exit code is ${?}"
else
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
-----------------------------------------------------------------------
# i=5
# While [[ i>10 ]]
>do
>ping -c3 www.google.com
>done
--------------------------------------------------------------------------
#!/bin/bash
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
--------------------------------------------------------------------
#nano elseifcreation.sh
#!/bin/bash
echo -e "1. User Creation \n 2. Group Creation \n 3. File Creation \n 4. Dir
Creation"
read n
if [[ $n -eq 1 ]]
then
read -p "Enter username: " USER_NAME
useradd -m ${USER_NAME}
elif [[ $n -eq 2 ]]
then
read -p "Enter Group name: " GROUP_NAME
groupadd -m ${GROUP_NAME}
if [[ $n -eq 3 ]]
then
read -p "Enter username: " USER_NAME
useradd -m ${USER_NAME}
if [[ $n -eq 4 ]]
then
read -p "Enter username: " USER_NAME
useradd -m ${USER_NAME}
fi
---------------------------------------------------------
Case:
#!/bin/sh
---------------------------------------------------------------