0% found this document useful (0 votes)
38 views

Bash Samplescripts

The document contains 34 Bash script samples demonstrating various Bash scripting techniques like printing text, user input, variables, arithmetic operations, conditionals, loops, functions, select menus and more. The scripts show how to check for files and directories, swap variables, sort directory listings, backup files, check for logged in users and display system information.

Uploaded by

jeffconnors
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Bash Samplescripts

The document contains 34 Bash script samples demonstrating various Bash scripting techniques like printing text, user input, variables, arithmetic operations, conditionals, loops, functions, select menus and more. The scripts show how to check for files and directories, swap variables, sort directory listings, backup files, check for logged in users and display system information.

Uploaded by

jeffconnors
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Bash Script Samples:-

#1.
#!/bin/bash
echo "Testing my first program"

#2.
#!/bin/bash
v1="Testing my first program"
echo "${v1}"

#3.
#!/bin/bash
echo "Type anything"
read v1
echo "" #blank line
echo "You have typed '${v1}'"

#4.
#!/bin/bash
echo "Print 1st number:"
read v1
echo "Print 2nd number:"
read v2
v3=`echo $v1`
v1=`echo $v2`
v2=`echo $v3`
echo " "
echo "This Program will swap 1st and 2nd number order:"
echo "v1=$v1"
echo "v2=$v2"

#5.
#!/bin/bash
echo "Enter 1st num:"
read v1
echo "Enter 1st num:"
read v2
echo " "
echo "Add two num:"
echo "$(($v1+$v2))"
#6.
#!/bin/bash
echo "List of files and directories"
echo "$(ls)"

#7.
#!/bin/bash
echo "List of files and directories"
echo "$(ls -l | grep ^d)"

#8.
#!/bin/bash
echo "List of files and directories"
echo "$(ls -l | grep ^-)"

#9.
#!/bin/bash
echo "List of files and directories"
echo "$(ls -l | grep ^l)"

#10.
#!/bin/bash
echo "List of files and directories sorted with size"
echo "$(ls -lS)"

#11.
#!/bin/bash
echo "List of files and directories sorted with size"
echo "$(ls -lt)"

#12.
#!/bin/bash
echo "List of total size of partition, used size, available size and mounted location "
echo "$(df -h | cut -c23-60 | grep -i [k,m,g])"

#13.
#!/bin/bash
echo "Starting etc directory backup....."
echo " "
tar -czf backup.tgz /etc
echo "Backup is done"
echo " "

#14.
#!/bin/bash
echo " "
echo "These users are currently logged in my local machine"
echo " "
last | grep -i "still logged in"
echo " "
echo " "

#15.
#!/bin/bash
#example of global variables
#user session info
echo "user name='$USER'"
echo "user home directory='$HOME'"
echo "user present working directory='$PWD'"
echo "user login shell='$SHELL'"
echo "user system name='$HOSTNAME'"
echo "user desktop environment='$DESKTOP_SESSION'"
echo "user working language='$LANG'"
echo "user keyboard layout='$GDM_KEYBOARD_LAYOUT'"
echo "user mails directory='$MAIL'"

#16.
#!/bin/bash
#testing file
if [ -e test.sh ] ; then
echo "The file "test.sh" exist."
else
echo "The file "test1.sh" does not exist."
fi

#17.
#!/bin/bash
if ! [ -e test1.sh ] ; then
touch test1.sh
echo "file has created"
else
echo "File is already existing"
fi
#18.
#!/bin/bash
#testing directory
if [ -d dir1 ] ; then
echo "True"
else
echo "False"
fi

#19.
#!/bin/bash
#testing numbers
if [ 0 = 0 ] ; then
echo "Ture"
else
echo "False"
fi

#20.
#!/bin/bash
if [ 0 != 0 ] ; then
echo "True"
else
echo "False"
fi

#21.
#!/bin/bash
#testing value
if [ string == string ] ; then
echo "Pass"
else
echo "Fail"
fi

#22.
#!/bin/bash
if [ string != string ] ; then
echo "True"
else
echo "False"
fi

#23.
#!/bin/bash
#testing value
if [ 0 = 0 ] || [ 1 = 0 ] ; then
echo "True"
else
echo "False"
fi

#24.
#!/bin/bash
if [ 0 = 0 ] && [ 1 = 1 ] ; then
echo "True"
else
echo "False"
fi

#25.
#!/bin/bash
if [ 0 = 0 ] && ! [ 1 = 0 ] ; then
echo "True"
else
echo "False"
fi

#26.
#!/bin/bash
#nested if
if [ -f abc ]; then
echo "'abc' is a file"
elif [ -d abc ]; then
echo "'abc' is a directory"
else
echo "'abc' does not exist"
fi

#27.
#!/bin/bash
for (( i=0; i<5; i++ ))
do
echo "$i"
done
#28.
#!/bin/bash
for i in 1 2 3 4 5
do
echo "$i"
done

#29.
#!/bin/bash
for i in {1..10}
do
echo "$i"
done

#30.
#!/bin/bash
i=5
while [ 0 -lt $i ]
do
echo "hello"
i=`echo $(($i-1))`
done

#31.
#!/bin/bash
#if 'abc' file is exist, this will be infinite loop.
while [ -e abc ]
do
echo "hello"
sleep 3
done

#32.
#!/bin/bash
#if 'abc' file is not exist, this will be infinite loop.
until [ -e abc ]
do
echo "hello"
sleep 3
done
#33.
#!/bin/bash
fun1()
{
if [ -f $var1 ]; then
echo "This is a file."
elif [ -d $var1 ]; then
echo "This is a directory."
else
echo "No file and direcctory exist with ${var1} name."
fi
}
read -p "Type any name for testing whether it existing into the system or not" var1
echo "System is checking...."
sleep 3
fun1

#34.
#!/bin/bash
echo -e -n "Enter 1st Num="
read a
echo -e -n "Enter 2nd Num="
read b
PS3="TO PERFORM ACTION FOR THIS CALCULATOR, CHOOSE ANY NUMBER="
select i in ADD SUB MUL DIV PER EXIT
do
case $i in
ADD)
add=`echo $[$a+$b]`
echo "$add"
;;
SUB)
sub=`echo $[$a-$b]`
echo "$sub"
;;
MUL)
mul=`echo $[$a*$b]`
echo "$mul"
;;
DIV)
div=`echo $[$a/$b]`
echo "$div"
;;
PER)
per=`echo $[$a%$b]`
echo "$per"
;;
EXIT)
exit
;;
esac
done

You might also like