#!/bin/bash
#########################################################
#### One line basic loop examples and batch commands ####
#########################################################

####################################################
#### C type loop one line. Introducing variables: i
#### All variables are strings
for((i=0; i < 100; i++)) ; do echo "$i: No hablaré en clase." ; done
#### C type loop scriptable
for((i=0; i < 100; i++)) ; do
    echo "$i: No hablaré en clase."
done

#############################################################
#### An infinite 'while' loop and a variable; true and false
Message="Another period of 10 seconds"
while [ 1 ] ; do
  sleep 10
  echo $Message
done

###########################################################################################
#### 'for' loops with list; using variables expansion; ImageMagick suite (convert program)
#### Variables expansion short manual (man bash --> <shift>/##<enter>):
#### ${variable#word}
#### ${variable##word}
####     Remove matching prefix pattern ('word' is expanded and matched against the expanded value of variable). 
####     If the pattern matches, the  beginning  of the  value of variable, then the result of the expansion is the
####     expanded value of variable with the shortest  matching  pattern (the  ``#''  case)  or  
####     the longest matching pattern (the ``##'' case) deleted.  
#### ${variable%word}
#### ${variable%%word}
####     Remove matching suffix pattern  ('word' is expanded and matched against the expanded value of variable). 
####     If the pattern matches  a trailing  portion of the  expanded value of variable, then the result of the 
####     expansion is the expanded value of variable with the shortest matching  pattern (the  ``%''  case) or
####     the longest matching pattern (the ``%%'' case) deleted.  
for ff in *.jpg ; do convert $ff -colorspace Gray ${ff%.jpg}-BN.jpg ; done     #### A Batch command: Convert pictures to gray
for ff in *.jpg ; do convert $ff -scale 25%x25% ${ff%.jpg}-Red.jpg ; done      #### A Batch command: Reduce the size of pictures

###############################################################
#### 'for' loops with list emulating C type loops
for i in `seq 1 2 100` ; do echo "$i is an odd number" ; done

##########################################################################################################################
#### A Batch command: Numbering filenames (the first instruction is a test to check that we are not going to make a mess)
#### Bash arithmetic: Since all variables are strings to do aritmetic implies converting strings to numbers
NuM=0; for ff in *.jpg ; do NuM=$(( $NuM + 1 )) ; echo ${ff%.jpg}-${NuM}.jpg ; done
NuM=0; for ff in *.jpg ; do NuM=$(( $NuM + 1 )) ; mv -v $ff ${ff%.jpg}-${NuM}.jpg ; done
NuM=0; for ff in *.jpg ; do NuM=$(( $NuM + 1 )) ; mv -v $ff GenericBasename-${NuM}.jpg ; done       #### alternative version with a generic name

