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

#Score Card, Change Using Case-Esac

The document contains several Bash script examples that demonstrate various conditional and looping constructs in Bash like if-then-else, case statements, for loops, file testing and manipulation. The scripts show how to check for file/directory properties, compare strings and numbers, iterate through lists, and perform other tasks like taking user input, running commands conditionally etc.

Uploaded by

MeetSharma
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)
26 views

#Score Card, Change Using Case-Esac

The document contains several Bash script examples that demonstrate various conditional and looping constructs in Bash like if-then-else, case statements, for loops, file testing and manipulation. The scripts show how to check for file/directory properties, compare strings and numbers, iterate through lists, and perform other tasks like taking user input, running commands conditionally etc.

Uploaded by

MeetSharma
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/ 12

#!

/bin/bash
echo "press 'm' to print MANIPAL"
read var
if [ $var = 'm' ]
then
echo "MANIPAL"
else
echo "You did not press m"
fi
# exit 0

# To check for even or odd number


#!/bin/bash
read -p "enter number" var
if [ `expr $var % 2` -eq 0 ]
then
echo "even"
else
echo "odd"
fi

#Score card, change using case-esac


#!/bin/bash
read -p "enter cgpa: " cgpa
if [ $cgpa -eq 6 ]
then
echo "You have scored a D"
elif [ $cgpa -eq 7 ]
then
echo "You have scored a C"
elif [ $cgpa -eq 8 ]
then
echo "You have scored a B"
elif [ $cgpa -eq 9 ]
then
echo "You have scored a A,good"
elif [ $cgpa -eq 10 ]
then
echo "You have scored a A+, Good job"
else
echo "Not much of a score, Better luck next time!"
fi
#!/bin/bash
#Shell script to add two values without shell parameters
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
#res=`expr $num1 + $num2`
echo "Result is `expr $num1 + $num2`"

#!/bin/bash
read -p "Enter your name?" fname lname
echo "The name is $lname $fname!"

#!/bin/bash
echo -n "Enter first name last name: "
read fname lname
echo "Your first name is $fname"
echo "Your last name is $lname"

#!/bin/bash
#TEST FOR EXISTING USERS AND THEIR FILES
read -p "Enter username to be checked for:" newuser
if grep $newuser /etc/passwd
then
echo "Files for user $newuser are:\n"
ls -l /home/$newuser/*
else
echo "The user name does not exist on this system!"
fi

#!/bin/bash
# testing string length
val1=testing
val2=c
if [ -n $val1 ]
then
echo "The string '$val1' is not empty"
else
echo "The string '$val1' is empty"
fi
if [ -z $val2 ];then
echo "The string '$val2' is empty"
else
echo "The string '$val2' is not empty"
fi
if [ -z $val3 ]
then
echo "The string $val3 is empty"
else
echo "The string $val3 is not empty"
fi

#!/bin/bash
# CHECKING DIRECTORIES
if [ -d $HOME ]; then
echo "Your HOME directory exists and is '$HOME'"
cd $HOME
ls -a
else
echo "There's a problem with your HOME directory"
fi
~

#!/bin/bash
#Checking if an object (file or directory) exists
# USING NESTED IF conditions

if [ -e $HOME ]; then
echo "OK directory exists, now lets check the file"

#checking if a file exists


if [ -e $HOME/testing ]
then
# the file exists, append data to it
echo "Appending date to existing file"
date >> $HOME/testing
cat $HOME/testing
else
# the file doesnt exist, create a new file
echo "Creating new file"
date > $HOME/testing
cat $HOME/testing
fi
else
echo "Sorry, you dont have a HOME directory"
fi

#!/bin/bash
# check for a file
if [ -e $HOME ]
then
echo "The object exists, is it a file?"
if [ -f $HOME ]
then
echo "Yes, its a file!"
else
echo "No, its not a file!"
if [ -f $HOME/.bash_history ]
then
echo "But this is a file!"
fi
fi
else
echo "Sorry, the object doesn’t exist"
fi

#!/bin/bash
# testing if you can read a file
filename=scr6
if [ -f $filename ]
then
# now test if you can read it
clear
echo
echo "Reading SHELL SCRIPT: <$filename>"
if [ -r $filename ]
then
head $filename
else
echo "Sorry, Im unable to read the $filename file"
fi
else
echo "Sorry, the file $file doesnt exist"
fi:
#!/bin/bash
# testing if you can read a file
pwfile=scr6
#/etc/shadow
# first, test if the file exists, and is a file
if [ -f $pwfile ]
then
# now test if you can read it
if [ -r $pwfile ]
then
head $pwfile
else
echo "Sorry, Im unable to read the $pwfile file"
fi
else
echo "Sorry, the file $file doesn’t exist"
fi

#!/bin/bash
# testing if a file is empty
file=scr199
touch $file
if [ -s $file ]
then
echo "The $file file exists and has data in it"
else
echo "The $file exists and is empty"
fi
date > $file
if [ -s $file ]
then
echo "The $file file has data in it"
else
echo "The $file is still empty"
fi

#!/bin/bash
# checking if a file is writeable
logfile=$HOME/shellscripts/testfile1
touch $logfile
chmod u-w $logfile
now=`date +%Y%m%d-%H%M`
if [ -w $logfile ]
then
echo "The program ran at: $now" > $logfile
echo "The fist attempt succeeded"
else
echo "The first attempt failed"
fi
chmod u+w $logfile
if [ -w $logfile ]
then
echo "The program ran at: $now" > $logfile
echo "The second attempt succeeded"
else
echo "The second attempt failed"
fi
echo "Check the file permissions"
ls -l $logfile

#!/bin/bash
# testing if a file is empty
file=sample1.dat
touch $file
if [ -s $file ]
then
echo "The $file file exists and has data in it"
else
echo "The $file exists and is empty"
fi
#appending data into file
date >> $file
if [ -s $file ]
then
echo "The $file file has data in it"
else
echo "The $file is still empty"
fi

#!/bin/bash
# testing file execution
read -p "Script file name:" fname
if [ -x $fname ]
then
echo "You can run the script:"
bash /$fname
else
echo "Sorry, you are unable to execute the script"
fi

#!/bin/bash
# check file ownership
if [ -O /etc/passwd ]
then
echo "You're the owner of the /etc/passwd file"
else
echo "Sorry, you're not the owner of the /etc/passwd fil
fi
# now check for file with ownership
if [ -O ./scr14 ]
then
echo "Great! You're the owner of this file"
else
echo "Sorry, you're not the owner of this file"
fi

#!/bin/bash
# check file group test
if [ -G $HOME/shellscripts/scr99 ]
then
echo "You're in the same group as the file"
echo "Check it yourself"
ls -l $HOME/shellscripts/scr99
else
echo "The file is not owned by your group"
fi

#!/bin/bash
# test file dates
if [ ./scr19 -nt ./scr18 ]
then
echo "The scr19 file is newer than scr18"
else
echo "The scr18 file is newer than scr19"
fi
if [ ./scr17 -ot ./scr19 ]
then
echo "The scr17 file is older than the scr19 file"
fi

#Compound Condition Testing


# Boolean logic to combine tests
#operators you can use:
# [ condition1 ] && [ condition2 ] AND
# [ condition1 ] || [ condition2 ] OR
#!/bin/bash
# testing compound comparisons
var=$HOME
fname=testing
if [ -d $var ] && [ -w $var/$fname ]
then
echo "The file exists and you can write to
echo "File Permissions for $fname are"
ls -l $var/$fname
else
echo " Cannot write to the file"
fi

#Advanced if-then Features


#Using double parantheses (( expression ))
# for advanced mathematical formulae
#!/bin/bash
val1=10
if (( $val1 ** 2 > 90 ))
then
(( val2 = $val1 ** 2 ))
echo "The square of $val1 is $val2"
fi
~

#Advanced if-then Features


#Using double brackets [[ expression ]]
# for advanced string manipulation (pattern matching)
#!/bin/bash

if [[ $USER == p???? ]]
then
echo "Hello $USER"
else
echo "System detects a new user"
fi
~

# Yet ANOTHER CONSTRUCT


#!/bin/bash
if [ $USER = "norman" ]
then
echo "Welcome $USER"
echo "You may login now!"
elif [ $USER = "samantha" ]
then
echo "Welcome $USER"
echo "You may login now!"
elif [ $USER = testing ]
then
echo "Special testing account"
elif [ $USER = jessica ]
then
echo "Admin Alert!"
echo "Please make a copy of files in system!"
else
echo "System detects an invalid username!"
fi

# Yet ANOTHER CONSTRUCT PART-2


#!/bin/bash
# case - esac

case $USER in
norman | samantha)
echo "Welcome $USER"
echo "You may login now!";;

testing) echo "Special testing account";;


jessica) echo "Admin Alert!"
echo "make a copy of files in system!";;
*) echo "System detects an invalid username!"
esac

#!/bin/bash
# ### Try it yourself ####
# Write a shell script to search to the following(no menu required)
# 1. Search for your college 2.Search for your course
# Accept both college and course as a user input mycourse
# Use four CSE MEC ECE EEE
# Ensure that the user enters a value

#######MAKE APPROPRIATE CHANGES #################

clear
read -p "Enter your college:" college
case $college in
MIT) echo "ok";;
*) echo "No such college!"
exit
esac

while [ -z $mycourse ]
do
read -p "Enter the course you belong to?" mycourse
if [ -z $mycourse ]; then
printf '\nYou did not enter a course, try again!\n'
else
printf '\nHello $USER!\n\t'
echo "You entered '$mycourse' "
printf '\n\n'
fi
done
case $mycourse in
ECE) echo "I belong to $mycourse";;
EEE) echo "I belong to $mycourse";;
MEC) echo "I belong to $mycourse";;
CSE) echo "I belong to $mycourse";;
*) echo "Invalid choice of course!"
esac

#!/bin/bash
# basic for command
for mystate in WB UP TN KL MP
do
echo The name of current state is $mystate
done
#!/bin/bash
# another example of how not to use the for command
for var in I said "let's" try this at 3 o’ clock”
do
echo "word:$var"
done

#!/bin/bash
# another example of how not to use the for command
for var in 1 2 3 45 55
#Paris New York London 'New Delhi' Tokyo )
do
echo "Now going to $var"
done

#!/bin/bash
# using a variable to hold the list
cities='Bengaluru Chennai Hyderabad'
cities=$cities' Mumbai'
for city in $cities
do
echo "I have to visit $city!"
done

#!/bin/bash
# iterate through all the files in a directory
for file in ./m*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
fi
done
#!/bin/bash
# iterating through multiple directories
for file in ./m* ./mydir/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
else
echo "$file doesn't exist"
fi
done

#!/bin/bash
# testing file execution
read -p "Script file name:" fname
if [ -x $fname ]
then
echo "You can run the script:"
./$fname
else
echo "You do not have execute permission!"
echo " Initiating file permission change!"
chmod u+x $fname
echo "$fname has execute permissions now, please check!"
fi

#!/bin/bash
# another example of how not to use the for command
for var in 14 5 4 3 2 1 0
do
echo "Program terminating in $var seconds"
done

You might also like