Efficient coding / Alternates for "For" loop


 
Thread Tools Search this Thread
Operating Systems AIX Efficient coding / Alternates for "For" loop
# 1  
Old 04-17-2009
Efficient coding / Alternates for "For" loop

Hi,

I have the current script with the following code and it works ok.

Code:
for i in `find . -name "???ABCEDFGH*"`
do
    SESS_NO=`echo $i | awk -F "." '{print $3}'`
    rm -f $LOGDIR/${OT_QUEUEID}*${SESS_NO}.log 2>/dev/null
    rm -f $i 2>/dev/null
done

I want to remove the usage of the following code and replace it with something else since there could be limits as to the size of arguments that a "for" command could take in AIX 5.2 ksh where in this executes.

Code:
`find . -name "???ABCEDFGH*"`

I could convert the same into something as follows

Code:
> /tmp/abcdefgh.txt
find . -name "???ABCDEFGH*" > /abcdefgh.txt
while read line
do
     SESS_NO=`echo $line | awk -F "." '{print $3}'`
     rm -f $LOGDIR/${OT_QUEUEID}*${SESS_NO}.log 2>/dev/null
     rm -f $line 2>/dev/null
done < /tmp/abcdefgh.txt

Please advise me on more alternate efficient methods other than what I have provided above.
Thx
Jerardfjay Smilie
# 2  
Old 04-17-2009
Welcome as the first member of the i-shun-for-loops-for-their-inherent-danger-club. The drinks are on the clubs president - me. ;-)

You are absolutely correct and i have said it a dozen times here that for-loops on unlimited content (like a directory listing) are a dangerous thing, because the code could break as the maximum command line length is reached.

You can run it directly in a pipeline without the intermediate file:

Code:
find /path/to/dir -name "something" -print | while read filename ; do
     do_something $filename
done

Btw., speaking of coding practice: maybe its only me but i find it easier to read later when the input to the while-loop is at the top and not the bottom. Therefore this:

Code:
cat file | while read line ; do
     do_something $line
done

is easier to read than that, especially when the loop is long:

Code:
while read line ; do
     do_something $line
done < file

I hope this helps.

bakunin
# 3  
Old 05-01-2009
There is also the "xargs" command which takes long inputs and creates a command for each line.

E.g. find some files and run the "file" (appears after xargs) on each one of them:

# find /usr/lib -name '*.so' | xargs file
/usr/lib/kdb/auto/GetLine/GetLine.so: executable (RISC System/6000) or object module not stripped
/usr/lib/IbTsLib/libTsIb.so: executable (RISC System/6000) or object module not stripped
/usr/lib/IbGxLib/libGxIb.so: executable (RISC System/6000) or object module not stripped
[snip]
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

Question about the difference between "for" and "while" loop

dear all, i got some questions about for/while loop when working on redirect command output to for/while loop. take one for example : in for loop : allfiles=`find /var/log -maxdepth 1 -type f -mtime +5` index=1 for ((i=0; i<${#allfiles}; i++)); do echo "$index:${allfiles}" ... (2 Replies)
Discussion started by: tiger2000
2 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

BASH: Interactive "cp" (and "mv") in a loop

Hi. I have a copy-file script (and a move-file script) that I recently tried to make interactive. I tested the former on three files from a text list, and watched to see what would happen. As the cp command was in a while/do/done loop, there was no pause for input: it wrote the file from the... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

8. Shell Programming and Scripting

"for" and "while" loop problem in "sh"

Hi, I have a problem with "for" and "while" loop in "sh". I have: #!/bin/sh for i in $(seq 1 500000); do echo $i doneand it's working in sh on my ubuntu, but when I try to run this on unix(I have access to my university's unix) it crash: syntax error at line 2: `$' unexpected...... (8 Replies)
Discussion started by: Physix
8 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question