Blog Single
Bash Script Activity 1
1. Write a shell script
  • Convert the below commands to script style.
  • Use validation.
  • mkdir D1
  • cp *.py D1
  • cd D1
  • chmod a+x *
  • After changing the file permission display list of files.
  • ls -l
#!/bin/bash
mkdir D1 2>>error.log
if [ $? -eq 0 ];then
	echo "Directory D1 is created"
else
	echo "Directory D1 is creation is failed"
	echo "Read error.log file"
	exit # exit from script
fi
cp *.py D1
if [ $? -eq 0 ];then
	echo "All python files are copied to D1 directory"
fi
cd D1
if [ $? -eq 0 ];then
	echo "Changed to D1 directory"
	echo "Now Current working directory is:$PWD"
	echo "List of files:-"
	ls -l *
fi
chmod a+x *
echo # empty line
echo "After changing file permission:-"
echo "List of files:-"
ls -l *
echo “End of the script”
2. Write a shell script that takes the path as input and count all the sub directories recursively.
(eg: /home/student/project/module1/Code/) 
#!/bin/bash
count=`ls -R /home/student/project/module1/Code/|wc -l`
echo "Total no.of files:$count"
3. Write a shell script
  • Test whether the login user is root user and perform the below cleanup operation.
  • Command: cp /dev/null /var/log/testrepo.log
#!/bin/bash
if [ `whoami` == "root" ];then
	cp /dev/null /var/log/testrepo.log
	if [ $? -eq 0 ];then
		echo "testrepo.log file is cleaned"
	else
		echo "testrepo.log file is not cleaned"
		echo "read error.log file for further message"
	fi
else
	echo "Sorry your not root user"
	exit
fi
4. Write a shell script
  • Using file test operators, validate following task.
  • 1. Read a directory name from STDIN(keyboard) and test whether it exist or not.
  • 2. if the directory exists, copy list of all .log files into directory.
  • 3. using tar command create a backup of all .log files.
  • Note: Use $?s Variable to display status operation.
#!/bin/bash
read -p "Enter a directory name:" dname
if [ -d $dname ];then
	cp *.log $dname
	if [ $? -ne 0 ];then
		echo "cp operation is failed"
		exit
	fi
else
	echo "Sorry directory $dname is not exists"
fi
cd  $dname # change to directory
tar -cf BACKUP.tar *
if [ $? -ne 0 ];then
	echo "tar operation is failed"
	exit
fi
5. Write a shell script
  • Read a user name, shell name, working kernel name from
  • Using single echo command displays all the inputs line by line.
#!/bin/bash
read -p "Enter a user name:" name
read -p "Enter a shell name:" sh_name
read -p "Enter a working os name:" os

echo  "
----------Input Details------------
USER NAME: $name
SHELL NAME: $sh_name
OS NAME: $os
-----------------------------------"
6. Write a shell script
  • Read a user name from command line argument
  • Test user name is root (or) admin, if any of the user name is matched read a shell name from , test input shell name is bash or ksh or sh. among the three shells, if any one shell name is matched, display input shell name and user name
#!/bin/bash
if [ $# -ne 1 ];then
	echo "Usage:Commandline argument will accept single argument"
	echo "Example:./p6 "
	exit
fi
if [ $1 == "root" -o $1 == "admin" ];then
     read -p "Enter a shell name:" sh_var
     if [ $sh_var == "bash" -o $sh_var == "ksh" -o $sh_var == "sh" ];then
		echo -e "Login name:$1\t Shell name:$sh_var"
     else
		echo "Sorry $sh_var shell is not matched"
     fi
else
	echo "Sorry $1 is not valid login name"
fi
7. Write a shell script to display the processes based on %CPU and Memory usage.
  • 1. Perform the above task Without argument (default)
  • 2. Specify the argument (cpu or mem) and It list the processes based on arguments
  • Note: use command line arguments,ps,sort commands
#!/bin/bash
echo "Start Time" `date`
# By default, it display the list of processes based on the cpu and memory usage #
if [ $# -eq 0 ];then
	echo "List of processes based on the %cpu Usage"
	ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu  # sorted based on %cpu
	echo "List of processes based on the memory Usage"
	ps -e -orss=,args= | sort -b -k1,1n # sorted bases rss value
         # If arguements are given (mem/cpu)
else
	case "$1" in
	mem)
	 echo "List of processes based on the memory Usage"
 	 ps -e -orss=,args= | sort -b -k1,1n
	 ;;
 	cpu)
	 echo "List of processes based on the %cpu Usage"
	 ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu
	 ;;
 	*)
		echo "Invalid Argument Given \n"
		echo "Usage : $0 mem/cpu"
	esac	
fi
echo "End Time" `date`
8. Write a shell script to read a filename from command line argument
  • 1. Test input file is exists or not?
  • 2. If file exists, determine file type.
  • Note use file test operators instead of file command.
#!/bin/bash
if [ $# -ne 1 ]; then
	echo "Usage:Commandline argument error" ; exit 
fi
if [ -e $1 ];then # test file is exists or not
	if [ -f $1 ];then
		echo "File:$1 is a regular file"
	elif [ -d $1 ];then
		echo "File:$1 is a directory file"
	elif [ -c $1 ];then
		echo "File:$1 is a character type file"
	elif [ -b $1 ];then
		echo "File:$1 is a block type file"
	elif [ -l $1 ];then
		echo "File:$1 is a link type file"
	elif [ -p $1 ];then
		echo "File:$1 is a namedpipe type file"
	elif [ -S $1 ];then
		echo "File:$1 is a Socket type file"
	fi
else
	echo "Sorry file $1 is not exists"
fi
9. Write a shell script
  • Using case statement modify the below code
  • name=`whoami`
	if  [ $name == "root" -o  $name == "admin" ];then
		yum -y install package
	elif [ $name == "userA" -o $name == "userB" ];then
		service status deamon
	else
		echo "Sorry your name not exists"
	fi
	
#!/bin/bash
name=`whoami`
case $name in
root|admin)  echo "Install following packages" 
	          # yum install package
		;;
