LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2006, 05:51 AM   #1
osio
Member
 
Registered: Jun 2005
Posts: 70

Rep: Reputation: 15
bash simple test with posix character class


I'd like to echo all files containing capital letters in their file names, but the script below echos all the files in the directory!:
Code:
#!/bin/bash
for i in *
   do
      if [[ "Si" != *[[:upper:]]* ]]
      then
         continue
      else
         echo $i "HAS a Capital letter in its filename!"
      fi
   done
What's wrong with the test?
Thanks!
Osio
 
Old 01-21-2006, 06:07 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You probably made a typo.

"S1" should be "$i"

Hope this helps.
 
Old 01-21-2006, 06:26 AM   #3
osio
Member
 
Registered: Jun 2005
Posts: 70

Original Poster
Rep: Reputation: 15
druuna, thanks but no, there is no "1" in the script, I checked again.
If the directory has these files
cApItAl
CaPiTaL
lower
when i run the script above I get:
cApItAl HAS a Capital letter in its filename!
CaPiTaL HAS a Capital letter in its filename!
lower HAS a Capital letter in its filename!
 
Old 01-21-2006, 06:30 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

That's my typo, sorry

"Si" should be "$i" (capital S vs dollar sign)

if [[ "Si" != *[[:upper:]]* ]]
should be
if [[ "$i" != *[[:upper:]]* ]]

Sorry for the confusion.
 
Old 01-21-2006, 06:57 AM   #5
osio
Member
 
Registered: Jun 2005
Posts: 70

Original Poster
Rep: Reputation: 15
Ouch, yes, I should have checked that. Thanks!
I've realised that the script overwrites files if used to rename files: 'CaPiTaL' is moved to 'capital' but 'cApItAl' as well, so the first file converted to lower case is lost.
I wrote the pseudo-code below to assign increasing digits to the multiple occurences of the same filename in lower case but I'm getting nowhere. Nested loops are far beyond my reach:
Code:
#!/bin/bash
for i in *
do if [[ "$i" != *[[:upper:]]* ]] then continue else identical=`ls | grep "$i" | wc` if [ $identical == 1 ] then mv $i $(echo $i | tr [:upper:] [:lower:]) else # whatever, I'm lost here N=0 # still lost mv $i ${i%%*}(N+1) fi done

Last edited by osio; 01-21-2006 at 07:01 AM.
 
Old 01-22-2006, 07:23 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, wc on it's own rtns 3 nums: lines words chars.
What you need is
wc -l
ie num of filenames that match. (pseudo-code)
If result (ie wc -l) > 1, add 1 to result, append num to old_filename and cvt old_filename to lower (ie new filename). Move old file -> new file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with test class pro, from C++ for dummies. RHLinuxGUY Programming 6 11-21-2005 11:08 AM
(linking?) problem compiling very simple array class btb Programming 3 08-02-2005 03:16 PM
Simple webserver test script (bash) somedudeppf Programming 3 01-14-2004 10:30 AM
Bash scripting - add a character to a line. welby Programming 1 01-14-2004 10:09 AM
Python how name variable inside a class - very simple? lugoteehalt Programming 5 10-22-2003 05:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration