3 Replies - 325 Views - Last Post: 04 January 2014 - 08:02 PM Rate Topic: -----

#1 Hydrox2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-January 14

Unix script help. Totally clueless :/

Posted 03 January 2014 - 05:41 AM

Hey guys,

If anyone has any spare time I'd really like some help with this script i'm supposed to write for this assignment. Any help would be massively appreciated. Here is what it has to do. I'll copy and paste this bit.


"Imagine you are a Unix system administrator and one of your regular jobs is to set up accounts for new users. In the present case this is pretty simple. You are given a list of new user-names and a list of fi les each user requires in her/his
home directory (folder). For each new user, the home folder needs to be created if it does not already exist, the fi les in the list need to be copied there, and their permissions need to be set to read, write, execute for the user only. This is pretty tedious if there are dozens (or hundreds) of new users to be
processed, so you will develop a Unix script which will do the job."

It will take four run-time arguments:
1. a path to a file listing the new user names,
2. a path to a fi le listing the fi les to be copied,
3. a path to a folder where master copies of the fi les are stored, and
4. a path to a folder where the new home folders will be created.

There are certain detailed requirements, as follows.
1. All four run-time arguments should be present.
2. If the le listing the new user-names is not in the path provided, the
script displays an appropriate error message and halts without further
processing.
3. If the fi le listing the files to be copied is not in the path provided, the script displays an error message and halts without further processing.
4. The script checks if master copies of all the fi les in the list can be found
in the path provided, and if any are missing, the script displays an error
message and halts without further processing.
5. If the folder where the new user home folders are to be created does not
exist in the path provided, the script displays an error message and halts
without further processing.
6. The script checks if any of the user-names already have home folders in
the home-folder-path. If so, the user is asked whether to delete or keep
the contents. Depending the answer,

DELETE- the existing home folder is deleted (together with all its contents);

or

KEEP- the existing home folder is renamed to its former name plus suffix _old


7. Once all these steps are passed, then for each user-name in the fi le,
(a) create the home folder, with the same name as the user-name.
(B) copy the fi les in the le list from the master copy folder into the new
home folder.
2© set the permissions of these files. NB only the user should have read,
write, execute permission; others should have no permissions on these files


I honestly am totally clueless when it comes to this, our tutor is less than helpful and it's all really confusing so I would seriously appreciate some help :) Thanks again

Is This A Good Question/Topic? 0
  • +

Replies To: Unix script help. Totally clueless :/

#2 no2pencil  Icon User is offline

  • Dic Head
  • member icon

Reputation: 5167
  • View blog
  • Posts: 26,850
  • Joined: 10-May 07

Re: Unix script help. Totally clueless :/

Posted 03 January 2014 - 05:57 AM

View PostHydrox2013, on 03 January 2014 - 07:41 AM, said:

It will take four run-time arguments:

Arguments hare handled with $1, $2, & so on.

#!/bin/bash

if [ ! $1 ]; then
  echo $1 is required as the file listing new user names
  exit
else 
  names_file=`echo $1`
fi
if [ ! $2 ]; then
  echo $2 is required as the file that lists the files to be copied
  exit
else
  files_file=`echo $2`
fi
if [ ! $3 ]; then
  echo $3 is required as the folder where master copies of the files are stored
  exit
else
  master_copies_file=`echo $3`
fi
if [ ! $4 ]; then
  echo $4 is required as the folder where the new home folders will be created
  exit
else
  new_files_folder=`echo $4`
fi



View PostHydrox2013, on 03 January 2014 - 07:41 AM, said:

There are certain detailed requirements, as follows.
1. All four run-time arguments should be present.

This is what I just explained above. Each argument that is required is verified. If it was not passed in, the script fails & says why.

View PostHydrox2013, on 03 January 2014 - 07:41 AM, said:

2. If the le listing the new user-names is not in the path provided, the
script displays an appropriate error message and halts without further
processing.

How would you do this on the command line? This is the same thing you would plug into the script.

View PostHydrox2013, on 03 January 2014 - 07:41 AM, said:

3. If the fi le listing the files to be copied is not in the path provided, the script displays an error message and halts without further processing.

Same as above. You do it in the script as you would do it on the command line.

View PostHydrox2013, on 03 January 2014 - 07:41 AM, said:

4. The script checks if master copies of all the fi les in the list can be found
in the path provided, and if any are missing, the script displays an error
message and halts without further processing.

You can read the files into a variable with : files=`ls ${directory}`, & then check them each with if [ -f ${file} ]; then...

View PostHydrox2013, on 03 January 2014 - 07:41 AM, said:

5. If the folder where the new user home folders are to be created does not
exist in the path provided, the script displays an error message and halts
without further processing.


You can check directories the same way you check files, with if [ -d ${directory} ]; then ...

View PostHydrox2013, on 03 January 2014 - 07:41 AM, said:

6. The script checks if any of the user-names already have home folders in
the home-folder-path. If so, the user is asked whether to delete or keep
the contents. Depending the answer,

Again... the same way you'd do this on the command line, mixed with what you'd do on #5.

View PostHydrox2013, on 03 January 2014 - 07:41 AM, said:

DELETE- the existing home folder is deleted (together with all its contents);

Again, scripts are just auotmating command line tasks.

View PostHydrox2013, on 03 January 2014 - 07:41 AM, said:

7. Once all these steps are passed, then for each user-name in the fi le,
(a) create the home folder, with the same name as the user-name.
(B)/> copy the fi les in the le list from the master copy folder into the new
home folder.
2© set the permissions of these files. NB only the user should have read,
write, execute permission; others should have no permissions on these files

I honestly am totally clueless when it comes to this, our tutor is less than helpful and it's all really confusing so I would seriously appreciate some help :)/> Thanks again

If you are really clueless on how to perform these steps, how are you in a scripting language to automate Linux/Unix tasks that require standard system administration? The requirements for this are easily re searchable, & you should not ave any trouble finding what you need in standard scripting books. Just think how you would do it on the command line, & then automate it in the script.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3190
  • View blog
  • Posts: 10,681
  • Joined: 12-December 12

Re: Unix script help. Totally clueless :/

Posted 03 January 2014 - 05:57 AM

What do you know about Unix scripts? Presumably you have a book or some notes.

Linux/Unix Tutorials
Was This Post Helpful? 0
  • +
  • -

#4 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: Unix script help. Totally clueless :/

Posted 04 January 2014 - 08:02 PM

View Postno2pencil, on 03 January 2014 - 06:57 AM, said:

#!/bin/bash

if [ ! $1 ]; then
  echo $1 is required as the file listing new user names
  exit
else 
  names_file=`echo $1`
fi
if [ ! $2 ]; then
  echo $2 is required as the file that lists the files to be copied
  exit
else
  files_file=`echo $2`
fi
if [ ! $3 ]; then
  echo $3 is required as the folder where master copies of the files are stored
  exit
else
  master_copies_file=`echo $3`
fi
if [ ! $4 ]; then
  echo $4 is required as the folder where the new home folders will be created
  exit
else
  new_files_folder=`echo $4`
fi



Slight refactor:
#!/bin/bash

raise() { echo "$1" && exit; }

[ $1 ] && names_file="$1" || raise '$1 is required as the file listing new user names'

[ $2 ] && files_file="$2" || raise '$2 is required as the file that lists the files to be copied'

[ $3 ] && master_copies_file="$3" || raise '$3 is required as the folder where master copies of the files are stored'

[ $4 ] && new_files_folder="$4" || raise '$4 is required as the folder where the new home folders will be created'




Yay for psuedo bash ternaries!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1