Automating Linux Script

  1. The problem statement, all variables and given/known data:
    I want to automate the creation or processing of the following:
  • Directory and subdirectory creation for your scenario company
  • Files in each of the directories
  • Symbolic links from 2 subdirectories to their parent directories
  • Setting appropriate file permissions for the directories and files:
    [list]
  • Directories
    [list]
  • the file owner can read, modify and access the scenario company directories,
  • the group members can read and access the scenario company directories,
  • everyone else has no access.
    [/list]
  • Files
    [list]
  • The owner can read and modify the file,
  • the group members can only read the file
  • everyone else has no permissions at all.
    [/list]

    [/list]
  • 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.

However, I have only been using Linux for a few weeks now and this is proving to be extremely complicated. I have read tutorials on automating but cannot understand it. This is a school assignment due today by 11:59pm central time. Any assistance would be greatly appreciated!

  1. Relevant commands, code, scripts, algorithms:
    Here is the script that I have completed through my previous assignments, it the base script that they want us to automate.
 \KGS_Home\
  Testing \+
  |- Beta
  |- DLC
  |- IndyGames
  Programming \+
  |- MainStory
  |- SideStory
  |- CharacterStats
  ArtDesign \+
  |- CoverArt
  |- Background
  |- Characters
  Marketing \+
  |- Trailers
  |- Demos
  |- Posters
   
   
  mkdir -v \KGS_Home
  cd KGS_Home
  mkdir \Testing
  mkdir \Programming
  mkdir \ArtDesign
  mkdir \Marketing
  cd Testing
  mkdir \Beta
  mkdir \DLC
  mkdir \IndyGames
  cd ..
  cd Programming
  mkdir \MainStory
  mkdir \SideStory
  mkdir \CharacterStats
  cd ..
  cd ArtDesign
  mkdir \CoverArt
  mkdir \Background
  mkdir \Characters
  cd ..
  cd Marketing
  mkdir \Trailers
  mkdir \Demos
  mkdir \Posters
  cd..
  ln -s \Testing\Beta \Beta
  ln -s \Testing\DLC \DLC
  ln -s \Testing\IndyGames \IndyGames
  ln -s \Programming\MainStory \MainStory
  ln -s \Programming\SideStory \SideStory
  ln -s \Programming\CharacterStats \CharacterStats
  ln -s \ArtDesign\CoverArt \CoverArt
  ln -s \ArtDesign\Background \Background
  ln -s \ArtDesign\Characters \Characters
  ln -s \Marketing\Trailers \Trailers
  ln -s \Marketing\Demos \Demos
  ln -s \Marketing\Posters \Posters
  chmod 750 �R /Testing
  chmod 750 �R /Programming
  chmod 750 �R /ArtDesign
  chmod 750 �R /Marketing
  touch Testing/TestingFile
  touch Programming/ProgrammingFile
  touch ArtDesign/ArtDesignFile
  touch Marketing/MarketingFile
  chmod 740 �R Testing/TestingFile
  chmod 740 �R Programming/ProgrammingFile
  chmod 740 �R ArtDesign/ArtDesignFile
  chmod 740 �R Marketing/MarketingFile
  
  1. The attempts at a solution (include all code and scripts):
    I have been unable to make any attempts because my professor neglected to explain how to automate at all. I have researched the topic but found little to no definitive results.

  2. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Colorado Technical University Online, Colorado, USA, Majid Mohammad, CS126-1303B-04 : Unix Fundamentals

What you want to do is unclear to me...
Are these backslashes artefacts? if not what OS are you using with which shell? For its not very UNIX to me...
What should I understand by:

 \KGS_Home\
  Testing \+
  |- Beta
  |- DLC
  |- IndyGames
  Programming \+
  |- MainStory
  |- SideStory
  |- CharacterStats
  ArtDesign \+
  |- CoverArt
  |- Background
  |- Characters
  Marketing \+
  |- Trailers
  |- Demos
  |- Posters

What do you understand by automating?

What I provided was the contents of a Vi file my instructor wanted me to make. The section you are asking about is simply a showing of by home directory for an imaginary company and its directories and sub-directories. That is simply to show how they connect to each other. What I am trying to automate is the process of creating those directories and sub-directories on various company computers without having to type everything out for each computer.

