[X-Unix] Re: Loop structure syntax question

Eugene list-themacintoshguy at fsck.net
Thu Nov 24 06:27:01 PST 2005


On Wed Nov 23 13:07:32 PST 2005, Mac Daddy <macdaddee at gmail.com> wrote:
> 
> I have a script running on several macs, a small part of which fails.
>
> ---------------------------------- snippet
> f# Add Admin users first.
>
> foreach login ( `cat ./logins_Y.txt` )
> dscl . merge /groups/cq users "$login"@<mydomain.com>
> end
>
> # Add normal Users next
>
> foreach login ( `cat ./logins_A.txt` )
> dscl . merge /groups/cq users "$login"@<mydomain.com>
> end
> --------------------------  end snippet
>

This is how I would it in Bourne shell:

	#
	# if you don't specify the path, the shell assumes your current
	# working directory.  it might explain why the script ran by
	# hand but not in a cron job, as the current working directory
	# may not be the same between your login environment and the
	# cron job environment.  also, avoid using the '.' path in your
	# shell scripts and stick with absolute paths.
	#
	loginidsfile="/path/to/logins_A.txt"
	for loginid in $(cat "${loginidsfile}")
	do
		# 
		# i prefer to quote all parameters that are subject to
		# variable substitution.  also, '<' and '>' characters
		# are just too close to being redirection pipes for my
		# tastes.
		#
		dscl . merge /groups/cq users "${loginid}@<mydomain.com>"
	done

You could then put this in a shell script function.  Instead of writing
the same code twice, you simply pass different login files to the same
function.  That is left as an exercise to the gentle reader...


-- 
Eugene
http://www.coxar.pwp.blueyonder.co.uk/


More information about the X-Unix mailing list