ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm busy with a file rename script. A typical filename that needs renaming consists of two parts (both variable length) separated by '@S' (e.g. 'wim@S1.mpg'); the separator must be changed to '@' so the resulting filename is 'wim@1.mpg'. Filenames that don't contain '@S' don't need to be renamed.
I parse the filename into an array (using tr) which works. I however don't seem to be able to determine the number of elements of the array. It either gives me the length of the element (when I use dash as the shell) or it gives the length 1 for any parsed filename if I use bash. I've tried to change ${#arr[*]} to ${#arr[@]} but that does not help. The rest of the code works (as far as I currently can see); it indeed populates the variable newname with the correct filename (so the array contains multiple elements if the original filename contains '@S'.
Code:
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 pathtoassets"
exit
fi
for f in $1/*.mpg
do
#parse filename
arr=$(echo $f | tr "@S" "\n");
# get number of elements in array
len=${#arr[*]}
echo elements: $len
# create new filename
newname=""
for x in $arr
do
newname=$newname@$x
done
#rename filename
echo "moving $f to $newname"
# mv $f $newname
done
A possible workaround is to compare the filenames to the new filename before moving but I like to know why I can't determine the number of elements in the array.
Thanks in advance
PS running Ubuntu 10.04 for development and it needs to be moved to a Fedora system but I will take that hurdle later.
PPS
There is still a bug in the newname creation but I will solve that myself.
Last edited by Wim Sturkenboom; 07-10-2012 at 02:16 AM.
Thanks; I don't script too often so lack experience on what is available and I live from internet examples
I encountered some issues with the use of 'tr' (it uses each of the characters in the first argument as a separator) so was looking at another solution and indeed dropped the array completely.
Code:
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 pathtoassets"
exit
fi
shopt -s nullglob
for f in $1/*.mpg
do
# generate new filename
newname=${f//"@S"/"@"}
#rename filename
if [ "$f" == "$newname" ]; then
echo "skipping $f"
else
if [ -f $newname ]; then
echo "skipping $f; will not overwrite existing file $newname"
continue
fi
echo "moving $f to $newname"
mv "$f" "$newname"
fi
done
Leaves the question WHY I can't determine the length of the array?
First example (yours) assigns a the string output of the commands to 'arr', which by the way looks like:
Code:
wim
1.mpg
(Your tr replaces each character with a new line, which might also not be what you expected)
The second example uses () to let bash know that you are assigning an array. It will show an answer of only 2 elements but I caution you against your current solution.
Firstly I agree with the above, but as to a solution to your question, you did not create an array, hence you are not getting the results you expect:
...
...
Thanks, that explains it
Quote:
Originally Posted by grail
You may need to be careful with your changes, // and / as a replacement do different things.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.