LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 11-05-2007, 12:55 PM   #1
aliasofmike
LQ Newbie
 
Registered: Nov 2007
Posts: 4

Rep: Reputation: 0
Bash Shell Scripting - using ls into array


User is new to shell scripting

This script is passed two arguements, a filename pattern and a prefix. The problem seems to be in passing an arguement with wildcards (I had it working fine when the wildcards were in the script itself). This is a simple task, but I need this same methodology to improve a script for generating fade-transitions for key frame animations.

<code>
#!/bin/sh

list=(`ls "$1"`)
#echo ${list[@]}
for filename in "${list[@]}"
do
mv $filename $2"_"$filename
done
</code>

A sample call would go as follows

$ rnmpp *uhood-casc*png 13

Thanks for any help, I'm a new linux user but I love it!
 
Old 11-05-2007, 02:20 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,500

Rep: Reputation: 285Reputation: 285Reputation: 285
Just use the rename command. For your example:

rename "" "$2_" $1

Which for your example parameters would be:

rename "" "13_" *uhood-casc*png

Last edited by macemoneta; 11-05-2007 at 02:21 PM.
 
Old 11-05-2007, 02:25 PM   #3
aliasofmike
LQ Newbie
 
Registered: Nov 2007
Posts: 4

Original Poster
Rep: Reputation: 0
thanks for the tip macemoneta

the problem is that I need to use this kind of thing in other instances. From the current directory, I want to list the files of a certian designation, and assign each filename to an element in an array. Simply using the `ls *$1*png` will not work, because in these cases the interpreter runs this command every time through the for loop (and files that have been removed or renamed no longer show up, often immeadiately breaking the loop). There is clearly some subtlety in the syntax that I am overlooking.

Again, thanks for the help.

For example, I have a directory of many png files, and in particular I wish to rename all files begining with "final" to be prepended. If I use

list=`ls $1*png`

and pass "final" it will succesfully insert each matching png filenames into the array
however, if i use:

list=`ls $1`

and pass "final*png" it only hits the first matching file

Last edited by aliasofmike; 11-05-2007 at 02:36 PM.
 
Old 11-05-2007, 02:37 PM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,500

Rep: Reputation: 285Reputation: 285Reputation: 285
Then you would:

rename "final" "$2_final" final*png

Read 'man rename'.
 
Old 11-05-2007, 02:44 PM   #5
aliasofmike
LQ Newbie
 
Registered: Nov 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks macemoneta, the rename command will absolutely fill the needs of the script I've given as an example.

But I need to be able to do this particular thing in many other instances...not renaming files, but listing files that fit passed criteria and putting their names into an array. I imagine my error involves how the wildcard would be interepreted in the statement list=`ls $1` if the wildcards are part of the passed arguement.
Sorry for not being more clear before.

To test the problem I'm having, I made a test script as follows.

#!/bin/sh

ls $1

If I call test final*png it only prints the very first matching filename, as opposed to the list of matching filenames.

I know this must be a rudimentary mistake, so I apologize for wasting your time.

Last edited by aliasofmike; 11-05-2007 at 02:52 PM.
 
Old 11-05-2007, 03:00 PM   #6
aliasofmike
LQ Newbie
 
Registered: Nov 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Well, I've come to the real issue.

$ test final*png

will pass the function all filenames that match the description, the substitution is of course done immeadiately, instead of passing the literal arguement to $1.

The only solution I know would be to require this syntax for calling the script

$ test "final*png"

Thanks for all your help.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script split array robertngo Programming 13 06-19-2011 11:01 PM
Help with Bash shell scripting HLM01 Linux - Newbie 7 01-31-2008 04:23 PM
My first bash at shell scripting (sorry...) Daws Linux - Newbie 5 03-21-2007 06:20 PM
Bash Scripting - Accessing Array Indices Jus144tice Programming 6 10-22-2006 09:20 PM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 05:25 PM


All times are GMT -5. The time now is 11:17 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration