Chapter 5 answers

From Linux Shell Scripting Tutorial - A Beginner's handbook
Revision as of 13:15, 17 July 2011 by Ansh (talk | contribs)
Jump to navigation Jump to search

← Chapter 5 ChallengesHomeChapter 6: Shell Redirection →

  • Decide whether the following sentence is true or false:
  1. False
  2. False
  3. False
  4. False
  5. True
  6. True
  7. True

  • Write a menu driven script using the select statement to print calories for food items such as pizza, burger, Salad, Pasta etc.

  • Write a shell script that, given a file name as the argument will count vowels, blank spaces, characters, number of line and symbols.
#!/bin/bash
file=$1
v=0
 
if [ $# -ne 1 ]
then
	echo "$0 fileName"
	exit 1
fi
if [ ! -f $file ]
then
	echo "$file not a file"
	exit 2
fi
 
while read -n 1 c
do
  l=$(echo $c | tr [:upper:] [:lower:])
  [[ "$l" == "a" || "$l" == "e" || "$l" == "i" || "$l" == "o" || "$l" == "u" ]] && (( v++ ))
done < $file
 
echo "Vowels : $v"
echo "Characters : $(cat $file | wc -c)"
echo "Blank lines : $(grep  -c '^$' $file)"
echo "Lines : $(cat $file|wc -l )"

  • Write a shell script that, given a file name as the argument will count English language articles such As 'A', 'An' and 'The'.
#!/bin/bash
file=$1
a=0
 
if [ $# -ne 1 ]
then
	echo "$0 fileName"
	exit 1
fi
if [ ! -f $file ]
then
	echo "$file not a file"
	exit 2
fi
 
while read line
do
  l=$(echo $line | tr [:upper:] [:lower:])

for word in $l
do
	[[ $word == "a" || $word == "an" || $word == "the" ]] && ((a++))
done

done < $file
 
echo "articles : $a"

  • Write a shell script that, given a file name as the argument will write the even numbered line to a file with name evenfile and odd numbered lines in a text file called oddfile.
#!/bin/bash
file=$1
counter=0
 
if [ $# -ne 1 ]
then
	echo "$0 fileName"
	exit 1
fi
if [ ! -f $file ]
then
	echo "$file not a file"
	exit 2
fi
 
while read line
do
	((counter++))
	EvenNo=$(( counter%2 ))

	if [ $EvenNo -eq 0 ] 
	then
		echo $line >> evenfile 
	else
		echo $line >> oddfile
	fi

done < $file

  • Write a shell script to monitor Linux server disk space using a while loop. Send an email alert when percentage of used disk space is >= 90%.

  • Write a shell script to determine if an input number is a palindrome or not. A palindromic number is a number where the digits, with decimal representation usually assumed, are the same read backwards, for example, 58285.

  • Write a shell program to read a number *such as 123) and find the sum of digits (1+2+3=6).

  • Write a shell program to read a number and display reverse the number. For example, 123 should be printed as as 321.

  • Write the shell program which produces a report from the output of ls -l in the following format using the for loop statement:
file1
file2
[DIR] test/
Total regular files : 7
Total directories : 4
Total symbolic links : 0
Total size of regular files : 2940

  • Write a shell script that will count the number of files in each of your sub-directories using the for loop.

  • Write a shell script that accepts two directory names as arguments and deletes those files in the first directory which are similarly named in the second directory.

  • Write a shell script to search for no password entries in /etc/passwd and lock all accounts.

  • Write a shell program to read two numbers and display all the odd numbers between those two numbers.

← Chapter 5 ChallengesHomeChapter 6: Shell Redirection →