Linux Script Question 2

I want to confirm I have the script in order, adding some features and listing all the functionality.  Let me know if there is a better process to do this.  Here is the requirement:

 Automate the following:
·         Directory and subdirectory creation
·         Files in each of the directories
·         Symbolic links from 2 subdirectories to their parent directories
·         Include branching to only execute the directory, file and link commands only if the directory or file does not already exist.
·         Set up an error log file with commands to route the errors messages into it.
·         Sett appropriate file permissions for the directories and files:
·         Directories
o    the file owner can read, modify and access the scenario company directories,
o    the group members can read and access the scenario company directories,
o    everyone else has no access.
·         Files
o    The owner can read and modify the file,
o    the group members can only read the file
o    everyone else has no permissions at all.

Below is my script thus far:

13 umask 027
      14 test -d /ARI && echo "ARI already exists" || mkdir -v /ARI
      15 #
      16 #
      17 # Create Next level of Directories
      18 test –d /ARI/SALES && echo “/ARI/SALES already exists” || mkdir –v /ARI/SALES
      19 test –d  /ARI/IT && echo “/ARI/IT already exists” || mkdir –v /ARI/IT
      20 test –d /ARI/MARKETING && echo “/ARI/MARKETING already exists” || mkdir –v /ARI/MARKETING
      21 test –d /ARI/FINANCE && echo “/ARI/FINANCE already exists” || mkdir –v /ARI/FINANCE
      22 #
      23 #
      24 # The next lines create the subfolders under the second level directories
      25 mkdir –v /ARI/SALES/REG_1
      26 mkdir –v /ARI/SALES/REG_2
      27 mkdir –v /ARI/SALES/REG_3
      28 mkdir –v /ARI/IT/OPS
      29 mkdir –v /ARI/IT/DEV
      30 mkdir –v /ARI/IT/RandD
      31 mkdir –v /ARI/MARKETING/FT
      32 mkdir –v /ARI/MARKETING/PT
      33 mkdir –v /ARI/MARKETING/CNT
      34 mkdir –v /ARI/FINANCE/AP
      35 mkdir –v /ARI/FINANCE/AR
      36 mkdir –v /ARI/FINANCE/ADT
      37 #
      38 #
      39 #Create symbolic links from OPS and PT subdirectories to their parent directory
      40 ln-s /ARI/IT /IT/OPS
      41 ln-s /ARI/MARKETING /MARKETING/PT
      42 umask 037
      43 #
      44 touch /ARI/SALES/REG_1/SALESFILE
      45 touch /ARI/SALES/REG_2/SALESFILE1
      46 touch /ARI/SALES/REG_3/SALESFILE2
      47 touch /ARI/IT/OPS/ITFILE1
      48 touch /ARI/IT/DEV/ITFILE2
      49 touch /ARI/IT/RandD/ITFILE3
      50 touch /ARI/MARKETING/FT/FILE1
      51 touch /ARI/MARKETING/PT/FILE2
      52 touch /ARI/MARKETING/CNT/FILE3
      53 touch /ARI/FINANCE/AP/FILE1
      54 touch /ARI/FINANCE/AR/FILE2
      55 touch /ARI/FINANCE/ADT/FILE3
      56 #
      57 done
      58 done
Jack_son_Asked:
Who is Participating?
 
woolmilkporcConnect With a Mentor Commented:
Perfect so far. Just one point:

You added ">&2" in the right places, but you forgot the general redirection of stderr to a file.

Add somewhere before line 14 (line 10 seems a good candidate):

exec 2>/path/to/error.log

where "/path/to/error.log" means filename and path to an error log file of your choice.

That's it.

wmp
0
 
woolmilkporcConnect With a Mentor Commented:
So far so good, except for the links and the second umask.

