LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-12-2012, 02:21 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
next BASH question about arrays with case swapping


i have a small script that uses tf and an array that is supposed to change the case of files, it prints out on the screen, but does not mv the file name from lowercase to uppercase as i desire. I have tried putting the mv command in the script, but i broke things.

Code:
#!/bin/bash

ls deaopts.*.orig > ORIG
FILES=`cat ORIG`

filemove ()

{
for f in "${FILES[@]}"
do
        echo $f | tr '[:lower:]' '[:upper:]'
done
}

filemove
Code:
[103236_rx30@rx30 ~]$ ls -la DEA*
-rwxrwxrwx 1 rx30 group 152 Sep 12 14:14 DEA-MOVE
[103236_rx30@rx30 ~]$ ls -la deao*
-rwxrwxr-x 1 rx30 group 183 Sep 12 11:12 deaopts.NY
-rwxrwxr-x 1 rx30 group  40 Mar  8  2012 deaopts.NY.orig
-rwxrwxr-x 1 rx30 group 183 May 16 10:28 deaopts.OK
-rwxrwxr-x 1 rx30 group 183 Sep 12 12:35 deaopts.cnf
-rwxrwxr-x 1 rx30 group  40 Dec 29  2011 deaopts.cnf.orig
-rwxrwxr-x 1 rx30 group  23 Mar  8  2012 deaopts.fil
[103236_rx30@rx30 ~]$ sh -xvv DEA-MOVE
#!/bin/bash

ls deaopts.*.orig > ORIG
+ ls deaopts.NY.orig deaopts.cnf.orig
FILES=`cat ORIG`
cat ORIG
++ cat ORIG
+ FILES='deaopts.NY.orig
deaopts.cnf.orig'

filemove ()

{
for f in "${FILES[@]}"
do
        echo $f | tr '[:lower:]' '[:upper:]'
done
}

filemove
+ filemove
+ for f in '"${FILES[@]}"'
+ echo deaopts.NY.orig deaopts.cnf.orig
+ tr '[:lower:]' '[:upper:]'
DEAOPTS.NY.ORIG DEAOPTS.CNF.ORIG
[103236_rx30@rx30 ~]$ ls -la DEA*
-rwxrwxrwx 1 rx30 group 152 Sep 12 14:14 DEA-MOVE
[103236_rx30@rx30 ~]$ ls -la deao*
-rwxrwxr-x 1 rx30 group 183 Sep 12 11:12 deaopts.NY
-rwxrwxr-x 1 rx30 group  40 Mar  8  2012 deaopts.NY.orig
-rwxrwxr-x 1 rx30 group 183 May 16 10:28 deaopts.OK
-rwxrwxr-x 1 rx30 group 183 Sep 12 12:35 deaopts.cnf
-rwxrwxr-x 1 rx30 group  40 Dec 29  2011 deaopts.cnf.orig
-rwxrwxr-x 1 rx30 group  23 Mar  8  2012 deaopts.fil
nothing changed... I am after changing the *.orig into caps.

Thanks for pointers.
 
Old 09-12-2012, 05:12 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Your script is going about the file names in a very roundabout way (ls to a file, cat the file to a variable, then loop over the names in the variable). There is also no move command, so of course nothing is being moved

Code:
#!/bin/bash

for f in deaopts.*.orig; do
   newf=$(echo $f | tr '[:lower:]' '[:upper:]')
   mv "$f" "$newf"
done

Last edited by suicidaleggroll; 09-12-2012 at 05:13 PM.
 
1 members found this post helpful.
Old 09-12-2012, 05:17 PM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
ahh so much faster then what I was trying to peace together. setting that up now.

good to know that I can just use the deaopts.*.orig right in the for do done loop.
 
Old 09-12-2012, 05:19 PM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Code:
[103236_rx30@rx30 ~]$ sh -xvv DEA-MOVE
#!/bin/bash

filemove ()

{
for f in deaopts.*.orig
do
        newf=$(echo $f | tr '[:lower:]' '[:upper:]')
        mv "$f" "$newf"
done
}

filemove
+ filemove
+ for f in 'deaopts.*.orig'
echo $f | tr '[:lower:]' '[:upper:]'
++ echo deaopts.NY.orig
++ tr '[:lower:]' '[:upper:]'
+ newf=DEAOPTS.NY.ORIG
+ mv deaopts.NY.orig DEAOPTS.NY.ORIG
+ for f in 'deaopts.*.orig'
echo $f | tr '[:lower:]' '[:upper:]'
++ echo deaopts.cnf.orig
++ tr '[:lower:]' '[:upper:]'
+ newf=DEAOPTS.CNF.ORIG
+ mv deaopts.cnf.orig DEAOPTS.CNF.ORIG
[103236_rx30@rx30 ~]$ d DEA*
-rwxrwxrwx 1 rx30 group 135 Sep 12 17:19 DEA-MOVE*
-rwxrwxr-x 1 rx30 group  40 Dec 29  2011 DEAOPTS.CNF.ORIG*
-rwxrwxr-x 1 rx30 group  40 Mar  8  2012 DEAOPTS.NY.ORIG*
Flawless, thank you very much.
 
Old 09-12-2012, 05:23 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Glad it worked out.

FYI - you don't need that function declaration and call either. It's not hurting anything, it's just not necessary.
 
Old 09-12-2012, 05:34 PM   #6
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
ok. thanks. still wrapping my head around building arrays. next step is to take a list of deaopts.XX file names, cat out a small portion of those files and use that to build an other array to move files around.

ex:

find all of the files in the directory deaopts.*
make an array of those files.
set - `cat deaopts.XX`
find ${1:3:3}.* -mtime +15 -exec mv '{}' /foo \; >/dev/null
wash repeat for all deaopts.* in the directory.

that can wait until tomorrow .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] BASH Arrays dsartain Programming 15 05-08-2012 01:39 PM
[SOLVED] BASH Question - nested case statements jbeiter Linux - Software 2 02-13-2012 11:08 AM
bash arrays question introuble Programming 1 05-20-2006 04:07 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 06:40 AM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 10:57 AM


All times are GMT -5. The time now is 04:22 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration