Write a bash script that takes three arguments (d1, d2, d3) on the command line,
ID: 3762001 • Letter: W
Question
Write a bash script that takes three arguments (d1, d2, d3) on the command line, then compares two directories, 40 1 d1 and d2, and copies every file that is present in d2 but not in d1, into directory d3. Hint 1: Use the following strategy. Enumerate the contents of d2 using a for loop. For each file discovered in d2, enumerate the contents of d1 using another loop, looking for a matching file name. If no match is found, copy the file to d3. Hint 2: To determine that there is no matching file name in d1, either (1) test for file existence using the -f operator or (2) set a flag variable to 0 before enumerating the contents of d1; set it to 1 if there is a matching name found; then, if, after enumerating all files in d1, the flag is still 0, no match was found.
Explanation / Answer
unction Get-Properties($Object, $MaxLevels="5", $PathName = "`$_", $Level=0) { if ($Level -eq 0) { $oldErrorPreference = $ErrorActionPreference $ErrorActionPreference = "SilentlyContinue" } #Initialize an array to store properties $props = @() # Get all properties of this level $rootProps = $Object | Get-Member -ErrorAction SilentlyContinue | Where-Object { $_.MemberType -match "Property"} # Add all properties from this level to the array. $rootProps | ForEach-Object { $props += "$PathName.$($_.Name)" } # Make sure we're not exceeding the MaxLevels if ($Level -lt $MaxLevels) { # We don't care about the sub-properties of the following types: $typesToExclude = "System.Boolean", "System.String", "System.Int32", "System.Char" #Loop through the root properties $props += $rootProps | ForEach-Object { #Base name of property $propName = $_.Name; #Object to process $obj = $($Object.$propName) # Get the type, and only recurse into it if it is not one of our excluded types $type = ($obj.GetType()).ToString() # Only recurse if it's not of a type in our list if (!($typesToExclude.Contains($type) ) ) { #Path to property $childPathName = "$PathName.$propName" # Make sure it's not null, then recurse, incrementing $Level if ($obj -ne $null) { Get-Properties -Object $obj -PathName $childPathName -Level ($Level + 1) -MaxLevels $MaxLevels } } } } if ($Level -eq 0) {$ErrorActionPreference = $oldErrorPreference} $props }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.