Visit Our UNIX and Linux User Community


loop doesnt work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop doesnt work
# 1  
Old 07-24-2009
loop doesnt work

It just does the break...even though the files are not the same...

Code:
 
# Compare extracts
        #==========================================
       
     count=0
     while (( count < 5 ))
     do
         (( count+=1 ))
         echo "Try $count"
         file1=$(ls -l /tmp/psjava.xml| awk '{print $1,$2}')
         file2=$(ls -l /tmp/dbextract.txt| awk '{print $1,$2}') 
         if (( count >= 5 ))
         then
           echo "Too many retries"
           echo "Please check source why the files dont match "
           exit 1
         fi    
            
         if [ "$file1" != "$file2" ]
         then
              echo "File1 and File2 don't match. I will sleep 30 seconds and try again. 5 tries is my limit."
       sleep 30
       else
              echo "File1 and File2 match."
              break 
         fi
 done

# 2  
Old 07-24-2009
Hi.

It works for me (ksh)

Code:
> touch a b
> ls -l
total 16
drwxr-xr-x    2 me     mygroup            256 Jul 24 17:03 .
drwxr-xr-x   29 me     mygroup           4096 Jul 24 17:02 ..
-rwx------    1 me     mygroup            735 Jul 24 17:03 Test
-rw-r--r--    1 me     mygroup              0 Jul 24 17:03 a
-rw-r--r--    1 me     mygroup              0 Jul 24 17:03 b
> ./Test
Try 1
File1 and File2 match.
> ln a c
> ls -l
total 16
drwxr-xr-x    2 me     mygroup            256 Jul 24 17:03 .
drwxr-xr-x   29 me     mygroup           4096 Jul 24 17:02 ..
-rwx------    1 me     mygroup            735 Jul 24 17:03 T
-rw-r--r--    2 me     mygroup              0 Jul 24 17:03 a
-rw-r--r--    1 me     mygroup              0 Jul 24 17:03 b
-rw-r--r--    2 me     mygroup              0 Jul 24 17:03 c
> ./Test
Try 1
File1 and File2 don't match. I will sleep 30 seconds and try again. 5 tries is my limit.
>

# 3  
Old 07-24-2009
This don't give the filenames:

Code:
file1=$(ls -l /tmp/psjava.xml| awk '{print $1,$2}')

Try this instead:

Code:
file1=$(ls -l /tmp/psjava.xml| awk '/^-/{print $8}')

# 4  
Old 07-24-2009
Hi Franklin.

It's not the filenames he's after, I believe.

It's the permissions and link count.

In any case $9 gives the filenames not $8, and why do two ls's on two different files and expect the filename to be t he same? (unless your name's freddie Smilie)

In any case on top of that the script works for me in ksh (not clear which shell he is using).
# 5  
Old 07-24-2009
Quote:
Originally Posted by scottn
Hi Franklin.
In any case $9 gives the filenames not $8
Not with every version of ls, probably it's more safe to use $NF assuming the OP doesn't have names with spaces.
With this version:

Code:
ls --version
ls (GNU coreutils) 5.97

I get this output:

Code:
$ ls -l
total 12
-rw-r--r-- 1 users franklin 336 2009-03-24 15:44 az
-rw-r--r-- 1 users franklin 336 2009-03-24 15:44 bz
-rwxr-xr-x 1 users franklin 651 2009-03-24 16:13 inplace.awk

Quote:
Originally Posted by scottn
why do two ls's on two different files and expect the filename to be t he same? (unless your name's freddie) Smilie
Smilie
# 6  
Old 07-24-2009
I try to avoid using GNU stuff Smilie

$NF is always a safe bet (spaces aside) (unless you're freddie, of course!)

Previous Thread | Next Thread
Test Your Knowledge in Computers #627
Difficulty: Medium
When a function clones itself, it is considered to be recursive-clone function.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Creating for loop for tar -tvf doesnt work

hi all, i have created a for loop, it looks like this - #!/bin/bash cd /mnt/local/data/tars for tar in * do base=$(basename "$tar") "$base" -tvf >> /mnt/local/data/logs/"$base".csv done but i get this error - ./tar_loop.sh: line 8:... (2 Replies)
Discussion started by: robertkwild
2 Replies

2. Shell Programming and Scripting

Why doesnt if inside a while loop work ?

I am looping through a file in bash and performing some operations on it. Here is the code. cat test.dat - One Two Three Case 1: With if inside while loop Output: One file found :) ------------------ isn't it supposed to print following output !!! one (3 Replies)
Discussion started by: qwarentine
3 Replies

3. UNIX for Dummies Questions & Answers

why doesnt it work?

I am trying to print out two fields in a file using awk. So, I have got awk -F '\t' 'NF = 2 {print $1 $2 "]"}' two.txt in a script called what.awk When i run this version like this - ./what.awk then it runs however I want to run the program like this awk -f what.awk two.txt. When I... (8 Replies)
Discussion started by: The undertaker
8 Replies

4. AIX

Vi doesnt work

Hi Guys, I have a strange problem.( AIX 6.1) "vi" is not working at all..Whenever i #vi <anythin> ,, it returns the prompt back. Any clues folks?? (14 Replies)
Discussion started by: muzahed
14 Replies

5. Shell Programming and Scripting

compiler doesnt work

this is my file I have written. // My first C++ program #include <iostream> int main() { std::cout << "Hi there!" << std::endl"; std::cout << "This is my first C++ program" << std::endl"; return(0); } This is the error I get, why? $ g++ first.cpp ksh: g++: not found (1 Reply)
Discussion started by: gustave
1 Replies

6. Red Hat

ldapsearch doesnt work.

Hii All, I am using openldap v2.3 on redhat El-4. When i run ldapsearch it returns all the entries. The command runs successfully. But when I run the ldapsearch with following filter option it doesnt work and immediately returns to the shell. ldapsearch uidNumber>=2000 I've started slapd... (0 Replies)
Discussion started by: shamik
0 Replies

7. UNIX for Dummies Questions & Answers

cp doesnt work - Help

When trying to copy a file in Solaris 8 it doesnt copy file or give a error. This worked 100% until the 29th. I've checked the rights and everything seems fine: drwxrwxrwx 2 bmuser bmgroup 11776 Jan 3 10:32 spool This is the file I want to copy: -rwxrwxrwx 1 bmuser bmgroup ... (26 Replies)
Discussion started by: rudi.okelly
26 Replies

8. HP-UX

ls command doesnt work

Good Day I mistakely renamed the dld.sl file in the /usr/lib directory. When i try to ls/ftp into the box i get this error :eek: crt0: ERROR couldn't open /usr/lib/dld.sl errno:000000002 I have tried to rename it back from the renamed file to the original file name, but it gives me the... (2 Replies)
Discussion started by: shawnbishop
2 Replies

9. UNIX for Dummies Questions & Answers

cd.. doesnt work

hi when i want to go to previous directory by typing cd.. i get the following message $ cd.. ksh: cd..: not found Please help rintingtong (2 Replies)
Discussion started by: rintingtong
2 Replies

10. Shell Programming and Scripting

why doesnt my script work!!!

Is there a utility or command I can use to tell the number of decimal places a number has. For instance, if the number is 432, it will give hundred as the number of decimal places. (7 Replies)
Discussion started by: Heedunk
7 Replies

Featured Tech Videos