| 1 | #!/bin/bash |
| 2 | # cvt.sh: |
| 3 | # Converts all the MacPaint image files in a directory to "pbm" format. |
| 4 | |
| 5 | # Uses the "macptopbm" binary from the "netpbm" package, |
| 6 | #+ which is maintained by Brian Henderson ([email protected]). |
| 7 | # Netpbm is a standard part of most Linux distros. |
| 8 | |
| 9 | OPERATION=macptopbm |
| 10 | SUFFIX=pbm # New filename suffix. |
| 11 | |
| 12 | if [ -n "$1" ] |
| 13 | then |
| 14 | directory=$1 # If directory name given as a script argument... |
| 15 | else |
| 16 | directory=$PWD # Otherwise use current working directory. |
| 17 | fi |
| 18 | |
| 19 | # Assumes all files in the target directory are MacPaint image files, |
| 20 | #+ with a ".mac" filename suffix. |
| 21 | |
| 22 | for file in $directory/* # Filename globbing. |
| 23 | do |
| 24 | filename=${file%.*c} # Strip ".mac" suffix off filename |
| 25 | #+ ('.*c' matches everything |
| 26 | #+ between '.' and 'c', inclusive). |
| 27 | $OPERATION $file > "$filename.$SUFFIX" |
| 28 | # Redirect conversion to new filename. |
| 29 | rm -f $file # Delete original files after converting. |
| 30 | echo "$filename.$SUFFIX" # Log what is happening to stdout. |
| 31 | done |
| 32 | |
| 33 | exit 0 |
| 34 | |
| 35 | # Exercise: |
| 36 | # -------- |
| 37 | # As it stands, this script converts *all* the files in the current |
| 38 | #+ working directory. |
| 39 | # Modify it to work *only* on files with a ".mac" suffix. |
| 40 | |
| 41 | |
| 42 | |
| 43 | # *** And here's another way to do it. *** # |
| 44 | |
| 45 | #!/bin/bash |
| 46 | # Batch convert into different graphic formats. |
| 47 | # Assumes imagemagick installed (standard in most Linux distros). |
| 48 | |
| 49 | INFMT=png # Can be tif, jpg, gif, etc. |
| 50 | OUTFMT=pdf # Can be tif, jpg, gif, pdf, etc. |
| 51 | |
| 52 | for pic in *"$INFMT" |
| 53 | do |
| 54 | p2=$(ls "$pic" | sed -e s/\.$INFMT//) |
| 55 | # echo $p2 |
| 56 | convert "$pic" $p2.$OUTFMT |
| 57 | done |
| 58 | |
| 59 | exit $? |