"umask 037" for files will result in permissions "640" which is "rw-r-----" (not "630", because you can't create executable files by setting umask, you must use "chmod") .
Although the result is indeed what you desire, just for accurateness the second umask should be "026", according to your specifications.


>> Symbolic links from 2 subdirectories to their parent directories <<

What do you mean with "... to their parent ..."

I'm not sure what you're trying to achieve.

Your command

ln-s /ARI/IT /IT/OPS

will create a link "/IT/OPS" pointing to "/ARI/IT"

and your command

ln-s /ARI/MARKETING /MARKETING/PT

will create a link "/MARKETING/PT" pointing to "/ARI/MARKETING"

So let's say you issue

cd /IT/OPS

you will be placed into /ARI/IT.

Is that what you desire? I don't think so.

Perhaps you mean creating a link "/ARI/OPS" pointing to "/ARI/IT/OPS"
and a link "/ARI/PT" pointing to "/ARI/MARKETING/PT"?

This would have to be done like this:

ln -s /ARI/IT/OPS /ARI/OPS
ln -s /ARI/MARKETING/PT /ARI/PT

.. but that's just a guess. If I'm wrong please explain your goal a bit more precisely.


As for the error log file:

Add ">&2" after every statement which is supposed to display an error message, like

14 test -d /ARI && echo "ARI already exists" >&2 || mkdir -v /ARI

Do this for all relevant lines (18-21), then add this following line to the script, near its top in a place before any error message is issued:

exec 2>/path/to/error.log

This will redirect all messages having ">&2" specified to that file, and also possible (e.g. syntax) error messages issued by the shell.


By the way, line 32 is a duplicate of line 31.


The two "done" statements at the end seem to terminate two loops whose initializations (for/while .... do ...) you didn't post, right?

I think you're aware that "done" statements are not allowed to exist without their opening "do" counterparts.
0
 
Jack_son_Author Commented:
Great, thanks for the input, I updated the script with your input:.  I think I got most of the points to roll out to Production.


      10 #
      11 # This will create the home ARI directory
      12 #
      13 umask 027
      14 test -d /ARI && echo "ARI already exists">&2 || mkdir -v /ARI
      15 #
      16 #
      17 # Create Next level of Directories
      18 test –d /ARI/SALES && echo “/ARI/SALES already exists”>&2 || mkdir –v /ARI/SALES
      19 test –d  /ARI/IT && echo “/ARI/IT already exists”>&2 || mkdir –v /ARI/IT
      20 test –d /ARI/MARKETING && echo “/ARI/MARKETING already exists”>&2 || mkdir –v /ARI/MARKETING
      21 test –d /ARI/FINANCE && echo “/ARI/FINANCE already exists”>&2 || mkdir –v /ARI/FINANCE
      22 #
      23 #
      24 # The next lines create the subfolders under the second level directories
      25 mkdir –v /ARI/SALES/REG_1
      26 mkdir –v /ARI/SALES/REG_2
      27 mkdir –v /ARI/SALES/REG_3
      28 mkdir –v /ARI/IT/OPS
      29 mkdir –v /ARI/IT/DEV
      30 mkdir –v /ARI/IT/RandD
      31 mkdir –v /ARI/MARKETING/FT
      32 mkdir –v /ARI/MARKETING/PT
      33 mkdir –v /ARI/MARKETING/CNT
      34 mkdir –v /ARI/FINANCE/AP
      35 mkdir –v /ARI/FINANCE/AR
      36 mkdir –v /ARI/FINANCE/ADT
      37 #
      38 #
      39 #Create symbolic links from OPS and PT subdirectories to their parent directory
      40 ln-s /ARI/IT/OPS /ARI/OPS
      41 ln-s /ARI/MARKETING/PT /ARI/PT
      42 umask 026
      43 #
      44 touch /ARI/SALES/REG_1/SALESFILE
      45 touch /ARI/SALES/REG_2/SALESFILE1
      46 touch /ARI/SALES/REG_3/SALESFILE2
      47 touch /ARI/IT/OPS/ITFILE1
      48 touch /ARI/IT/DEV/ITFILE2
      49 touch /ARI/IT/RandD/ITFILE3
      50 touch /ARI/MARKETING/FT/FILE1
      51 touch /ARI/MARKETING/PT/FILE2
      52 touch /ARI/MARKETING/CNT/FILE3
      53 touch /ARI/FINANCE/AP/FILE1
      54 touch /ARI/FINANCE/AR/FILE2
      55 touch /ARI/FINANCE/ADT/FILE3
      56 #
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.