2017-07-06 41 views
0

我需要檢查圖像是否有900x900像素的分辨率和文件名不允許包含_thumb_V巴什腳本IF條件

我試圖用這個做line:
如果圖片的像素爲900x900像素且不包含_v或_thumb =>在文件擴展名之前將單詞_thumb添加到文件名的末尾。

行了它的所有有關:

if file $picture | grep -q 900x900 && ! file $picture | grep -q _thumb && ! file $picture | grep -q _v; 

腳本:

#Change to current .sh directory 
cd -P -- "$(dirname -- "$0")" 
for picture in */*/Templates/*.jpg 
do 
    filename=${picture##*/} 
    filename1=$(echo "$filename" |sed 's/.\{4\}$//') 
    parent_dir="$(dirname -- "$(/usr/local/bin/realpath "$picture")")" 

    #Colors 
    red=`tput setaf 1` 
    green=`tput setaf 2` 
    magenta=`tput setaf 5` 
    reset=`tput sgr0` 


    if file $picture | grep -q 900x900 && ! file $picture | grep -q _thumb && ! file $picture | grep -q _v; 
     then 
      mv -v "$picture" "$parent_dir/"$filename1"_thumb.jpg" 
      echo "${green} [PASS] $filename1 Thumbnail 900x900 found and renamed ${reset}" 
     else 
      echo "${magenta} [WARNUNG] $filename1 contains _thumb already or is a _v picture or isn't 900x900 pixels ${reset}" 
     fi 
+0

問題是它不識別圖片,即使它是900x900並且不包含關鍵字。 在此先感謝 –

+0

建議使用http://www.shellcheck.net/...例如,引用您的變量..你可以嘗試'如果文件'$ picture「| grep -q'900x900''沒有其他條件,看看它是否工作? – Sundeep

回答

0

我看到的幾個問題。 #3,#4和#5是最重要的看看。在您的第一線

    1. 目標殼您需要與done
    2. 完成for循環你需要逃脫$filename1內雙引號。周圍的報價目前沒有引用這個。
    3. 雙引號該行中的變量
    4. 澄清如果您正在尋找圖像大小或只是該文件中有900x900?原因grep -q 900x900找不到圖像大小。正如其他人所提到的,您需要將它與imagemagik identify結合使用。

    隨着修復:

    #!/bin/bash 
    #Change to current .sh directory 
    cd -P -- "$(dirname -- "$0")" 
    for picture in */*/Templates/*.jpg 
    do 
        filename=${picture##*/} 
        filename1=$(echo "$filename" |sed 's/.\{4\}$//') 
        parent_dir="$(dirname -- "$(/usr/local/bin/realpath "$picture")")" 
    
        #Colors 
        red=`tput setaf 1` 
        green=`tput setaf 2` 
        magenta=`tput setaf 5` 
        reset=`tput sgr0` 
    
    
        if file "$picture" | grep -q 900x900 && ! file "$picture" | grep -q _thumb && ! file "$picture" | grep -q _v; 
         then 
          mv -v "$picture" "$parent_dir/\"$filename1\"_thumb.jpg" 
          echo "${green} [PASS] $filename1 Thumbnail 900x900 found and renamed ${reset}" 
         else 
          echo "${magenta} [WARNING] $filename1 contains _thumb already or is a _v picture or isn't 900x900 pixels ${reset}" 
         fi 
    done 
    
  • 0

    不要使用 「文件$圖片| grep的900x900」,因爲你不知道,如果文件名包含900x900。我建議使用命令的識別存在於ImageMagick軟件包像:

    if [[ "900 900" == $(identify -format '%w %h' "$picture") ]] ; then 
        if [[ $picture != *_v.jpg && $picture != _thumb.jpg ]] ; then 
        mv "$picture" "${picture%.jpg}_thumb.jpg" 
        fi 
    fi 
    

    我稍微用假設_V從規範偏離和_thumb似乎只是延長了。我認爲避免像best_view.jpg這樣的意外匹配更加明智。 PS:當文件名或dirnames中有空格時,請檢查您的腳本是否正常工作。