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

Write a script that uses a loop to generate a basic project directory tree (usin

ID: 3848247 • Letter: W

Question

Write a script that uses a loop to generate a basic project directory tree (using the standard described in the first lecture, or one of your own devising). (5 pts)

Write a script that uses a loop to copy HTML and CGI files from a project directory to a web server directory. (Assume the project is in ~/project, with html and cgi sub-directories, with no sub-directories under those; assume a destination of ~/public_html.) (5 pts)

Modify the script in the previous problem so that it only copies files if the source file (the original) is newer than the destination file (the copy). (Hint: look at the options for the test command.) (10 pts)

Extend the script in the previous problems so that an environment variable can be used to control which copying behavior ('update only' versus 'copy all') is used. (That is, export COPY_ALL=1 will copy all files regardless of file age; unset COPY_ALL will result in the "copy only if changed" behavior added in the previous problem.) (10 pts)

UNIX

Explanation / Answer

Write a script that uses a loop to generate a basic project directory tree (using the standard described in the first lecture, or one of your own devising). (5 pts)
Ans)

I dont have the project directory tree as mentioned in the question. So i have picked the next question directory structure as sample. But if you can share the project directory or the exact requirements. I can write the new code and share to you. Please feel free to revert on the same.

Code:
#!/bin/bash

# script to generate project directory
# script name: generateDir.sh

mkdir ~/project
mkdir ~/project/html ~/project/cgi
mkdir ~/public_html

Execution and output:
Unix Terminal> ./generateDir.sh
Unix Terminal> cd ~

Unix Terminal> ls -lthr project public_html/
public_html/:

project:
total 0
drwxr-xr-x 2 krishna 1896053708    68B Jun 12 07:23 html
drwxr-xr-x 2 krishna 1896053708    68B Jun 12 07:23 cgi
Unix Terminal>


Write a script that uses a loop to copy HTML and CGI files from a project directory to a web server directory. (Assume the project is in ~/project, with html and cgi sub-directories, with no sub-directories under those; assume a destination of ~/public_html.) (5 pts)
Ans)

Code:
#!/bin/bash

# copy files from html and cgi to public_html directory

# Iterating through each file in html directories
echo "copying files from ~/project/html to ~/public_html"
for file in `ls ~/project/html`
do
   # copying each file in html to webserver directory
   cp ~/project/html/$file ~/public_html
done


# Iterating through each file in cgi directories
echo "copying files from ~/project/cgi to ~/public_html"
for file in `ls ~/project/cgi/`
do
        # copying each file in cgi to webserver directory
        cp ~/project/cgi/$file ~/public_html
done


Execution and output:
Below is the list of files html and cgi directory has:

Unix Terminal> ls -tlrh
total 0
drwxr-xr-x 5 krishna 1896053708   170B Jun 12 07:25 html
drwxr-xr-x 4 krishna 1896053708   136B Jun 12 07:25 cgi
Unix Terminal> ls -R
cgi   html

./cgi:
cgi1   cgi2

./html:
html1   html2   html3

Initially public_html directory does not have files in it
Unix Terminal> ls -R public_html
Unix Terminal>

Unix Terminal> ./copy.sh
copying files from ~/project/html to ~/public_html
copying files from ~/project/cgi to ~/public_html
Unix Terminal>

Unix Terminal> ls -R public_html/
cgi1   cgi2   html1   html2   html3

Modify the script in the previous problem so that it only copies files if the source file (the original) is newer than the destination file (the copy). (Hint: look at the options for the test command.) (10 pts)
Ans)
Code:

#!/bin/bash

# program to copy only if the source file (the original) is newer than the destination file
# script name: copy_1.sh

# Iterating through each file in html directories
echo "copying files from ~/project/html to ~/public_html"
for file in `ls ~/project/html`
do
        # copying each file in html to webserver directory
   if [ ~/project/html/$file -nt ~/public_html/$file ]
   then
       echo "new file in source ~/project/html/$file detected. copying..."
           cp ~/project/html/$file ~/public_html
   else
       echo "the file ~/project/html/$file is not newer than ~/public_html/$file"
   fi
done


# Iterating through each file in cgi directories
echo "copying files from ~/project/cgi to ~/public_html"
for file in `ls ~/project/cgi/`
do
        # copying each file in cgi to webserver directory
   if [ ~/project/cgi/$file -nt ~/public_html/$file ]
   then
       echo "new file in source ~/project/cgi/$file detected. copying..."
           cp ~/project/cgi/$file ~/public_html
   else
       echo "the file ~/project/cgi/$file is not newer than ~/public_html/$file"
   fi
done