userA|userB)	echo "Test daemon status"
		# service status daemon.service
		# service status sshd.service
		;;
*)		echo "Sorry $name your name not exists"
esac
10. Write a shell script
  • Read a business enquiry number from
  • Test whether the input enquiry number ranges between 501 and 599
  • If input enquiry number is valid, read a quotation number from
  • Test whether the input quotation number ranges between 501 and 800
  • If quotation number is valid read a customer name from .
  • Test whether the input customer name is "cusA" or "cusB" or "cusC".
  • If customer name matches with any of the three above names, read a PO number from .
  • Test whether the PO number ranges between 500 and 1000
  • if input PO number is valid display all input details(enquiry number,quotation,customer,PO)
  • Note: above is dependency based task. Hence try nested condition style.
#!/bin/bash
read -p "Enter a enquiry number:" eno
if [ $eno -gt 500 -a $eno -lt 600 ];then
	read -p "Enter a quotation number" qno
	if [ $qno -gt 500 -a $qno -le 800 ];then
		read -p "Enter a customer name:" cname
		if [ $cname == "cusA" -o $cname == "cusB" -o $cname == "cusC" ];then
			read -p "Enter a PO number" po
			if [ $po -ge 500 -a $po -le 1000 ];then
				echo -e "Business Enquiry number:$eno\tQuotation number:$qno"
				echo -e "Customer name:$cname\t PO Number:$po"
				echo "PO Received Date:`date +%D`"
			else
				echo "Invalid PO Number"
			fi
		else
			echo "Invalid customer name"
		fi
	else
		echo "Invalid quotation number"
	fi
else
	echo "Sorry input enquiry number is invalid"
fi
11. Write a shell script
  • Count the number of processes using ps -e,wc -l commands
  • Test if the count is above 200 or not. If count is above 200 calculate total number of bash processes.
	if  [ $name == "root" -o  $name == "admin" ];then
		yum -y install package
	elif [ $name == "userA" -o $name == "userB" ];then
		service status deamon
	else
		echo "Sorry your name not exists"
	fi
	
#!/bin/bash
if [ `ps -e|wc -l` -gt 200 ];then
	echo "more than 200 processes are running in your system"
else
	echo "less than 200 processes are running in your system"
fi
12. Using cut command split given string into separate variables
       S="root:0:/bin/bash:/root"
       Expected Result
       name="root"
       ID=0
       sh=/bin/bash
       Login_Path=/root
	
  • How to convert to sh,Login_Path variables to shell variables?
#!/bin/bash
S="root:0:/bin/bash:/root"
name=`echo $S|cut -d":" -f 1`
ID=`echo $S|cut -d":" -f 2`
sh=`echo $S|cut -d":" -f 3`
Login_Path=`echo $S|cut -d":" -f 4`
export sh
export Login_Path
echo "Name:$name
ID:$ID
SH_NAME:$sh
Login_Path:$Login_Path"
13. Write a shell script
  • fnames="/home/usrA/p1.sh:/bin/p2.sh"
  • How to append the following files to existing variable /usr/bin/p3.sh and /home/usrB/p4.sh
  • Expected Result
  • fnames="/home/usrA/p1.sh:/bin/p2.sh:/usr/bin/p3.sh:/home/usrB/p4.sh"
#!/bin/bash
fnames="/home/usrA/p1.sh:/bin/p2.sh"
fnames="$fnames:/usr/bin/p3.sh:/home/usrB/p4.sh"
echo $fnames
14. Write a shell script
  • s="kumar,sales,4000"
  • task 1 : using cut command split the above string with comma(,) as delimiter and extract the value 4000
  • task 2 : using bc tool calculate 12 % of 4000 and initialize the result to a local variable.
  • task 3 : using echo command display the calculated value and employee name(kumar).
#!/bin/bash
s="kumar,sales,4000"
cost=`echo $s|cut -d, -f 3`
result=`echo "$cost*0.12"|bc`
name=`echo $s|cut -d, -f1`
total=`echo $result+$cost|bc`
echo -e "Name:$name\n12% of cost is:$result"
echo "Sum of cost is:$total" 
15. Write a shell script
  • a) How to display - "Welcome to unix shell script" (including " " Quotes)
  • Expected result: “Welcome to unix shell script”
  • b) name="root"
  • Expected result: "$name value is:root"
  • c) V1=10
  •    V2=20
  • Expected result: "V1 value and V2 value:$V1 * $V2 = 200"
#!/bin/bash
echo \"Welcome to unix shell script\"

echo '"Welcome to unix shell script"' # another way

name="root"
echo '$name value is:'$name
echo "\$name value is:$name" # another way

V1=10
V2=20
echo "V1 value and V2 value:\$V1*\$V2=`expr $V1 \* $V2`"
Tags:
Share:
Comments
Write a comment
Your email address will not be published. Required fields are marked *
Review*
Name*
Email*
Notify me of new posts by email