Register
It is currently Sat Nov 01, 2014 8:02 am

Embedded if in for not responding


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Nov 19, 2011 9:20 am   

Joined: Sat Nov 19, 2011 9:12 am
Posts: 2
I have files named as below. I want to echo only file names less than 10. But there is no output.

$ ls
1 10 11 12 13 14 2 3 4 5 6 7 8 9
$ for i in *; do (if["$i" -lt 10] then echo "$i" fi); done
$
$

AND:

How can I rename the same by using above type cmd:
01 02 03 04 ...... 14

Thanks in advance.


Top
 Profile  
 PostPosted: Sat Nov 19, 2011 9:52 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
makh wrote:
I want to echo only file names less than 10.


Best approach:
Code:
for f in [1-9]; do echo "$f"; done



The syntax you're using is wrong. Instead of:
Code:
for i in *; do (if["$i" -lt 10] then echo "$i" fi); done

you'd write:

Code:
for i in *; do if [ "$i" -lt 10 ]; then echo "$i"; fi; done


[ is a command itself, it's just a shortcut to test(0) ; if is a shell keyword.


By following your idea, you can even write the condition with (( ... )) bash(1)'s syntax, like this:
Code:
for i in *; do if ((i < 10)); then echo "$i"; fi; done


----

makh wrote:
How can I rename the same by using above type cmd:
01 02 03 04 ...... 14


Well, since you know that each filename is simply an integer, then just add a zero to the left of the variable
that you're using to loop through your files, thanks to mv(1).

Code:
for f in [1-9]; do mv "$f" "0$f"; done


Last edited by jsz on Sat Nov 19, 2011 9:56 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Sat Nov 19, 2011 9:56 am   

Joined: Sat Nov 19, 2011 9:12 am
Posts: 2
Thanks a lot .... solved for me.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP