Shell script to duplicate directory, lock one, and

2019-08-01 04:21发布

问题:

I'm a reporter who works with data sets. Every time I receive a new data set, I do two things immediately: Duplicate the file(s) and lock the originals.

I want to automate that process. By altering a shell script I use to create nested folders for reporting projects and stringing it together with a bunch of commands, I was able to achieve the basic functionality, but it is messy. I would be grateful for your help in streamlining.

Here's what I'm doing now (bear with me and I promise to be as clear as possible):

I download the data to a Desktop folder called "DataInbox" where I also have the following shell script, saved as newdata.sh:

    #!/bin/bash

if [[ -z "${1}" ]]; then
  die "FolderName Required"
fi


/bin/mkdir -p ~/Desktop/DataFarm/$1/{ProtectedOrig,Data}

echo -n "---Data Folder Setup

---Data Introduction

---Data Audit/Manipulation

---Data Queries" > ~/Desktop/DataFarm/$1/Data/DataJournal.txt

By executing bash newdata.sh DirectoryName I create a directory with the subfolders "Data" and "ProtectedOrig" and a text file called "DataJournal.txt" with four headings built in.

Inside the "DataInbox" directory I run this mess, only strung together with &&:

$ ditto NewData/  NewDataCopy

$ bash newdata.sh DirectoryName

$ mv NewData/ /Users/JSG/Desktop/DataFarm/DirectoryName/ProtectedOrig/NewData

$ mv NewDataCopy/ /Users/JSG/Desktop/DataFarm/DirectoryName/Data/NewDataCopy

$ chflags -R uchg /Users/JSG/Desktop/DataFarm/DirectoryName/ProtectedOrig/

Like I said, I'd like to streamline this. In fact, what I'd really like to do is turn the whole thing into a shell script, and I'm struggling to make it work. Thank you in advance from an eager novice.

回答1:

Not sure if I'm misunderstanding your question, but you want to merge all that into one script like this?

#!/bin/bash

if [[ -z "${1}" ]]; then
  die "FolderName Required"
fi

newDirName="DirectoryName"
newBaseDir="~/Desktop/DataFarm/$1/"
/bin/mkdir -p $newBaseDir/{ProtectedOrig,Data}

echo -n "---Data Folder Setup

---Data Introduction

---Data Audit/Manipulation

---Data Queries" > $newBaseDir/Data/DataJournal.txt

ditto NewData/ NewDataCopy
newdata.sh $newDirName
mv NewData/ $newBaseDir/ProtectedOrig/NewData
mv NewDataCopy/ $newBaseDir/Data/NewDataCopy
chflags -R uchg $newBaseDir/ProtectedOrig/