Trying to run a basic for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to run a basic for loop
# 1  
Old 10-15-2013
Trying to run a basic for loop

OS : RHEL 6.1
Shell : Bash

I had a similair post on this a few weeks back. But I didn't explain my requirements clearly then. Hence starting a new thread now.

I have lots of files in /tmp/stage directory as show below.

I want to loop through each files to run a command on each file.
I want to skip those files whose names end with a number. How can I do this ? My following attempts in writing the for loop has failed.


Code:
# touch sharp
# touch sharper
# touch sharpest
# touch sharpener
# touch sharpy
# touch sharper2
# touch sharpest4

# pwd
/tmp/stage
# ls -ld *
-rw-r--r--. 1 root root 0 Oct  8 03:12 sharp
-rw-r--r--. 1 root root 0 Oct  8 03:12 sharpener
-rw-r--r--. 1 root root 0 Oct  8 03:12 sharper
-rw-r--r--. 1 root root 0 Oct  8 03:14 sharper2
-rw-r--r--. 1 root root 0 Oct  8 03:12 sharpest
-rw-r--r--. 1 root root 0 Oct  8 03:14 sharpest4
-rw-r--r--. 1 root root 0 Oct  8 03:12 sharpy
#

# for i in /tmp/stage/sharp[a-b]* ; do echo $i ; done
/tmp/stage/sharp[a-b]*
#
#
#
# for i in /tmp/stage/sharp[a-b] ; do echo $i ; done
/tmp/stage/sharp[a-b]
#
#
# for i in /tmp/stage/sharp*[a-b] ; do echo $i ; done
/tmp/stage/sharp*[a-b]
#

I want this for loop to run a command on each file like

Code:
for f in /tmp/stage/sharp[a-z]
do
 echo "Processing $f"
 # do something on $f
done

# 2  
Old 10-15-2013
What gives the following command?
Code:
for i in sharp!(*[0-9])
do echo $i
done

Note that this is Shell expression, not REGEX ! ...

Last edited by ctsgnb; 10-15-2013 at 04:50 PM.. Reason: removing the second wildcard since only files ending with numbers should be filtered out
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 10-15-2013
Quote:
Originally Posted by kraljic
I want to skip those files whose names end with a number. How can I do this ?
Code:
$ ls
f1  f2  f3  fa  fb  fc

$ for f in *[!0-9]; do echo $f; done
fa
fb
fc

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 10-15-2013
You were quite close (but allowing only a and b chars, not the entire alphabet):
Code:
for f in /tmp/stage/sharp*[a-z]
 do  echo "Processing $f"
# do something on $f
 done

This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-15-2013
At the risk of being pedantic, I will point out that the set of files not ending in a digit and the set of files ending with a letter may not be identical. Which is the correct set is left for the OP to decide.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 10-15-2013
Absolutely right. I just wanted to point out that he/she stopped his/her experiments close to (somewhat discussible, yes) success.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 10-16-2013
Thank you very much Rudic, Alister. Yes. I mistakenly entered a-b instead a-z. It has been a long day Smilie

Thank you ctsgnb . I encountered the following error for the solution you've provided.

Code:
$ for i in /tmp/stage/sharp!(*[0-9]); do echo $i ; done
-bash: !: event not found

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic FOR loop with break

Oracle Linux : 6.4/bash shell In the below I want to break out of the loop when it enters the 5th iteration. #!/bin/bash for i in 1 2 3 4 5 6 do echo "$i" if echo "Oh Nooo... i = $i. I need to stop the iteration and jump out of the loop" then break fi done But, it only... (3 Replies)
Discussion started by: John K
3 Replies

2. Shell Programming and Scripting

Basic help improving for in loop

I'm obviously very new to this. I'm trying to write a simple for loop that will read the directory names in /Users and then copy a file into the same subdir in each user directory. I have this, and it works but it isn't great. #!/bin/bash HOMEDIRS=/Users/* for dirs in $HOMEDIRS; do if ];... (5 Replies)
Discussion started by: Heath_T
5 Replies

3. UNIX for Dummies Questions & Answers

Basic loop awk/shell script question..

Hi, Sorry if this is a newbie question. I guess you can use either awk or shell script for this sequence of operations, but knowing very little about either of them I'm not sure how I should try to write this. The basic objective is to copy certain files that are scattered all over my... (10 Replies)
Discussion started by: pc2001
10 Replies

4. Shell Programming and Scripting

Basic bash, echo in loop for

Hi, I am trying to make a script to manage log. I want to write the name of the .gz I moved and the date : for i in `ls $replog/*.gz` do echo " $i " `echo $i date +%d:%m:%Y` `echo $datee `>> $replog/mrnet.log mv $i /var/log/vieux-logs done I need to echo... (10 Replies)
Discussion started by: Dabless
10 Replies

5. Shell Programming and Scripting

Unix Shell basic loop massive n00b

hey guys I would really appreciate some help, i need to do a project for a job that requires minimal UNIX scripting and im REALLY stuck basically Im stuck at what i believe is something really simple but i just dont have a clue how to do it efficiently and properly and i REALLY appreciate some... (16 Replies)
Discussion started by: thurft
16 Replies

6. Shell Programming and Scripting

Basic While loop won't exit...

Hi everyone - just like to say great forum...I've learned a lot off here but I just can't figure this one out...(first post) I'm writing a script to monitor a directory and email the latest modified file....(I realize there are better ways than I'm trying here...I don't like copying and pasting... (5 Replies)
Discussion started by: trevthefatty
5 Replies

7. Shell Programming and Scripting

really basic for loop question

sorry for being dumb here, but is there a way my for loop can take an entire line of a file into consideration instead of each word in a line... ill explain if i have a file like this # cat list serial: 23124 hostname: server1 and a script that does this # cat list.sh #!/bin/sh ... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

8. Shell Programming and Scripting

Basic bash 'for loop' usage

Hi! I have a simple question about using a for loop. I'm trying to open up all the zip files in the currect directory with ark, but I am getting the error "bash: syntax error near unexpected token `for $i ; do ark $i ; done ; I looked in the info pages for bash, but I can't seem to figure... (2 Replies)
Discussion started by: Orange Stripes
2 Replies

9. UNIX for Dummies Questions & Answers

A basic question of FOR loop

Hi, have a basic query. Please see the below code: list="one two three" for var in $list ; do echo $var list="nolist" Done Wht if I want to print only first/ last line in the list Eg one & three Regards er_ashu (3 Replies)
Discussion started by: er_ashu
3 Replies
Login or Register to Ask a Question