Chapter 3 answers

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search

← Chapter 3 ChallengesHomeChapter 4: Conditionals Execution (Decision Making) →

1. Make a backup of existing variable called PS1 to OLDPS1. Set PS1 to '$'. Reset your prompt using OLDPS1 variable.

OLDPS1=$PS1
PS1='$'
date
clear
PS1=$OLDPS1
date


2. Customize your bash prompt by setting PS1 variable to 'I Love Scripting '.

PS1='I Love Scripting '
date
clear


3. Edit your $HOME/.bashrc file and set your new PS1 variable.

vi $HOME/.bashrc
PS1='I Love Scripting '

Save and close the file.



4. Create a list of legal and illegal bash variable names. Describe why each is either legal or illegal.

# legal variables 
backup="/nas0"
_datasrc="/dev/st0"
# illegal variables
# white space
_ data src="/dev/st0"


5. Write a command to display the environment.

printenv


6. Write a shell script that allows a user to enter his or her top three ice cream flavors. Your script should then print out the name of all three flavors.

#!/bin/bash
read -p "Enter your three ice cream flavors : " ice1 ice2 ice3
echo "Thanks $USER!"
echo "1# ${ice1}"
echo "2# ${ice2}"
echo "#3 ${ice3}"


7. Write a shell script that allows a user to enter any Internet domain name (host name such as www.cyberciti.biz). Your script should than print out the IP address of the Internet domain name.

#!/bin/bash
# Version 1
read -p "Enter any Internet domain name : " domainname
host "${domainname}"

OR

#!/bin/bash
# Version 2
read -p "Enter any Internet domain name : " domainname
host "${domainname}" | grep 'has address'


8. Write a shell script that allows a user to enter any existing file name. The program should then copy file to /tmp directory.

#!/bin/bash
# Version 1 (blind copy)
read -p "Enter any file name : " filename
cp $filename /tmp

OR

#!/bin/bash
# Version 2 (first check for $filename and than copy it, else display an error message)
read -p "Enter any file name : " filename

# if file exists, than copy it
if [ -f "${filename}" ]
then
	cp -v "$filename" /tmp
else
	echo "$0: $filename not found."
fi


9. Write a shell script that allows a user to enter directory name. The program should then create directory name in /tmp directory.

#!/bin/bash
# Version 1 (blind "mkdir directory" command)
read -p "Enter any directory name : " directory 
mkdir "/tmp/${directory}"

OR

#!/bin/bash
# Version 2 (Make sure dir does not exist, else display an error message)
read -p "Enter any directory name : " directory 

# Make sure dir does not exits
if [ ! -d "/tmp/${directory}" ]
then
	mkdir -v "/tmp/${directory}" 
else
	echo "$0: /tmp/${directory} already exits."
fi

OR

#!/bin/bash
# Version 3 
# Make sure dir does not exist, else display an error message
# Use variables 
BASE="/tmp"

read -p "Enter any directory name : " directory 

# create a path
mydir="${BASE}/${directory}"

# Make sure dir does not exits
if [ ! -d "${mydir}" ]
then
	mkdir -v "${mydir}" 
else
	echo "$0: ${mydir} already exists."
fi


10. Write a shell script that allows a user to enter three file names. The program should then copy all files to USB pen.

#!/bin/bash
# Version 1
PEN="/media/usb"

read -p "Enter three file names : " f1 f2 f3
cp -v "$f1" "$f2" "$f3" $PEN

OR

#!/bin/bash
# Version 2
# Make sure file exits and usb pen is mounted at $PEN
# Set path
PEN="/media/usb"

read -p "Enter three file names : " f1 f2 f3

# Make sure pen drive exits else die
[ ! -d "$PEN" ] && { echo "$0: USB pen drive not found at $PEN"; exit 1; } 

# Make sure pen drive is mounted else die
if grep -wq "$PEN" /etc/mtab 
then   
	# Make copy only if file exists, else display an error
	[ -f "$f1" ] && cp -v "$f1" $PEN || echo "$0: $f1 not found."
	[ -f "$f2" ] && cp -v "$f2" $PEN || echo "$0: $f2 not found."
	[ -f "$f3" ] && cp -v "$f3" $PEN || echo "$0: $f3 not found."
else   
	echo "$0: USB pen is not mounted at $PEN."
fi


11. Write a simple shell script where the user enters a pizza parlor bill total. Your script should then display a 10 percent tip.

#!/bin/bash
# Version 1
clear
echo "*************************"
echo "*** Joes Pizza Parlor ***"
echo "*************************"
echo 
echo "Today is $(date)"
echo 
read -p "Enter a pizza parlor bill : " bill

tip=$(echo "scale=2; (${bill}*10) / 100" | bc -l)
total=$(echo "scale=2; $tip + $bill" | bc -l)
echo "Pizza bill : $bill"
echo "Tip (10%)  : ${tip}"
echo "--------------------------"
echo "Total      : ${total}"
echo "--------------------------"


12. Write a simple calculator program that allows user to enter two numeric values and operand as follows. The program should then print out the sum of two numbers. Make sure it works according to entered operand.

#!/bin/bash
read -p "Enter two values : " a b
read -p "Enter operand ( +, -, /, *) : " op
ans=$(( $a $op $b ))
echo "$a $op $b = $ans"

← Chapter 3 ChallengesHomeChapter 4: Conditionals Execution (Decision Making) →