0% found this document useful (0 votes)
16 views10 pages

Part A Procedure

Uploaded by

asmazarazoya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Part A Procedure

Uploaded by

asmazarazoya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Write a shell script that displays list of all the files in


the current directory to which the user has read, write and
execute permissions.

for file in *
do
if [ -f $file ]
then
if [ -r$file -a -w$file -a -xfile ]
then
ls -l $file
fi
fi
done

procedure:
step 1- open a nano editor
nano progname.sh
step 2- type the program
step 3- save the program
ctrl+x
press Enter
step 4- Execute the program
sh progname.sh

2.Write a shell script that accepts one or more file nameand


converts all of them to uppercase, provided they exist in the
current directory.

echo "Enter filename"


read file
if [ ! $file ]
then
echo "Filename doesnot exists"
fi
tr '[a-z]' '[A-Z]' <$file

procedure:
step 1-create a file with content
cat>filename (creating a file with content)
hello world (content)
ctrl+d (to save the file)
step 2- open a nano editor
nano progname.sh
step 3- type the program
step 4- save the program
ctrl+x
press Enter
step 5- Execute the program
sh progname.sh
1. Write grep commands to the following:
a. To select the lines from a file that has exactly 2
characters.
b. To select the lines from a file that has more than one
blank spaces.

a. To select the lines from a file that has exactly 2 characters.


echo "Enter file name: "
read file1
echo “Lines that has exactly has 2 characters”
grep -x '..' file1

procedure:
step 1-create a file with content
cat>filename (creating a file with content)
hello
hi
world
my
ctrl+d (to save the file)
step 2- open a nano editor
nano progname.sh
step 3- type the program
step 4- save the program
ctrl+x
press Enter
step 5- Execute the program
sh progname.sh

Output: $sh 5a.sh


Enter file name:
filename
Lines that has exactly has 2 characters
hi
my
b. To select the lines from a file that has more than one blank spaces.
echo "Enter file name: "
read file1
echo “Lines that has has more than one blank space”
grep ' .' file1

procedure:
step 1-create a file with content
cat>filename (creating a file with content)
hi my name is rama
welcome to my world
ctrl+d (to save the file)
step 2- open a nano editor
nano progname.sh
step 3- type the program
step 4- save the program
ctrl+x
press Enter
step 5- Execute the program
sh progname.sh

Output: $sh 5b.sh


Enter file name
filename
hi my name is rama
2. Write a shell script which accepts two file names as
arguments. Compare the contents. If they are same, then
delete the second file.

echo "Enter first


file name " read
file1
echo "Enter second
file name " read
file2

if cmp -s $file1 $file2


then
echo "Contents of both files are same "
rm $file2
echo “successfully removed duplicate file”
else
echo "Contents of both files are not same”
fi

procedure:
step 1-create a file with content
cat>file1
hello wolrd
ctrl+d
step 2- create a second file
cat>file2
hello wolrd
step 3- open a nano editor
nano progname.sh
step 4- type the program
step 5- save the program
ctrl+x
press Enter
step 6- Execute the program
sh progname.sh

Output:$sh 6.sh
Enter first file name
file1
Enter second file name
file2
contents of the files are same
successfully removed duplicate file
3. Write a shell script
a. to count number of lines in a file that do not contain
vowels.
b. to count number of characters, words, lines in a given
file.

a. to count number of lines in a file that do not contain vowels.

echo "Enter file name "


read file1
echo “Number of lines that do not contain vowels”
grep -civ '[aeiou]' $file1

procedure:
step 1-create a file with content
cat>filename (creating a file with content)
try
hello
my
ctrl+d (to save the file)
step 2- open a nano editor
nano progname.sh
step 3- type the program
step 4- save the program
ctrl+x
press Enter
step 5- Execute the program
sh progname.sh

Output: $sh 7a.sh


Enter file name
file1
Number of lines that do not contain vowels-2
b. to count number of characters, words, lines in a given file.

echo “Enter file name”


read file1
c=`cat $file1 | wc -m`
w=`cat $file1 | wc -w`
l=`cat $file1 | wc -l`
echo “Number of characters in $file is $c”
echo “Number of words in $file is $w”
echo “Number of lines in $file is $l”

procedure:
step 1-create a file with content
cat>filename (creating a file with content)
hello welcome to my world
ctrl+d (to save the file)
step 2- open a nano editor
nano progname.sh
step 3- type the program
step 4- save the program
ctrl+x
press Enter
step 5- Execute the program
sh progname.sh

Output:-$sh 7b.sh
Enter file name
f1
Number of characters in f1 is 33
Number of words in f1 is 6
Number of lines in f1 is 4
4. Write a shell script to list all the files in a given
directory.

echo "Enter directory name"


read dir
if [ -d $dir ]
then
echo "List of files in the directory"
ls -p $dir |grep -v /
else
echo "Enter proper directory name"
fi

procedure:
step 1-create a directory
mkdir dir
cd dir
touch file1 (creating 3 empty files)
touch file2
touch file3
mkdir subdir (creating a subdirectory)
step 2- open a nano editor
nano progname.sh
step 3- type the program
step 4- save the program
ctrl+x
press Enter
step 5- Execute the program
sh progname.sh

Output:$sh p8.sh
Enter the directory name
dir
List of files in the directory are
file1 file2 fil3
5. Write a shell script to display list of users currentlylogged
in.

echo "List of users currently logged in is:$(who)"

Output:$sh p9.sh
List of users currently logged in is: Student

6. Write a shell script to read three text files in the


current directory and merge them into a single file and
returns a file descriptor for the new file.

echo "Enter first


file name " read
file1
echo "Enter second
file name " read
file2
echo "Enter third
file name " read
file3

if [ -f $file1 -a -f $file2 -a -f $file3 ]


then
echo “ all three file exists “
cat $file1 $file2 $file3 > $new.txt
else
echo “files does not exist”

procedure:
step 1-create a file with content
cat>file1
hi
ctrl+d (to save the file)
cat>file3
hello
ctrl+d
cat>file2
from me
ctrl+d
create three files with content
step 2- open a nano editor
nano progname.sh
step 3- type the program
step 4- save the program
ctrl+x
press Enter
step 5- Execute the program
sh progname.sh
Output:$sh 10.sh
Enter first file name
f1
Enter second file name
f2
Enter third file name
f3
all three files exists

$cat new.txt
Hi hello from me

You might also like