Execution and output:
To test the code i have modified the file ~/project/cgi/cgi1. You can see script has detected that file and copied it.
Unix Terminal> ./copy_1.sh
copying files from ~/project/html to ~/public_html
the file ~/project/html/html1 is not newer than ~/public_html/html1
the file ~/project/html/html2 is not newer than ~/public_html/html2
the file ~/project/html/html3 is not newer than ~/public_html/html3
copying files from ~/project/cgi to ~/public_html
new file in source ~/project/cgi/cgi1 detected. copying...
the file ~/project/cgi/cgi2 is not newer than ~/public_html/cgi2
Unix Terminal> ls -R ~/public_html
cgi1   cgi2   html1   html2   html3

Unix Terminal> ls -tlhr ~/public_html
total 8
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:34 html3
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:34 html2
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:34 cgi2
-rw-r--r-- 1 krishna 1896053708    12B Jun 12 07:45 cgi1
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:45 html1
Unix Terminal>


Extend the script in the previous problems so that an environment variable can be used to control which copying behavior ('update only' versus 'copy all') is used. (That is, export COPY_ALL=1 will copy all files regardless of file age; unset COPY_ALL will result in the "copy only if changed" behavior added in the previous problem.) (10 pts)
Ans)
Code:

#!/bin/bash

# script name: copy_2.sh
# program to copy all files if COPY_ALL environment variable set to 1 otherwise only copy changed files

# Iterating through each file in html directories
echo "copying files from ~/project/html to ~/public_html"
for file in `ls ~/project/html`
do
   # copying each file in html to webserver directory
   # copying all files irrespective of age
   if [ $COPY_ALL -eq 1 ]
   then
       cp ~/project/html/$file ~/public_html
   else
       if [ ~/project/html/$file -nt ~/public_html/$file ]
       then
           echo "new file in source ~/project/html/$file detected. copying..."
           cp ~/project/html/$file ~/public_html
       else
           echo "the file ~/project/html/$file is not newer than ~/public_html/$file"
       fi
   fi
done


# Iterating through each file in cgi directories
echo "copying files from ~/project/cgi to ~/public_html"
for file in `ls ~/project/cgi/`
do
        # copying each file in cgi to webserver directory
   # copying all files irrespective of age
   if [ $COPY_ALL -eq 1 ]
   then
       cp ~/project/cgi/$file ~/public_html
   else
       if [ ~/project/cgi/$file -nt ~/public_html/$file ]
       then
           echo "new file in source ~/project/cgi/$file detected. copying..."
               cp ~/project/cgi/$file ~/public_html
       else
           echo "the file ~/project/cgi/$file is not newer than ~/public_html/$file"
       fi
   fi
done


Execution and output:
Initially when environment variable is set to 1
Unix Terminal> export COPY_ALL=1

Unix Terminal> env|grep COPY_ALL
COPY_ALL=1

Unix Terminal> ls -tlrh cgi html
cgi:
total 8
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:25 cgi2
-rw-r--r-- 1 krishna 1896053708    12B Jun 12 07:44 cgi1

html:
total 8
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:25 html3
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:25 html2
-rw-r--r-- 1 krishna 1896053708    13B Jun 12 07:43 html1
Unix Terminal>
Unix Terminal> cd ..
Unix Terminal> ls -tlrh public_html/
total 8
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:34 html3
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:34 html2
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:34 cgi2
-rw-r--r-- 1 krishna 1896053708    12B Jun 12 07:45 cgi1
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 07:45 html1

We could see all the files are copied from cgi and html to public_html directory irrespective of age. See the date change in below filenames
Unix Terminal> echo $COPY_ALL
1
Unix Terminal> ./copy_2.sh
copying files from ~/project/html to ~/public_html
copying files from ~/project/cgi to ~/public_html
Unix Terminal> ls -tlrh ~/public_html
total 16
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 08:08 html3
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 08:08 html2
-rw-r--r-- 1 krishna 1896053708    13B Jun 12 08:08 html1
-rw-r--r-- 1 krishna 1896053708     0B Jun 12 08:08 cgi2
-rw-r--r-- 1 krishna 1896053708    12B Jun 12 08:08 cgi1
Unix Terminal> date
Mon Jun 12 08:08:17 IST 2017
Unix Terminal>

After setting environment variable to 0 we can see files are trying to copy based on age.
Unix Terminal> COPY_ALL=0
Unix Terminal> env|grep COPY_ALL
COPY_ALL=0
Unix Terminal>
Unix Terminal> echo $COPY_ALL
0

Unix Terminal> ./copy_2.sh
copying files from ~/project/html to ~/public_html
the file ~/project/html/html1 is not newer than ~/public_html/html1
the file ~/project/html/html2 is not newer than ~/public_html/html2
the file ~/project/html/html3 is not newer than ~/public_html/html3
copying files from ~/project/cgi to ~/public_html
the file ~/project/cgi/cgi1 is not newer than ~/public_html/cgi1
the file ~/project/cgi/cgi2 is not newer than ~/public_html/cgi2
Unix Terminal>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote