Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

UNIX \"Desbian System\" commands please and thank you very much! Create a script

ID: 3831332 • Letter: U

Question

UNIX "Desbian System" commands please and thank you very much!

Create a script rotateLog.bash with the following functionality:

a. (2 points) The script should accept a directory and a filename from the command line

b. (4 points) If the file or directory doesn't already exist, create it

c.  (5 points) Rotate the file using the following procedure (note: before copying make sure what you are going to copy exists):

    1. Copy filename.2 to filename.3

    2. Copy filename.1 to filename.2

    3. Copy filename to filename.1

    4. rm filename

    5. touch filename

Explanation / Answer

So you wish to rotate the file specified by "filename" inside the given directory. Comment if you need anything.. Please find the script below

rotateLog.bash

#!/bin/bash

filename=""
dir=""

#Read directory name
echo -n "Enter the directory name : "
read dir

#Read file name
echo -n "Enter the file name : "
read filename

#Create directory if not present
if [ ! -e "$dir" ];then
mkdir "$dir"
fi

#Create file if not present
if [ ! -e "$filename" ];then
touch "$filename"
fi


#Rotating the files

if [ -e "${filename}.2" ];then
cp "${filename}.2" "${filename}.3"
fi

if [ -e "${filename}.1" ];then
cp "${filename}.1" "${filename}.2"
fi

if [ -e "$filename" ];then
cp "$filename" "${filename}.1"
fi

if [ -e "$filename" ];then
rm -f "$filename"
fi

touch "$filename"