korn shell "loops & arrays"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting korn shell "loops & arrays"
# 1  
Old 09-15-2004
korn shell "loops & arrays"

Hi,

I am trying to write a script which will loop until a certain action has been performed. I have two files i would like to compares.

For example:
file1 has a list of user ids (about 900) from the company's e-mail server.
file2 has a list of user ids (about 50 or so) from /etc/passwd.

I have file1 in a array, I'd like to have file2 in a loop.
When the id matches it will be redirected the output to /dev/null ,
but when the two ids do not match, I need to redirected the output to file3. This is so I can delete user that have moved on.

This was my 1st try.

#!/usr/bin/ksh
set -A array file1
for i in ${array[@]}
do
echo "==== $i vs file2 ===="
diff $i file2
done > file3

------------------------------------------

and my 2nd try.

#!/usr/bin/ksh
egrep -if file2 file1 > tmp_name
egrep -ivf tmp_name file2 > file3
rm tmp_name

-------------------------------------------

and then:


#!/usr/bin/ksh
while read username
do
while read file2
do
if [ '$file1' = '$file2' ]; then

else

if
done < file2
done < file1
# 2  
Old 09-15-2004
u can use 'comm' command to make it simple.


comm x y

where x and y are files having the list of names/userids.
# 3  
Old 09-16-2004
In case you want to get the ksh script running, I'll comment on that. In your first try you were moving toward a solution where you repeatedly scanned a file. Don't do that. You want to read each file once. I think something like this will work:
Code:
i=0
exec < file1
while read array[i] ; do
      ((i=i+1))
done
exec <file2
while read entry ; do
      i=0
      found=0
      while ((i<${#array[@]})) ; do
            if [[ $entry = ${array[i]} ]] ; then
                 found=1
                 break
                 ((i=i+1))
            fi
      done
      if ((found)) ; then
            echo $entry is in file1
      else
            echo $entry is not in file1
      fi
done

You might get away with something like:
set -A array $(cat file1)
but the resulting set statement must be less than the max line length. It might work at first, then fail later. The loop seems a bit safer. And it fires up a cat process. So the loop will be a bit faster too.
# 4  
Old 09-17-2004
cool, thank you both.
# 5  
Old 09-23-2004
Question

Perderabo,

Can you please explain your code line by line? I think I fishing for the same type of solution, but a lot of it is confusing to me.
# 6  
Old 09-23-2004
I'm not going to explain every line. Most of it is straightforward. What is it you don't understand? The arrays? Do you know what an array is?

One thing: ${#array[@]} is the number of elements in the array.
# 7  
Old 09-23-2004
Lightbulb

i guess i don't know what it is. Smilie my thought of an array was a way to use multiple values for one variable name. well, I wanted to use your code of reading 2 files (lists) and using the variables on a conditional basis to print a message. (won't go any further, due to cross-posting).

ok, so in this part:

Code:
i=0
exec < file1
while read array[i] ; do
      ((i=i+1))

done

you're reading the file in and looping.

My confusion is with this part:
Code:
exec <file2
while read entry ; do
      i=0
      found=0
      while ((i<${#array[@]})) ; do
            if [[ $entry = ${array[i]} ]] ; then
                 found=1
                 break
                 ((i=i+1))
            fi
      done
      if ((found)) ; then
            echo $entry is in file1
      else
            echo $entry is not in file1
      fi
done

I assume that the found=0 is setting the count to zero? This line is the most confusing to me: while ((i<${#array[@]})) ; do

Are you saying do .. while "i" is less than the value of array[@]?
Thanks for any input.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (14 Replies)
Discussion started by: thisissouvik
14 Replies

2. AIX

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (2 Replies)
Discussion started by: thisissouvik
2 Replies

3. Web Development

How would I mod_rewrite "/~a1Pha" and "/=a1Pha" to "/paste/a1Pha.htm"? (internally & externally)

Basically I want to shorten URLs on my html pasting site (pasteht.ml), by using "/~a1Pha" instead of "/paste/a1Pha". The ID is 5 numbers and letters, both cases. For example: /~idnum serves /paste/idnum.htm /=idnum serves /paste/idnum.htm /paste/idnum redirects to /~idnum (to update any old... (0 Replies)
Discussion started by: phillips1012
0 Replies

4. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

5. Red Hat

files having Script which works behind "who" & "w" commands

Dear All, plz print the path of files which have the script of "who" & "w" commands. thnx in advance. (6 Replies)
Discussion started by: saqlain.bashir
6 Replies

6. Programming

fork&pipe "interpretting" shell - problem

hello everybode.Got some sort of "problems" with this stuff; well this is a program int main() { int Pipe; int origStdin, origStdout; int childPID; origStdin = dup(0); origStdout = dup(1); pipe(Pipe); if( (childPID = fork()) < 0 ) { perror(... (2 Replies)
Discussion started by: IdleProc
2 Replies

7. Shell Programming and Scripting

How to include RETURN KEY with Background process "&" in Shell Script

Hello All, I am a newbie in Shell script programming, and maybe you can help me with my query. I need to write a shell script (mntServer.ksh) that will start a background process and also to be able to run another script. The mntServer.ksh script contains: #!/bin/ksh... (1 Reply)
Discussion started by: racbern
1 Replies

8. Shell Programming and Scripting

shell script & sqlplus "plzz help"

hi friends, i m tryin to load data from one set of table to other i have sql procedure al ready for it..! i m going to load the procedure only if data in one of my table for example table "landing " have 10 records each attribute of this table is file_name status date ... (2 Replies)
Discussion started by: kulbir
2 Replies

9. Shell Programming and Scripting

Find -name "*.txt" in Korn Shell Script

The following find command works on the Korn Shell command line: find . \( ! -name . -prune \) -type f -name "*.txt" -mtime +100 In the particular directory I'm in, the above find will list correctly the three text files that exist that haven't been modified in over 100 days: ... (3 Replies)
Discussion started by: jwperry
3 Replies

10. Shell Programming and Scripting

Korn shell "select" command

I wish to display a full "ls -l" line per selection using the korn shell "select" command. ie : 1) -rw-rw---- 1 u22adm tbs 6144 Mar 15 10:29 y.dat 2) -rw-rw---- 1 u22adm tbs 4096 Mar 15 10:29 y.idx etc etc I can get spaces in : select f in "a a" "b b" "c c"; do... (2 Replies)
Discussion started by: mpegler
2 Replies
Login or Register to Ask a Question