Add Me!Close Menu Navigation

My technical corner about Linux, Perl, programming, computer networks and network security

Add Me!Open Categories Menu

Basics of writing bash scripts, improving your command line

Sometimes many commands in linux based systems are too complicated to remember or use them. Many options/prefixes are quite problematic as well. I had had this problem some years ago and I coped with it. I used imagemagick and I needed some improvements to my work because of imagemagick’s syntax.

 

If you know, imagemagick is a great program to edit pictures in the command line. But, one picture is too few for me, so I use this for mass edit many pictures. I used to this command (example for one picture):

convert -scale 100% -quality 100% -bordercolor black -border 20 PICTURE.jpg final/PICTURE.jpg

 

But what if more that one? No problem – a for loop in bash always helps.

for i in *.jpg; do convert -scale 100% -quality 100% -bordercolor black -border 20 $i final/$i; done

 

This isn’t still complicated, but what if I want add a watermark? ;-)

for i in *.jpg; do convert -scale 100% -quality 100% -bordercolor black -border 20 $i final/$i; composite -gravity southeast -geometry +0+5 ~/doc/watermark.png $i $i; done

But wait a moment… does the directory final/ exist in the above example?

 

All of these problems we may solve with our simple script:

### The final/ directory must be, so it will be created
mkdir final &>/dev/null

### Old pictures will be removed
rm -f final/* &>/dev/null

for i in $@; 
do 
   composite -gravity southeast -geometry +0+5 ~/doc/watermark.png $i $i
   convert -scale 100% -quality 100% -bordercolor black -border 20 $i final/$i
done

 

We assume that the script has been saved as pictures.sh. Now we can use it exactly like that: ./pictures.sh *.jpg and it’s all.

You should consider create scripts with commands which are used by you everyday, because these simple improvements may be beneficial for your job. You better know, which commands are too complicated for you.

Leave a Reply

You must be logged in to post a comment.