For loops with multiple variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loops with multiple variables
# 1  
Old 03-24-2011
For loops with multiple variables

Hi script gurus.

I have need to know how to use for loop with multiple variable.

Basically lets take for example /etc/passwd file has following entries

Quote:
sparcguy:*:100:100:Solaris admin +sparcguy@unix.com:/home/sparcguy:/usr/bin/ksh
aixguy:*:101:100:Aix Admin +aixguy@unix.com:/home/aixguy:/usr/bin/ksh
hpuxguy:*:102:100:Hpux Admin +hpuxguy@unix.com:/home/hpuxguy:/usr/bin/ksh
Quote:
cat /etc/passwd | grep \@ | sed 's/:/+/g' | cut -d+ -f1,6 | sed 's/+/ /g'
The above cat command will basically first greps the real users that have email addresses then converts ':' to '+' then using cut to extract the userid and email addresses using the '+' as a delimiter to produce the below output

sparcguy sparcguy@unix.com
aixguy aixguy@unix.com
hpuxguy hpuxguy@unix.com

Assuming I pipe the above output to a file called /tmp/list

I know I can just cat the file to display the above but how to I use for loop to display it instead?

Quote:
for i in `cat /tmp/list | awk '{ print $1 }'` && for j in `cat /tmp/list | awk '{ print $2 }'`
the above does not seems to work, does anybody knows how to use multiple variables in a for i in `cat file` loop?

thx in advance
# 2  
Old 03-24-2011
Is something like this what you're looking for?
Code:
sed 's/\([^:]*\).*+\([^:]*\).*/\1 \2/' /etc/passwd | while read name email
do
  echo "$name"
  echo "$email"
  ...
  ...
done

# 3  
Old 03-24-2011
Hi Franklin52,

Thanks for your replies. Well your method works but I need to do some more further manipulation. I'm trying to rewrite the solaris password aging script "pwage" for hpux Trusted systems. I already figured out the calculation part I just need to find a way for the script to be able to reply to the user that's why I'm trying to use a for loop.

https://www.unix.com/shell-programmin...sword-age.html



Quote:
for i in `cat /tmp/list | awk '{ print $1 }'`
do
Quote:
for j in `find /tcb -name $i -exec ls -1 {} \;`
do
LASTPWCHG=`cat $j | grep u_succhg | sed 's/:/#/g' | cut -d# -f3`
DAYSEC=`echo "60*60*24" | bc`
DAWNOFTIME=`/usr/contrib/bin/perl -e 'print int(time)'`
SECSAGO=`echo "$DAWNOFTIME - $LASTPWCHG" | bc`
echo $DAYSEC
echo $DAWNOFTIME
echo $LASTPWCHG
echo $SECSAGO
DAYSAGO=`echo $SECSAGO/$DAYSEC | bc`
echo $DAYSAGO
done
MAXAGE=90
LEFTDAYS=`echo "$MAXAGE - $DAYSAGO" | bc`
echo $LEFTDAYS
if [ "$LEFTDAYS = 7 ]
then
show a reminder and send to user email address.... etc

.
.
.

done

Maybe a little explaination for benefit of others

DAYSEC= number of seconds in a day = 60*60*24

DAWNOFTIME = total number of seconds from 1970 to the present time

LASTPWCHG = on trusted hpux servers encrypted passwords are stored in directories and has a field called u_succhg with a huge number like example :u_succhg#1300355419: this number is from 1970 to the date where the last password was successfully changed in seconds.

so basically find the time from 1970 minus the time of the last password changed you get the number of days ago password was changed compares that to 90 days password expiry you will get remainder in days when the password going to expire

Last edited by sparcguy; 03-24-2011 at 05:26 AM..
# 4  
Old 03-24-2011
Not sure if this is what you're looking for, but it's unnecessary to use a temporary file for the userid.
You could do something like this:
Code:
MAXAGE=90

while IFS=: read uid dummy
do
  for j in `find /tcb -name "$uid" -exec ls -1 {} \;`
  do
    LASTPWCHG=`awk -F: '/u_succhg/{print $3}' "$j"`
    DAYSEC=86400
    DAWNOFTIME=`/usr/contrib/bin/perl -e 'print int(time)'`
    SECSAGO=`echo "$DAWNOFTIME - $LASTPWCHG" | bc`
    DAYSAGO=`echo "$SECSAGO/$DAYSEC" | bc`
    LEFTDAYS=`echo "$MAXAGE - $DAYSAGO" | bc`
    if [ "$LEFTDAYS" -le 7 ]
    then
       # show a reminder and send to user email address.... etc
    fi
  done
done < /etc/passwd

# 5  
Old 03-24-2011
thanks for your reply again dude.

I found a less painful way.

Quote:
EMAILID=`cat ./passwd | grep $i | sed 's/:/+/g' | cut -d+ -f6`
no need to loop

Will share the script for password aging for hpux trusted servers in another thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh to multiple hosts and then run multiple for loops under remote session

Hello, I am trying to login to multiple servers and i have to run multiple loops to gather some details..Could you please help me out. I am specifically facing issues while running for loops. I have to run multiple for loops in else condition. but the below code is giving errors in for... (2 Replies)
Discussion started by: mohit_vardhani
2 Replies

2. Shell Programming and Scripting

Help with multiple for loops

All, I have set up ssh trust between 3 machines. The aim is to connect from machine-A to B and C and clear the txt files in tmp on all 3 machines. And, I have 3 environments and each environment has 2 hosts. So I should be able to run this script on any environment. Here is the logic: I want... (5 Replies)
Discussion started by: pnara2
5 Replies

3. Shell Programming and Scripting

Unset variables in shell when it running two different loops

I have a script to start/stop/restart the tomcat application. When we run the script first time i.e stop/start it set all env variables(DISTRIB_ID,NAME,TOMCAT_CFG,....etc),but when we restart the tomcat it is running in the same shell.....I need to set the variables when i restart the tomcat(in the... (1 Reply)
Discussion started by: praveen265
1 Replies

4. Shell Programming and Scripting

Two variables in output file name nested for loops

I am trying to use two nested for loops to process some files and then create a new file using both variables in the output file name. I have several files in this naming style: S1_L3_all_R1.fastq S1_L3_all_R2.fastq S1_L4_all_R1.fastq S1_L4_all_R2.fastq . . S1_L8_all_R1.fastq... (3 Replies)
Discussion started by: aminards
3 Replies

5. UNIX for Dummies Questions & Answers

While Loops Multiple File

Guru, I try to make a loop of 2 files, Input File1.txt: 1 2 File2.txt: A B C A, B and C is a file name, inside A X Y Z Expected Output A.1 X (2 Replies)
Discussion started by: guns
2 Replies

6. Shell Programming and Scripting

while loops and variables under bash

Hi, This is probably going to be very simple but i came across something i can't quite explain. Here is the situation: i have a list of files, which i'd like to process one by one (get the size, make some tests, whatever) and generate some statistics using different variables. Something... (5 Replies)
Discussion started by: m69w
5 Replies

7. Shell Programming and Scripting

Multiple loops for Load test

Hi All, I am trying to write a bash script that will read a list of numbers from a file, then it needs to use netcat to create a socket connection and pass header/request. I need to limit it to 100 connections. So, after the first set of 100 are fineshed, I need to loop and do it again with... (0 Replies)
Discussion started by: willdev
0 Replies

8. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies

9. UNIX for Dummies Questions & Answers

Variables being worked on inside of loops

I have a while read loop that reads values inside of a file and then performs an expr operation on each. Everything works fine, the way it's supposed to but, once the loop is finished, I can not access the variable that I used inside of the loop (but that variable was created outside of the... (7 Replies)
Discussion started by: yongho
7 Replies

10. UNIX for Dummies Questions & Answers

Multiple for loops within a Menu?

I have program that I want to be able to use I guess you would call them functions.... to run muliple little programs or loops with one menu script. How would I do this. Here is some code I am using. Sorry about the formatting....it doesn't paste well. echo "*****************************... (3 Replies)
Discussion started by: darthur
3 Replies
Login or Register to Ask a Question