Operating System - HP-UX
1752571 Members
5200 Online
108788 Solutions
New Discussion юеВ

Re: for loop processing problem

 
SOLVED
Go to solution
MikeL_4
Super Advisor

for loop processing problem


I am doing some processing against the /home
directory for certain users and am having an error I'm not sure how to resolve..

I'm entering:

cd /home
for user in tupz* u* dis*; do echo $user; done

The output from this command return:
tupzab4
tupzhb6
tupzjmb
ual001
ubs000
ucb006
uck002
udf003
uds003
dis*

If it doesn't find a user starting with "dis" as in this case, it tries to process a user called: "dis*"

Is there anything I can do to eliminate this
problem???

Thanks

dis*
[root@us1lxmgt01 /home]#
4 REPLIES 4
RAC_1
Honored Contributor

Re: for loop processing problem

for user in tupz* u* dis*; do echo $user 2> /dev/null; done

Anil
There is no substitute to HARDWORK
Simon Hargrave
Honored Contributor
Solution

Re: for loop processing problem

for user in `ls -d tupz* u* dis*`

this will populate with only matches.
Muthukumar_5
Honored Contributor

Re: for loop processing problem

you can simply use as,

ll /home/ | grep -E '^d' | grep -E ' tupz[^*]|u[^*]|dis[^*] '



Easy to suggest when don't know about the problem!
Rodney Hills
Honored Contributor

Re: for loop processing problem

By definition ksh/sh will not expand a word if there is no matching filename. csh behaves differently.

A possibile solution to prevent "dis*" from being processed, is to add the following after the "for" command-

if [[ $user != ${user%%\*} ]] ; then continue ; fi

This if statement tests $user to see if it ends with a "*". If so then skip it.

HTH

-- Rod Hills
There be dragons...