Bash shell examples
A few random Bash scripts I wrote a long time ago.
--------------------------
BIRTHDAY
bart 01/01/2000
lisa 02/03/2001
moe 04/09/1902
fred 06/04/1832
---------------------------------------------------------
#!/bin/bash
#This script will search the birthday file for a name the display the
#calendar month of the year they were born.
echo -e "\nHere is a list of everyone in the birthday file:\n"
awk '{print $1}' birthday
echo -e "Enter the name of the person who's birthday you need to know\n"
read NAME
grep $NAME birthday > birth
A=$(awk -F/ '{print $2 }' birth)
B=$(awk -F/ '{print $3}' birth)
clear
echo -e "Here is the calendar for the month and year $NAME was born\n"
cal $A $B
---------------------------------------------------
#!/bin/bash
#this program sorts the passwd file by group id then displays a list of user name and group id's
awk -F: '{print $1, $5}' /etc/passwd | sort -2 | more
----------------------------------------------------------
#!/bin/bash
#This script will display a list of all users who's id numbers are between
#the number entered by the user.
echo -e "This script will display all the users who id numbers are between\n"
echo -e "the two fields entered by the user\n"
echo -e "Enter starting number:\c"
read A
echo -e "\nEnter ending number:\c"
read B
echo -e "User Name/UID"
while [ "$A" -le "$B" ]
do
grep ":$A:" /etc/passwd | awk -F: '{print $1, $3}' >> uidl
let A="$A"+1
done
sort uidl |uniq | more
-----------------------------------------------------------
#!/bin/bash
#This script will display files belonging to the user you specify in the
#directory you specify
echo -e "This script will display files belonging to the user you specify in the directory you specify\n"
echo -e "Enter user name: \c"
read NAME
echo -e "\nEnter directory to search in (all subdirectories will be searched:\c"echo -e "\n\n"
read DIR
find "/$DIR" -type f -user "$NAME" >file5 2>/dev/null
if [ -s file5 ]
then
more file5
else
echo -e "\nFile not found"
fi
rm file5
----------------------------------------------------------
#!/bin/bash
echo -e "This script will display a file of your choice\n"
echo -e "Enter file name: \c"
read FILENAME
echo -e "\nEnter directory to search in (all subdirectories will be searched:\c"echo -e "\n\n"
read DIR
find "/$DIR" -type f -name "$FILENAME" >file5 2>/dev/null
if [ -s file5 ]
then
more file5
else
echo -e "\nFile not found"
fi
rm file5
--------------------------------------
#!/bin/bash
echo -e "This program will display the user\n id and group id number for the name you enter below\n"
echo -e "Enter name: \c"
read NAME
echo -e "Name UID GID\n"
awk -F: '{print $1, $3, $4}' /etc/passwd | grep $NAME > namef
more namef
rm namef
----------------------------------------------
#!/bin/bash
#This script displays a list of who is online and what is in their comment field
echo -e "Here is a list of who is "
echo -e "online and anything in their comment field.\n"
awk -F: '{print $1, $5}' /etc/passwd > file
who | awk '{print $1}' > file2
sort file > file11
sort file2 > file22
./join -1 1 -1 1 file11 file22 > file3
echo -e "\nUser Comment Field"
echo -e "~~~~ ~~~~~~~~~~~~~"
uniq file3
rm file
rm file2
rm file11
rm file22
rm file3