string comparison in bash
Hello everyone,
I wrote the following scrip to rename some jpg files to what I hope is a better organized way to keeping up with them. Code:
#!/bin/bash for the file and later on I plane to use "read" in the scrip so I can enter other "older dates" for my archive of files. Now, for my problem as you can see the first "if" statement skips the files with the prefix of the current date. This is "not" what I want, but for testing purposes it was what I could understand. I want the first if statement to skip the files that have the "format" of the date command. So any file that has "six numbers" before a "_" will be skipped. Thanks in advance. |
Quote:
Code:
if echo ${file:0:6} | grep -q ^[0-9]*$ Just a side note: you can avoid the second and third if statements if you format the number by padding with leading zeros. One method using printf: Code:
mv "$file" "${x}_n$(printf "%03u" $num).jpg" Code:
num=1001 |
I'm not too good with bash, but you could try `expr`
Code:
expr "$file" : '[0-9]\{6\}_.*.jpg' If you have bash >=3.0 then you can just do Code:
[[ "$file" =~ [0-9]{6}_ ]] |
Thanks,
Those solutions look great. David |
I just had to post the results of the new scrip.
Thanks angrybanana and colucix. Code:
#!/bin/bash Need to study up on the read command to make it bullet proof so I don't enter something stupid. |
All times are GMT -5. The time now is 06:33 AM. |