0% found this document useful (0 votes)
7 views9 pages

Assignment 6 U22CS040

The document contains a series of shell scripts for various tasks including deleting lines from files, creating directories, checking file existence, reading file contents, appending data to files, removing duplicate lines, and counting files owned by the user. Each task is accompanied by a code snippet that demonstrates the implementation of the script. The scripts are designed to handle user input and provide appropriate feedback for each operation.

Uploaded by

Akshat Sahu
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)
7 views9 pages

Assignment 6 U22CS040

The document contains a series of shell scripts for various tasks including deleting lines from files, creating directories, checking file existence, reading file contents, appending data to files, removing duplicate lines, and counting files owned by the user. Each task is accompanied by a code snippet that demonstrates the implementation of the script. The scripts are designed to handle user input and provide appropriate feedback for each operation.

Uploaded by

Akshat Sahu
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/ 9

Name : Sangam Birla

Admi. No. : U22CS040


Assignment 06

1. Write a shell script that deletes all lines containing a


specified word in one or more files supplied as
arguments to it.

#CODE
if [ $# -eq 0 ]; then
echo "Usage: $0 word file1"
exit 1
fi

word="$1"
shift

for file in "$@"; do


if [ ! -f "$file" ]; then
echo "$file does not exist."
continue
fi

sed -i "/$word/d" "$file"

echo "Deleted lines containing '$word' from $file."


done

#FILE
#OUTPUT

#FILE

2. Write a shell script that Create a Directory after


Confirming Existence.

#CODE
if [ $# -eq 0 ]; then
echo "Usage: $0 directory_path"
exit 1
fi
directory="$1"

if [ -d "$directory" ]; then
echo "Directory '$directory' already exists."
else
echo "Directory '$directory' does not exists."
mkdir -p "$directory"
echo "Directory '$directory' created successfully."
fi

#OUTPUT

3. Write a shell script that check the existence of File.

#CODE
if [ $# -eq 0 ]; then
echo "Usage: $0 file_path"
exit 1
fi

file_path="$1"

if [ -f "$file_path" ]; then
echo "File '$file_path' exists."
else
echo "File '$file_path' does not exist."
fi

#OUTPUT

4. Write a shell script that Reading File. Create a file


named ide.txt with the contents given below
a. Visual Studio Code
b. Code::Blocks
c. Eclipse

#CODE
file_path="ide.txt"

if [ ! -f "$file_path" ]; then
echo "File '$file_path' does not exist."
exit 1
fi

echo "Contents of $file_path:"


while IFS= read -r line; do
echo "$line"
done < "$file_path"

#FILE

#OUTPUT

5. Write a shell script that take filenames as input and


append data to any file on your filesystem and delete
the duplicate file.

#CODE
append_data() {
local file="$1"
local data="$2"

if [ ! -f "$file" ]; then
echo "File '$file' does not exist."
return 1
fi

echo "$data" >> "$file"


echo "Data appended to '$file'."
}
delete_file() {
local file="$1"

if [ ! -f "$file" ]; then
echo "File '$file' does not exist."
return 1
fi

rm "$file"
echo "File '$file' deleted."
}

if [ $# -eq 0 ]; then
echo "Usage: $0 file1 [file2 ...] data"
exit 1
fi

data="${@: -1}"

for ((i=1; i<=$#-1; i++)); do


filename="${!i}"

append_data "$filename" "$data"

delete_file "$filename"
done

#FILES

#OUTPUT
#FILES

6. Write a shell script that remove duplicated lines from a


file.

#CODE
if [ $# -eq 0 ]; then
echo "Usage: $0 file_path"
exit 1
fi

file_path="$1"

if [ ! -f "$file_path" ]; then
echo "File '$file_path' does not exist."
exit 1
fi

awk '!seen[$0]++' "$file_path" > "$file_path.tmp"


mv "$file_path.tmp" "$file_path"

echo "Duplicate lines removed from '$file_path'."


#FILE

#OUTPUT

#FILE

7. Write a shell script that Counts the only files that is


own by you.

#CODE
current_user=$(whoami)
file_count=$(find . -maxdepth 1 -type f -user "$current_user" | wc -l)

echo "Number of files owned by $current_user: $file_count"

#OUTPUT

You might also like