LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-03-2017, 12:34 AM   #1
Adol
Member
 
Registered: Feb 2011
Location: Osaka, Japan
Distribution: Gentoo, Opensuse
Posts: 268

Rep: Reputation: 5
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
 
Old 05-03-2017, 12:58 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,510

Rep: Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552
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)?
 
Old 05-03-2017, 02:06 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,263
Blog Entries: 10

Rep: Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161
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.
 
Old 05-03-2017, 10:07 AM   #4
Adol
Member
 
Registered: Feb 2011
Location: Osaka, Japan
Distribution: Gentoo, Opensuse
Posts: 268

Original Poster
Rep: Reputation: 5
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!!!
 
Old 05-03-2017, 12:42 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,263
Blog Entries: 10

Rep: Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161Reputation: 5161
Quote:
Originally Posted by Adol View Post
Not sure what my syntax error is.
http://ryanstutorials.net/bash-scrip...statements.php
 
Old 05-03-2017, 01:24 PM   #6
luvr
Member
 
Registered: May 2005
Location: Boom - The Home Town of Tomorrowland, Belgium
Distribution: Slackware, Xubuntu
Posts: 348
Blog Entries: 1

Rep: Reputation: 140Reputation: 140
Quote:
Originally Posted by Adol View Post
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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy Bash Script Linux?! How?! eRzBeNgEl Linux - Newbie 7 04-11-2011 07:35 PM
Easy question about bash hackarre Programming 2 09-22-2007 04:35 PM
Easy Bash Script Question skibud2 Linux - Newbie 1 11-17-2004 11:14 AM
Bash script!! need help!!!!!could be easy! yulitao Programming 3 03-25-2004 06:52 PM
Easy bash script question TheRealDeal Linux - General 11 08-26-2003 09:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration