If you have any queries please leave a message here
Your Message
×


FILE HANDLING IN SHELL SCRIPT


Write a shell script to show the contant of a text file

echo "Enter the file name....."
read file
contant=`cat $file`
echo "$contant"
Write a shell script to pick each line from the given text file

echo "Enter the file name....."
read file
while read line
do
# Pick each line from the given file
currentLine=`echo ${line}`
echo "$currentLine"
done < $file
Find the number of vowel and space present in a text file

echo "Enter the file name..."
read file
vowel=0
space=0
while read line
do

# Pick each line from the given file
line=`echo ${line}`

# Calculate the length of each line pick from the file
len=${#line}
for i in {1..$len}
do

# Pick each character from the line
ch=`echo $line | cut -c$i`

# Count total number of space present in the given file
if [ "$ch" == " " ]
then
space=`expr $space + 1`
fi

# Count total number of vowel present in the given file
case $ch in
a|A|e|E|i|I|o|O|u|U) vowel=`expr $vowel + 1` ;;
esac
done
done < $file
echo "Total number of Vowels = $v"
echo "Total number of Blank Space = $s"
Write a shell script to append the contents of a text file into another text file

echo "Enter the source file name....."
read sourceFile
echo "Enter the destination file name....."
read destinationFile

# Check whether the source file has read permission or not
if test -r $sourceFile
then

# Check whether the destination file has write permission or not
if test -w $destinationFile
then
while read line
do
# Copy each line from source file to destination file
`echo ${line} >> $destinationFile`
done < $sourcefile
else
echo "$destinationFile is not exist or not have write permission"
fi
else
echo "$sourceFile is not exist or not have read permission"
fi
Write a shell script that delete odd numbered lines from a given text file

echo "Enter the file name....."
read file
tempfile="/tempfile.txt"
if test -r file
then
# Create a temporary file
`touch $tempfile`
lineNo=1
while read line
do
line=`echo ${line}`

# Check whether the line number is odd or even
r=$(($lineNo % 2))
if test $r -eq 0
then
# Copy even lines into temporary file
echo $line >> $tempfile
fi
lineNo=`expr $lineNo + 1`
done < $file

# Copy the temporary file content into the given file
`cp $tempfile $file`

# Delete the temporary file
`rm -rf $tempfile`
else
echo "$file is not exist or not have read permission"
fi
Write a shell program to translate all the lower case letters in any text file to the upper case letters.

# Input file name from the user
echo "Enter the file name....."
read SourceFile

# Check whether the inputed file is present or not
if test -f $SourceFile
then
# Create a temporary file
TempFile="/Temp"
touch $TempFile

# Pick all the contents of "SourceFile"
# Convert all the lowercase character into uppercase
# Store the converted contents into "TempFile"
tr '[[a-z]]' '[[A-Z]]' < $SourceFile > $TempFile

# Copy "TempFile" data into "SourceFile"
cp $TempFile $SourceFile

# Delete the temporary file
rm -rf $TempFile
else
echo "File Not Found"
fi
Given the filename by the user as the input, write a shell script to display the first five lines of the file.

# Input file name from the user
echo "Enter the file name....."
read file

# Check whether the inputed file is present or not
if test -f $file
then
count=0;
# Read each line from the inputed file
while read line
do
# Pick each line from the file and store it into a variable
currentLine=`echo ${line}`
echo "$currentLine"
count=`expr $count + 1`
if test $count -eq 5
then
break
fi
done < $file
else
echo "File Not Found"
fi
Write a shell script to display the list of the files whose filename consists of 4 characters (with any file extension) and filename starts with the alphabet f.

# Input directory name from the user
echo "Enter the directory name....."
read directory

# Check whether the inputed directory is present or not
if test -d $directory
then
# Create a temporary file
TempFile="/Temp"
touch $TempFile

# Create a list of all the files and directories
# whose filename consists of 4 characters (with any file extension) and
# filename starts with the alphabet f
# Store the created list into "TempFile"
ls $directory | grep '^f...$' > $TempFile
ls $directory | grep '^f...\..*$' >> $TempFile

# Read each line from the "TempFile"
while read line
do
currentLine=`echo ${line}`
File="$directory/$currentLine"
if test -f $File
then
echo "$File"
fi
done < $TempFile
else
echo "Directory Not Found"
fi



ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us