shell; number of elements in array
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 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. |
Code:
for f in *@S1*;do echo mv $f ${f/@S/@} ;done If you are using shell arrays, it's time to think again. |
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 |
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:
Code:
arr=$(echo $f | tr "@S" "\n") Code:
wim 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. |
You may need to be careful with your changes, // and / as a replacement do different things.
|
Quote:
Quote:
I indeed want to replace every '@S' by '@'. Thanks again, billy and grail |
No you are correct ... just didn't want you to get caught with another not predicted result :)
|
The single slash gave me the unpredicted result :D Thanks
|
All times are GMT -5. The time now is 08:38 PM. |