So we agree looking at what you posted could be used as a basic script to create what you want, optimizing the code as it is is waste of time ( like using mkdir -p ..)
You could maybe save a couple of lines in the chmod process, that is going at a higher level and use -R 755 * and -R 740 [TPAM]*/*File
Put all that in script and make it executable then it is just copying your script to the new computers and executing it or you may look at the command rdist and see if you will not use its facilites...

What would -R 755 * and -R 740 [TPAM]*/* File do exactly?

---------- Post updated at 01:23 PM ---------- Previous update was at 01:22 PM ----------

I just looked into rdist but it looks like that isn't something my instructor intends for us to use

chmod -R 755 * would recursively change every file and directory in the current directory (except anything beginning with a . (dot) in the current directory) to have permissions rwxr-xr-x .

chmod -R 740 [TPAM]*/* would recursively change every file and directory in any directory beginning with "T", "P", "A" or "M" in the current directory (except any file or directory beginning with . (dot) in exactly the directory beginning with "T", "P", "A" or "M" in the current directory) to have a permission of rwxr----- .

$ mkdir Tx Px Ax Mx              

$ touch Tx/.T1 Px/P1 Ax/.Ax Mx/Mx

$ find . -ls
2138774        0 drwxr-xr-x    6 scott            staff                 204 Sep 23 20:48 .
2139783        0 drwxr-xr-x    3 scott            staff                 102 Sep 23 20:48 ./Ax
2139820        0 -rw-r--r--    1 scott            staff                   0 Sep 23 20:48 ./Ax/.Ax
2139784        0 drwxr-xr-x    3 scott            staff                 102 Sep 23 20:48 ./Mx
2139821        0 -rw-r--r--    1 scott            staff                   0 Sep 23 20:48 ./Mx/Mx
2139782        0 drwxr-xr-x    3 scott            staff                 102 Sep 23 20:48 ./Px
2139819        0 -rw-r--r--    1 scott            staff                   0 Sep 23 20:48 ./Px/P1
2139781        0 drwxr-xr-x    3 scott            staff                 102 Sep 23 20:48 ./Tx
2139818        0 -rw-r--r--    1 scott            staff                   0 Sep 23 20:48 ./Tx/.T1

$ chmod -R 740 [TPAM]*/*

$ find . -ls            
2138774        0 drwxr-xr-x    6 scott            staff                 204 Sep 23 20:48 .
2139783        0 drwxr-xr-x    3 scott            staff                 102 Sep 23 20:48 ./Ax
2139820        0 -rw-r--r--    1 scott            staff                   0 Sep 23 20:48 ./Ax/.Ax
2139784        0 drwxr-xr-x    3 scott            staff                 102 Sep 23 20:48 ./Mx
2139821        0 -rwxr-----    1 scott            staff                   0 Sep 23 20:48 ./Mx/Mx
2139782        0 drwxr-xr-x    3 scott            staff                 102 Sep 23 20:48 ./Px
2139819        0 -rwxr-----    1 scott            staff                   0 Sep 23 20:48 ./Px/P1
2139781        0 drwxr-xr-x    3 scott            staff                 102 Sep 23 20:48 ./Tx
2139818        0 -rw-r--r--    1 scott            staff                   0 Sep 23 20:48 ./Tx/.T1

Thank you Scott, I understand that now. But how will I automate everything in my script?

Automation is about:

  • Make a function for everything
  • Minimize functions
  • Use functions/commands to an extend
  • Fetch the users thoughts/choice
  • Use of arguments

Or using abrevihations as everyone else:
KISS
Keep It Stupid & Simple

Or in other words, think what the user NEEDS, not what you think of.
Or give him lesser options, makes it easier to fetch available choices.

Example:

cd $HOME/KGS_HOME
mkdir -p Testing/{Beta,DLC,Indi}
mkdir -p Programming/{MainStory,SideStory,CharacterStats}
...
chmod 755 * -R

hth

EDIT:
Other than that, i (as well) have no idea what you talk about automation...
You list a bunch of folders, where is the automation in that?
What does your script do (other than creating folders)?

EDIT2:
Automation requires BRAIN v2.0 at least.
You (should) KNOW what you want to automate.... we cant help if we dont have enough data to evaluate from...

All I know is that my instructor never covered automating in my course. The course consisted of teaching me what it took to make the code I provided. Next thing I know this assignment came up. The first part of my post states the exact text copied and pasted from my teachers assignment list. I cannot give any more information then I already have because I have only been using Linux for 5 weeks now and I understand barely 5% of this assignment. All I know is that I need to automate the process of creating directories and folders along with soft links and error messages for an imaginary company I have named KGS. I think it is meant to be used to insure that every company computer has the directories set up the same way.

You will have to ask your instructor what he means, then.

I did ask him for more assistance, actually. Three times, to be exact. This first time he provided me with a link that brought me to a page that helped me understand automating slightly but didn't help me understand how to automate this assignment. After I realized that would not be enough for me to work with I contacted him again. This time he told me he already sent me a link and that if that wasn't enough for me then to research it on my own. I had been doing that previously and had by that point already created this thread as well. I then emailed him again stating that this was not enough help because I had called my student adviser for help, researched the topic, created this thread, and tried getting help from 4 of my peers, only one of whom would reply to me. That student was just as stuck as me. So here I am, having filed for an incomplete because I couldn't complete the assignment due to lack of understanding. I'm still trying to understand this automating stuff. I don't know how to set it up to check if the directories and files already exist or how to make error messages appear.

---------- Post updated at 01:31 PM ---------- Previous update was at 01:27 PM ----------

To make it more clear, my instructor told me to file for an incomplete if I couldn't finish my assignment by the due date and that he had done all he could for me and to work with what I have, which is nothing but a thread where no one understands my assignment any better then I do and an assignment that I can't complete. Is there any one who can try to break down automating each part of my script bit by bit? I can't seem to grasp this topic at all and I need some simple descriptions of how this is all supposed to work.

"How to automate a script" depends on what needs to be automated in what way, the same way "how to write a script" depends on what you want to write to accomplish what end; so "automate" is as vague to us as it is to you. There isn't a secret hidden meaning we're intentionally hiding from you, and isn't a particular "automation" process to follow.

I'm guessing (but only guessing) that he wants you filling in things with variables from commandline parameters, instead of using hardcoded names. Template for what, to do what, in what way, remains a mystery.

Her might also mean feeding things into a script's stdin to fill out answers automatically. Or might not.

You can run your script on another computer by means of the ssh command ...

Now there's an answer that helps a lot. Thank you! This whole time I thought automating was something specific. That explains a lot for me. Is it possible to save script and run it elsewhere? How would I go about making my code an executable file, if that is even possible?

---------- Post updated at 02:03 PM ---------- Previous update was at 02:02 PM ----------

How would I go about using that command?

You can do something like this:

ssh username@host <local-scriptfile.sh

chmod +x ? Or did you mean something different?

A shell script is a simple text file. You can create/edit it with a text editor.
You can run it in a Unix shell (terminal) with bash myscript .
You can make it executable with chmod +x myscript and put a shebang (first line) #!/bin/bash . Then you can run it as a new command ./myscript and, if . is in your PATH, myscript .

You asked a lot of questions in one and i can't answer them all (a forum is simply not the right place to teach someone - a good book is better suited for that). I will try to give you some starting points though and suggest some literature you might want to read.

Generally: if you do not know how to use command then issue man command at the commandline. In most cases a manual page (hence the name) with a description will come up, telling you what the command does, which options it understands and how they influence its operation, etc. - basically everything you need to know about it.

I suggest you try that by issuing man ssh . Issuing man chmod would have told you what Scott explained to you above. You might want to try that too an compare what you read there with what Scott told you. Man(ual) pages are always written the same way, so once you get used to using them they are telling you everything you want to know. If you want to work successfully with Unix/Linux you can't start to early using them.

Automation is usually done using scripts instead of issuing single commands. Once we find out that the same group of commands are oftenly used together we can "package" them into a script and then, instead of repeatedly calling the commands, use the script. Almost every OS has such a script language: in z/OS (MVS) it is called REXX, in DOS/Windows it is called "Batch", in modern Windows there is "PowerShell" and in Unix there are even several such languages, most of them related: almost every commando processor (="shell") is not only intended for interactive use but also has its own programming language. The most common shells/languages today are: Korn Shell (ksh), Bourne-Again Shell (bash), Bourne-Shell (sh/bsh), POSIX shell (sh).

You will have to select one of these, but they are very similar. Most code written for one will also run in the others.

My suggestion is to pick either ksh or bash - these are the most widely used shells - but my preference is with ksh. It is IMHO better suited for scripting, but this might be personal preference. It would go too far to discuss the differences here. You can start with both of these and get working results.

A recommendable book to learn programming in ksh is the "KornShell Programming Tutorial" by Barry Rosenberg. Written with a fine sense of humor all the while being really informative.

I hope this helps.

bakunin

1 Like