0% found this document useful (0 votes)
266 views

Name-Muskan Agarwal Assignment-1 Codingclub

The document contains solutions to coding assignment questions involving shell scripting tasks like moving files to directories based on their name, appending current date to log file names, archiving log files older than 7 days, analyzing web server log files, checking if a folder exists and setting environment variables. Various shell scripting concepts like loops, conditions, parsing file names, executing commands and checking statuses are demonstrated.

Uploaded by

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

Name-Muskan Agarwal Assignment-1 Codingclub

The document contains solutions to coding assignment questions involving shell scripting tasks like moving files to directories based on their name, appending current date to log file names, archiving log files older than 7 days, analyzing web server log files, checking if a folder exists and setting environment variables. Various shell scripting concepts like loops, conditions, parsing file names, executing commands and checking statuses are demonstrated.

Uploaded by

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

Name-Muskan Agarwal

Assignment-1
Codingclub
Question-2

i) for file in `ls *.txt`;


do
folderName=`echo $file |awk -F. '{print $1}'`;
if [ -d $folderName ];
then
echo Removing folder $folderName as it already exists
rm -R $folderName;
fi
mkdir $folderName;
echo Moving $file to $folderName
mv $file $folderName;
echo moved $file to $folderName;
done

Moving abc.txt to abc


moved abc.txt to abc
Moving def.txt to def
moved def.txt to def
Moving ghi.txt to ghi
moved ghi.txt to ghi
Moving jkl.txt to jkl
moved jkl.txt to jkl
Question-3

#!/bin/bash

for files in `ls *.log.1`


do
status=$?
if [ $status -ne 0 ]
then
echo "No files with extension log.1"
fi
fileName=`echo $files | awk -F. '{print $1}'`
echo "Renaming file $files"
TodayDate=`date +%d%m%Y`
newFile="$fileName-$TodayDate.log.1"
mv $files $newFile
done

Muskan@DESKTOP-EJUM3U4 MINGW64 ~/Desktop/LinuxLearning/Assignment 1/Temp


$ ./AppendCurrentDateToLogFiles.sh
Renaming file abc.log.1
Renaming file def.log.1
Renaming file ghi.log.1
Renaming file jkl.log.1
Renaming file lmn.log.1

Muskan@DESKTOP-EJUM3U4 MINGW64 ~/Desktop/LinuxLearning/Assignment 1/Temp


$ ls
abc-28062020.log.1 def-28062020.log.1 jkl-28062020.log.1
AppendCurrentDateToLogFiles.sh* ghi-28062020.log.1 lmn-28062020.log.1
Question-4

#!/bin/bash

for files in `find /var/log -mtime +7 -type f`


do
cp $files /var/log/backup/
echo "Moving $files to backup folder"
done

Muskan@DESKTOP-EJUM3U4 MINGW64 ~/Desktop/LinuxLearning/Assignment 1


$ ./ArchiveFilesModified7daysAgo.sh
Moving ../linux-content/.git/config to backup folder
Moving ../linux-content/.git/description to backup folder
Moving ../linux-content/.git/HEAD to backup folder
Moving ../linux-content/.git/hooks/applypatch-msg.sample to backup folder
Moving ../linux-content/.git/hooks/commit-msg.sample to backup folder
Moving ../linux-content/.git/hooks/fsmonitor-watchman.sample to backup folder
Moving ../linux-content/.git/hooks/post-update.sample to backup folder
Moving ../linux-content/.git/hooks/pre-applypatch.sample to backup folder
Moving ../linux-content/.git/hooks/pre-commit.sample to backup folder
Moving ../linux-content/.git/hooks/pre-push.sample to backup folder
Moving ../linux-content/.git/hooks/pre-rebase.sample to backup folder
Moving ../linux-content/.git/hooks/pre-receive.sample to backup folder
Moving ../linux-content/.git/hooks/prepare-commit-msg.sample to backup folder
Moving ../linux-content/.git/hooks/update.sample to backup folder
Moving ../linux-content/.git/index to backup folder
Moving ../linux-content/.git/info/exclude to backup folder
Moving ../linux-content/.git/logs/HEAD to backup folder
Question-5

Muskan@DESKTOP-EJUM3U4 MINGW64 ~/Desktop/LinuxLearning/Assignment 1


$ egrep -o 'https?://[^\)"]+' access.log | sort | uniq -c | sort -nr |head -4

1475 https://fundoopush-dev.bridgelabz.com/login
1141 https://fundoopush-dev.bridgelabz.com/dashboard/article
176 https://fundoopush-dev.bridgelabz.com/add-post
74 http://www.google.com/bot.html

Question-9

read -p "Enter the name of folder you want to make " folderName
if [ -d $folderName ]
then
echo "Folder already exists"
else
mkdir $folderName
echo "Making directory $folderName"
fi
Muskan@DESKTOP-EJUM3U4 MINGW64 ~/Desktop/LinuxLearning/Assignment 1
$./FolderExistsOrNot.shell
Enter the name of folder you want to make Temp
Making directory Temp

Question-10
list=`ls`
status=$?
if [ $status -eq 0 ]
then
echo "ls command executed successfully"
else
echo "ls command not executed successfully"
fi
list=`hello`
status=$?
if [ $status -eq 0 ]
then
echo "hello command executed successfully"
else
echo "hello command not executed successfully"
fi
___________________________________________________________________________
Muskan@DESKTOP-EJUM3U4 MINGW64 ~/Desktop/LinuxLearning/Assignment 1
$ ./checkExecutionStatus.shell
ls command executed successfully
./checkExecutionStatus.shell: line 9: hello: command not found
hello command not executed successfully

Question-11

#!/bin/bash

case "$usersecret" in
"") echo "setting environment variable to dH34xJaa23"
usersecret='dH34xJaa23'
;;
*) echo "Environment variable already set"
;;
esac

Muskan@DESKTOP-EJUM3U4 MINGW64 ~/Desktop/LinuxLearning/Assignment 1


$ ./EnvironmentVariable.sh
setting environment variable to dH34xJaa23
Question-13

Question-14

i) cat data.csv | awk '{if($4>10000) print $2,$7}'

EmployeeName TotalPay
NATHANIEL 567595
GARY 538909

You might also like