Write a shell script to enter the records in a file from keyboard and display the file. Make proper validation.

f=0
echo "Enter roll no"
read rn

test1=` echo $rn | grep -x -E '[[:digit:]]+' `
len=` echo -n $test1 | wc -m `

if [ $len -eq 0 ]
then
	echo "Roll number accept only number"
	f=1
fi

echo "Enter name"
read name

test2=` echo $name | grep -x -E '[^0-9]+' `
len2=` echo -n $test2 | wc -m `
if [ $len2 -eq 0 ]
then
	echo "Name accept only character"
	f=1
fi

echo "Enter marks"
read marks

if [ $marks -lt 0 -o $marks -gt 100 ]
then
	echo "Marks must be between 0 to 100"
	f=1
fi

if [ $f -eq 0 ]
then
	echo "Your all input is accepted"
	echo "$rn $name $marks" >> stud.txt
	echo "Content of your file is "
	cat stud.txt
else
	echo "Sorry your input is not accpted"
fi

Comments

Leave a Reply

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