Bash Guide for Beginners, chapter 4 solutions

Solutions to:

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_05.html
1.

grep bash /etc/passwd | cut -d: -f1

2.

grep "^daemon" /etc/group

3.

grep --invert-match "^daemon" /etc/group

4. info with line numbers:

grep -n localhost /etc/hosts

number of occurrences of string:

grep -o localhost /etc/hosts |wc -w

5.

cd /usr/share/doc
for file in *
do
	if grep "$file" /etc/shells > /dev/null
	then
		echo "$file"
	fi
done

6.

sum=0
for file in *
do
	if grep "$file" /etc/shells > /dev/null
	then
		sum=`expr $sum + $(find "$file" -name README |wc -l)`
	fi
done
echo $sum

7.

find . -mtime 0.42 -type f
for file in *
do
	if [ `stat -c %Y "$file"` -gt `expr $(date "+%s") - 36000` ] && [ -f "$file" ]
	then
		echo "$file"
	fi
done

8.

9.

grep -c ".*" test

10.

grep "^/dev" /etc/fstab | awk '{print $1}'
grep -o "^/dev[^[:space:]]*" /etc/fstab

11.

user=rwh
if grep "^$user:" /etc/passwd > /dev/null
then
	echo user $user exists in the fstab
else
	echo user $user does not exist in the fstab
fi

12.

cd /etc
ls |grep [0-9]
ls |grep "[[:digit:]]"
This entry was posted in answers, scripting. Bookmark the permalink.

2 Responses to Bash Guide for Beginners, chapter 4 solutions

  1. punbra says:

    Could you post the answers for chapters 10 and up?

  2. Hail says:

    ls -R /usr/share/doc | grep -v “.a_string” | grep -c README

Leave a Reply

Your email address will not be published. Required fields are marked *