Menu

[Solved] Objective Write C Shell Script Copies Files Java Class Home Directory New One Analyze New Q37155881

Objective : Write a C Shellscript which copies all files(*.java and *.class) from your homedirectory to a new one, and Analyze thenew directory information such as number of files, userpermissions, and disk usage.

Sample Output:

                            

                     << CS Directory Analysis>>      Date:  

============================================================

  1. Current Directory: /home/tomss
  2. New Directory Created : /home/tomss/pgm01
  3. File information
  1. Total Number of files : 22 files
  2. Directory files:   0 files
  3. Plain text files:   10 files
  4. File have read permissions: 3 files
  5. File have write permissions: 4 files
  6. File have execute permissions : 2files
  7. File have length of zero : 0 files
  8. Others : 0 files
  1. Disk usage : 50 Kb
  2. Biggest 5 files in directory :

14.0 KB ./folder1/folder2/file

12.0 KB ./folder1/folder2/file2etc

============================================================

                                                                                      Reported by : <user>    

Run C shell :

-bash-4.2$ csh

[tomss@csx8 ~]$ source .login

%DirAnalyze.sh   pgm01

                                                                

Notes.

  1. Change to C shell :   $csh  

                                   %source .login

  1. Write a C shell script( csh ) named“DirAnalyze.sh”

The script need to start with a line#! /bin/csh

  1. DirAnalyze.sh  
  • put your name, section name and simple description for theassignment
  • Print Title and date at thesame line use echo command andcut command

     << CS2351 Directory Analysis>>      Date: Tue Oct 29, 2018

      Eg.  

      Wed Oct30 17:48:15 CDT 2018 take –> Wed Oct 30, 2018

  1. Display current directory

Use the pwdcommand to make sure your home directory

  1. Create New Directory and copyfiles

Create new directory with themkdir command, directory name should bethe first argument $1.

* Command lineargument are in special shell variables 0, 1, 2, 3 ,4…etc.

   $0 is the name of thescript itself, $1 the first, $2 the second ..etc.

DirAnalyze.sh  pgm01  

——————- ———-

   $0                       $1

* Copyfiles

  1. copy *.java file in your $HOME directory to pgm01
  2. copy *.class files in your $HOME directory to pgm01

cp $currentDir/*.java  $newDir

cp $currentDir/*.class  $newDir

  1. change directory to pgm01
  1. File information

Use foreach loop and if statement

foreach file(argument list)

  

   if( -d $file) then

       @ x ++

else if( -f$file    ) then

    @ y ++

else if( -r$file   ) then

       

      ……

   else

      ….

  endif

   end

       

# file inquiry Operator

-d File is a Directory

-e File Exists

-f   File is a plainfile

-o user owns file

-r user has read permission

-x user has execute permission

-z file has length of Zero

  1. Disk Usage

Usedu command

    

  1. Find top 5 big files -print top 5 big file namesand their size in decreasing order in human readable format(Hint :use “du” and “sort” and “head” commands together to achievethis)
  1. Run C shell

          bash-4.2$ csh

         [jongyk@csx8 ~]$ source .login

         %DirAnalyze.sh pgm01

Examples

vi DirAnalysis.sh

#! /bin/csh

      2 #Author : Jongyeop Kim

      3 #Section: 401

      4 #Description : put yourdescription

      5

      6 echo -n “<< CS2351 GradeReport >> Date:” ;

      7 echo `date |cut -c1-20`”n”

      8

      9 ## set variables

     10 set dirCount=0

     11 set plainCount=0

     12 set exePermission=0

     13 set zeroLength=0

     14

     15 set currentDir=`pwd`

     16 set newDir=$1

     17

     18 echo “A. Current Directory :”`pwd`

     19 mkdir $newDir

     20

     21 echo “B. New Directory Created “`pwd`”/”$newDir

     22 cp $currentDir/*.java $newDir

     23 cp $currentDir/*.class $newDir

     24

     25 ## Change directory to pgm02

     26

     27 cd $newDir

     28

     29 ##count number file in your newDirectory

     30

     31 foreach file (*)

     32 ## echo $file

     33   if( -x $file) then

     34      echo”directory”

     35      @dirCount ++

     36   else if( -f $file )then

     37     echo”plainCount”

     38     @ plainCount++

     39   else

     40     echo”zeroLength”

     41     @zeroLength=0

     42   endif

     43 end

     44

     45 echo “File is a directory”$dirCount

     46 echo “File is a plain file”$plainCount

     47 echo “File have length of zero”$zeroLength

Expert Answer


Answer to Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, an… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *