LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Script Question Maybe easy (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-question-maybe-easy-4175605132/)

Adol 05-03-2017 12:34 AM

Bash Script Question Maybe easy
 
Hello All,

I'm trying to rework a script I saw a long time ago(found it and posted it below). It's actually for refreshing student user accounts on OS X, but of course the scripting is the same.

I've got it working except for one part. I'm not sure how to make it only run the script if the admin is not logging in. I know I need to use an "if" statement, but how do I check for admin login and then set that as a parameter to not run the script.

Here is what I have: Pretty simple.

Code:

#!/bin/sh

#  New Student Refresh.sh

#
#  Created by Sebastian Petreus on 5/2/17.
#

#set localAdmin = ddit

### Script action ###
# If this is not the admin user...
if ( $1 != $localAdmin ) then

# Copy a new default home directory from the user template
rsync -avzh --delete /Library/Management/student /Users/

# Change Permissions
chown student -R /Users/student
fi

### Always exit with 0 status
exit 0

The "$localAdmin" part I saw online. Not sure what it means, but doesn't work for me.

Open to any ideas.

Thanks!

And it's a very simple adaptation from this. I just don't want it copying whole directories over:

Code:

#!/bin/tcsh -f

############################ refresh-student-tmp.sh ############
# Mike Bombich | bombich@apple.com               
# Copyright 2003 Apple Computer, Inc.   
# Use Loginwindow Manager (http://www.bombich.com/software/lwm.html)
# to make this shell script run each time a user logs in or out.
####################################################################


### Description ###
#
# The purpose of this script is to move the home directory
# of the user that was just logged in to a temporary location,
# then copy the user template to the Users directory to provide a
# fresh, consistent interface for the next user.
# This script assumes that you want to manage users that all have
# the same home directory specified in NetInfo or LDAP.
#
# If a user that just logged out realizes that they need
# something that was saved in the home directory, they can get to
# it by selecting "Go to Folder..." from the Finder's "Go" menu,
# then typing "/tmp". This script will only replace a home
# directory if the same user logs in (e.g. the default "student"
# user). If you login as the local admin, for example,
# the "student" user directory will still be in /Users.
#
# /tmp is cleared out on restarts. You could also create a cron task
# to remove these backups on a regular basis


### Properties ###
#
# These items must be modified to suit your environment before
# implementing this script! You do not need to make any other
# modifications to this file than these properties.
#
# defGrp: the group that your default user is assigned to
# defHome: the location of your default user's home directory
# defTemplate: the location of your default user home dir template

set defGrp = staff #(This was changed in Leopard.  It use to be "student")
set defHome = /Users/student
set defTemplate = /Library/Management/student
set localAdmin = ddit

### Script action ###
# If this is not the admin user...
if ( $1 != $localAdmin ) then
       
       
        # Copy a new default home directory from the user template
        rm -fr /Users/student
        cp -fr /Library/Management/student /Users/

        # Change the ownership of the new home directory to the user logging in
        /usr/sbin/chown -R ${1}:${defGrp} $defHome
endif

### Always exit with 0 status
exit 0


pan64 05-03-2017 12:58 AM

Code:

if [ "$1" != "$localAdmin" ] then
would be better.
I do not really understand:
Quote:

The "$localAdmin" part I saw online. Not sure what it means, but doesn't work for me.
what did you want to achieve, and why did you think this will do that for you, and what's happened (instead of the expected behaviour)?

ondoho 05-03-2017 02:06 AM

usually, shell script == bash. sometimes dash.
it seems in your post neither the original nor your adaptation are bash.
i can see both scripts failing on bash/dash.

please clarify.

Adol 05-03-2017 10:07 AM

Pan64: My goal is to only run the rsync command if the admin user (ddit) doesn't log in. If "ddit" logs in I do not want any changes to the student user so that it can be edited, fixed, saved, etc...

When the student or other user logs in, I would like it to run my rsync command so that every student steps into s fresh environment that was set up before hand.

I thought it would work because I stole it from the original script that we used in the past. From my understanding it's checking to see if the local admin is signing in.

The actual rsync is working as it should and is tied to the login window as an automatic run command, but can't get the admin user part to work.

Does that make sense?

At this point my if statement is failing with the following:
Code:

./New Student Refresh.sh: line 20: syntax error near unexpected token `fi'
./New Student Refresh.sh: line 20: `fi'

Not sure what my syntax error is.

ondoh: I can change the #!/bin/sh to #!/bin/bash

Does this actually make a difference in the script? I thought that since I used the hashtag it doesn't read that line.

Thank you both for your help!!!

ondoho 05-03-2017 12:42 PM

Quote:

Originally Posted by Adol (Post 5705678)
Not sure what my syntax error is.

http://ryanstutorials.net/bash-scrip...statements.php

luvr 05-03-2017 01:24 PM

Quote:

Originally Posted by Adol (Post 5705678)
Not sure what my syntax error is.

As the link posted by ondoho demonstrates, the syntax of an if command is:
Code:

if CONDITION
then
  STATEMENTS
fi

Note that “then” is on a separate line, i.e., it is a statement in its own right.
Alternatively, you could insert a semicolon (i.e., “;”) between the CONDITION and the “then”:
Code:

if CONDITION ; then
  STATEMENTS
fi

Quote:

I can change the #!/bin/sh to #!/bin/bash

Does this actually make a difference in the script? I thought that since I used the hashtag it doesn't read that line.
If the first line of a script starts with a “hashbang” (i.e., “#!”—a hash followed by a bang or exclamation mark), then it will be called the “hashbang” or “shebang” line, and it will be considered special, in that it should then identify the program that will be used to run the script.

Thus, if your script begins with “#!/bin/sh”, then it will be run by the /bin/sh program—i.e., the default command shell on your system. If, on the other hand, it begins with “#!/bin/bash”, then it will be run specifically by the bash shell. Using the default shell should be the more portable option, while bash will generally be more powerful—but if you use any features that only bash supports, then your script will not run on a system on which bash is unavailable.

In fact, the “shebang” line can be set to other values as well, such as “#!/usr/bin/perl” (for a Perl script), or “#!/usr/bin/python” (for a Python script), or even “#!/usr/bin/python3” (for a Python script that requires Python 3, and will not work with Python 2).


All times are GMT -5. The time now is 02:57 AM.