CSC220 Homework 1 Solutions 2010
--D. Thiebaut 12:47, 28 September 2010 (UTC)
Contents
allUsersByDate2.sh
Version 1
#! /bin/bash
# allUsersByDate2.sh
# Julia Burch
# September 22, 2010
# Like the first script, this one outputs all the users in the /Users
# directory by date of their last access. This time, only usernames are visible.
# Usage: ./allUsersByDate2.sh
for year in "2005" "2006" "2007" "2008" "2009" "2010\|:"
do
if [ "$year" = "2010\|:" ]; then
echo "2010"
else
echo $year
fi
ls -l /Users | grep $year > results.txt
cat results.txt |
while read line
do
IFS=" "
arr=($line)
if [ ${arr[8]} != "lost+found" ]; then
echo ${arr[8]}
fi
done
echo
done
Version 2
#! /bin/bash
# allUsersByDate2.sh
# Alex Cheng (220a-ag)
# 09/16/10
# A script that lists all the users on grendel/beowulf by year
# of last access to their account. The output should only show
# the user's name and should list all the years# starting in
# 2005 and ending in 2010.
#
# Sample Output:
# 2006:
# flatland
# bmikic
# mmartin2
#
# Usage:
# allUsersByDate2.sh
for number in 2005 2006 2007 2008 2009 2010 ; do
if [ $number = '2010' ] ; then
echo "2010:"
ls -ltr /Users | grep $number | grep -v lost | awk '{print $9}'
ls -ltr /Users | grep ':' | grep -v lost | awk '{print $9}'
else
echo ${number}:
ls -ltr /Users | grep $number | grep -v lost | awk '{print $9}'
fi
echo
done
allUsersByDate.sh
#! /bin/bash
# allUsersByDate.sh
# Julia Burch
# September 22, 2010
# This script lists all users on grendel by year of last access to their account.
# Usage: ./allUsersByDate.sh
for year in "2005" "2006" "2007" "2008" "2009" "2010\|:"
do
if [ "$year" = "2010\|:" ]; then
echo "2010"
else
echo $year
fi
ls -l /Users | grep $year
echo
done
biggestVar2.sh
Version 1: sweet and simple!
#! /bin/bash
# biggestVar.sh
# Julia Burch
# September 22, 2010
# This script prints the 10 largest directories in /var.
# ./biggestVar.sh
du -h /var 2>&1 | sort -h | tail -10
Version 2: sophisticated and more complex
#! /bin/bash
# biggestVar2.sh
# Alex Cheng (220a-ag)
# 09/16/10
# A cript that prints out the 10 largest directories
# with the size showing up in human readable format.
# With help from: http://ubuntuforums.org/showthread.php?t=885344
#
# Sample Output:
# 84M/var/cache/yum/x86_64/13
# 84M/var/cache/yum/x86_64
# 102M/var/lib/rpm
# 151M/var/cache/yum/Fedora 13 - x86_64
# 195M/var/lib
# 289M/var/cache/yum
# 292M/var/cache
# 549M/var/spool/mail
# 549M/var/spool
# 1.1G/var
#
# Usage:
# biggestVar2.sh
# get the list of files in var and their size
# puts stderr in err.out, puts stdout in out.out
du -k /var > /tmp/err.out 2>&1 > /tmp/out.out
# sorts out.out numerically
# puts stderr in intermerr.out, puts stdout in intermout.out
cat /tmp/out.out | sort -n | cut -f2 | xargs -d '\n' du -sh > /tmp/intermerr.out 2>&1 > /tmp/intermout.out
# gets the 10 largest files
cat /tmp/intermout.out | tail -10
# removes excess files
rm /tmp/err.out
rm /tmp/out.out
rm /tmp/intermerr.out
rm /tmp/intermout.out
biggestVar.sh
#! /bin/bash
# biggestVar.sh
# Julia Burch
# September 22, 2010
# This script prints the 10 largest directories in /var.
# ./biggestVar.sh
du /var 2>&1 | sort -n | tail -10
lab1answers.sh
#! /bin/bash
# lab1answers
# Julia Burch
# September 22, 2010
# Answers to lab 1 exercises
# ./lab1answers.sh
echo "Path-Related Section: Question 1"
echo
pwd
echo
echo "Path-Related Section: Question 2"
echo
ls ..
echo
echo "Path-Related Section: Question 3"
echo
ls /home
echo
echo "Questions About File-Searching: Question 1"
echo
cat /etc/printcap | grep "ford"
echo
echo "Filtering Log Files: Question 1"
echo
wget http://maven.smith.edu/~hadoop/log.txt
wc -l log.txt
echo
echo "Filtering Log Files: Question 2"
echo
grep "real" log.txt | sort -r
echo
echo "Filtering Log Files: Question 3"
echo
grep "real\|processing" log.txt
echo
echo "Pipes: Question 1"
echo
ls /Users | wc -l
echo
echo "Pipes: Question 2"
echo
ls -ltr /Users | tail -3
echo
echo "Pipes: Question 3"
echo
ls -lt /Users | tail -3
echo
echo "Pipes: Question 4"
echo
ls -l /Users | grep "Sep 22"
echo
echo "More challenging pipes: Question 1"
echo
cd /Users/classes/220a/bin
./stdouterr.py 2>&1 | grep "Error"
echo
echo "More challenging pipes: Question 2"
echo
./stdouterr.py 2>&1 | wc -l
echo
echo "More challenging pipes: Question 3"
echo
./stdouterr.py 2>&1 | grep "Error" | wc -l