Bash function to produce variants of images at 2X and 3X resolutions I use ImageMagick to crop image, encapsulate the related code to functions.
Here is the code:
convertc2(){
  convert $1 -crop $2 &quot;two.png&quot;
}


converto3(){


  Delimeter=&quot;placeHolder&quot;

  if [[ $2 == *&quot;x&quot;* ]]; then
    Delimeter=&quot;x&quot;
  else
    Delimeter=&quot;X&quot;
  fi

  First=&quot;${2%$Delimeter*}&quot;
  Fourth=&quot;${2##*+}&quot;
  Lhs=&quot;${2%%+*}&quot;
  Rhs=&quot;${2#&quot;$Lhs+&quot;}&quot;

  Second=&quot;${Lhs##*$Delimeter}&quot;
  Third=&quot;${Rhs%+*}&quot;

  One=$(echo &quot;scale=0 ; $First * 1.5&quot; | bc)
  One=${One%.*}
  Two=$(echo &quot;$Second * 1.5&quot; | bc)
  Two=${Two%.*}
  Three=$(echo &quot;$Third * 1.5&quot; | bc)
  Three=${Three%.*}
  Four=$(echo &quot;$Fourth * 1.5&quot; | bc)
  Four=${Four%.*}

  Final=$One&quot;X&quot;$Two&quot;+&quot;$Three&quot;+&quot;$Four

  echo $Final

  convert $1 -crop $Final &quot;three.png&quot;
}

Usage like this:

convertc2 /Users/dengjiangzhou/Desktop/Simulator\ Screen\ Shot\ -\ iPhone\ 8\ -\ 2019-02-20\ at\ 14.39.02.png   300X300+0+0 

converto3 /Users/dengjiangzhou/Desktop/Simulator\ Screen\ Shot\ -\ iPhone\ 8\ -\ 2019-02-20\ at\ 14.39.02.png    300X300+0+0                  


I need the image size of @2X and @3X. So the function converto3  use convertc2 's size and transform to its size.
Delimeter=&quot;placeHolder&quot;
    
if [[ $2 == *&quot;x&quot;* ]]; then
    Delimeter=&quot;x&quot;
else
    Delimeter=&quot;X&quot;
fi

I really care about how to improve the above code. The &quot;x&quot; , I may type &quot;X&quot; or &quot;x&quot;.
how to use this &quot;${2%$Delimeter*}&quot; insensitive?
And the calculation code in  converto3() is very ugly.
Double-quote variables used as command line arguments

Instead of this:


convert $1 -crop $2 "two.png"



Write like this:

convert "$1" -crop "$2" "two.png"


This is to protect from word splitting and glob expansion.

Double quote... naturally

This is hard to read and confusing:


Final=$One"X"$Two"+"$Three"+"$Four



I suggest to write like this:

Final="${One}X$Two+$Three+$Four"


Use better variable names

It's really hard to make sense of a program that uses variable names like First, Fourth, Second, Lhs, that don't reveal their purpose.

Also, the convention is to use lowercase names, without capitalizing the first letters.

Chopping off characters case insensitively

Instead of detecting if the delimiter is x or X and storing it in a variable,
you could use the pattern [xX], for example:

width=${spec%%[xX]*}


For example if you have 200x300 or 200X300 in spec, width becomes 200.
(Do take note of the meaningful names.)

Floating point math in the shell

Unfortunately Bash doesn't do floating point math.
It would give you a syntax error if you tried to multiply something by 1.5.
On the other hand, if you want to multiply by 1.5, and you don't mind truncating decimal points (as is the case here), you could multiply by 3 and divide by 2.

That is, instead of this:

width=$(bc &lt;&lt;&lt; "$width * 1.5")


You could write:

((width = width * 3 / 2))


Use here strings

Instead of echo ... | cmd, write cmd &lt;&lt;&lt; "...".