LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   string comparison in bash (https://www.linuxquestions.org/questions/programming-9/string-comparison-in-bash-615829/)

davimint 01-23-2008 04:18 PM

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
# Scrip to rename jpg files
num=1
x=$(date +%m%d%y)
skip=${x}
for file in *.jpg; do
   
    if [ "${file%_*.jpg}" = "$skip" ]; then
        echo Skipping file $file
        let num++
        continue
    fi         

    if [ $num -le 9 ]; then
        mv "$file" "${x}_n00${num}.jpg"
        echo Moving $file
    fi

    if  [ $num -ge 10 ]; then
        mv "$file" "${x}_n0${num}.jpg"
        echo Moving $file
    fi

    let num++

done

The way I have written the scrip is to use the date as the prefix
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.

colucix 01-23-2008 04:55 PM

Quote:

Originally Posted by davimint (Post 3033042)
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.

I would extract the first six characters from the file name and then be sure they are all numbers using grep in this way:
Code:

if echo ${file:0:6} | grep -q ^[0-9]*$
then
    echo first six chars are numbers
else
    echo nope
fi

or something similar.

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"
Another method using an artifact:
Code:

num=1001
mv "$file" "${x}_n${num:1:3}.jpg"


angrybanana 01-23-2008 04:57 PM

I'm not too good with bash, but you could try `expr`
Code:

expr "$file" : '[0-9]\{6\}_.*.jpg'
that will evaluate to something other then 0 if true.

If you have bash >=3.0 then you can just do
Code:

[[ "$file" =~ [0-9]{6}_ ]]

davimint 01-23-2008 05:03 PM

Thanks,
Those solutions look great.

David

davimint 01-23-2008 06:36 PM

I just had to post the results of the new scrip.

Thanks angrybanana and colucix.

Code:

#!/bin/bash
#
# Scrip to change jpg files for my cannon snapshot
#
num=1
echo "Enter you six digit month code"

read x

for file in *.jpg; do

  if echo ${file:0:6} | grep -q ^[0-9]*$; then
        echo skipping file  $file
        let num++
        continue
  else
        mv "$file" "${x}_n$(printf "%03u" $num).jpg"
        echo moving $file to "${x}_n$(printf "%03u" $num).jpg"
    fi
    let num++

done

Everything works great

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.