LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   next BASH question about arrays with case swapping (https://www.linuxquestions.org/questions/linux-newbie-8/next-bash-question-about-arrays-with-case-swapping-4175426922/)

lleb 09-12-2012 01:21 PM

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.

suicidaleggroll 09-12-2012 04:12 PM

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


lleb 09-12-2012 04:17 PM

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.

lleb 09-12-2012 04:19 PM

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.

suicidaleggroll 09-12-2012 04:23 PM

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.

lleb 09-12-2012 04:34 PM

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 :D.


All times are GMT -5. The time now is 06:39 PM.