[X-Unix] Loop structure syntax question

Philip J Robar pjrobar at areyoureallythatstupid.org
Wed Nov 23 15:43:36 PST 2005


On Nov 23, 2005, at 1:07 PM, Mac Daddy 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 reads two text files on disk and then adds each name read into  
> a special group in the Netinfo db. It's part of a larger script,  
> and the larger script fails somewhere in the second loop reading  
> "logins_A.txt", leaving the group only partially filled with  
> usernames. VERY BAD, as the group is used for authentication on the  
> machine.
>
> This thing is, this script NEVER fails when run by hand! Only when  
> run in cron. I have the script set to run in #!/bin/csh because  
> this 'foreach' loop structure above fails if I run it under /bin/sh  
> or /bin/bash and the code was given to me by someone else who uses  
> csh on Solaris.
>
> I think it's /bin/csh that's the problem, since when invoked by  
> hand, I'm using /bin/bash, which in turn is invoking this script  
> that runs under /bin/csh. There's no problem then. It's the only  
> thing I can think of. I even have a SECOND script in the cron set  
> to run under #!/bin/sh which checks to see if the first one fails,  
> notifies me if it does, then re-runs the first script as an attempt  
> to correct the situation before I can check. This check script  
> works every single time, further making me think /bin/csh by itself  
> is the problem.
>
> ANYWAY, I'd gladly have the script run under /bin/sh or /bin/bash  
> IF I just knew the proper syntax for the 'foreach' structure under  
> one of the other shells, but I can't figure it out and haven't  
> found it yet.
>
> Can somebody help me out with the proper syntax for one of the  
> other shells please?

First of all I've give the standard advice that using the C shell for  
scripting is a poor choice. See http://www.faqs.org/faqs/unix-faq/ 
shell/csh-whynot/.

Second, I don't think that you've looked very hard for the sh/bash  
looping structure as the internet is rife with scripting tutorials  
which I and others on this list have referenced numerous times.

	Advanced Bash-Scripting Guide, An in-depth exploration of the art of  
shell scripting; http://www.tldp.org/LDP/abs/html/index.html
	See in particular: http://www.tldp.org/LDP/abs/html/loops1.html

	http://www.shelldorado.com/

	for login in $(cat ./logins_A.txt); do
		dscl . merge /groups/cq users ${login}@mydomain.com
	done														

$(...) Is the modern form of command substitution.
${...} Isolates the variable name from characters that follow it so  
that they are not interpreted as part of the variable name. Although  
in this particular case it's not needed, it's still good form to use  
it so as to clearly communicate what you're doing.

Third, without an error message it's hard to know where to even start  
at debugging your code, but here's something to think about. '@' is a  
command in the C shell and '<' and '>' are special characters. You're  
not escaping any of them from their special meaning to the shell.

You might also want to think about who the script is run by and what  
name space is present when running it by hand versus cron.


Phil
--
"To announce that there must be no criticism of the President, or  
that we are to stand by the President, right or wrong, is not only  
unpatriotic and servile, but is morally treasonable to the American  
public." -- Teddy Roosevelt



More information about the X-Unix mailing list