#Score Card, Change Using Case-Esac
#Score Card, Change Using Case-Esac
/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
#!/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"
#!/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
if [[ $USER == p???? ]]
then
echo "Hello $USER"
else
echo "System detects a new user"
fi
~
case $USER in
norman | samantha)
echo "Welcome $USER"
echo "You may login now!";;
#!/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
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