Command Line Tidbits

programming

Image

Command line tidbits

The linux command line is a powerful tool to accomplish things. Here we collect some random usage.

Too many options

I tend to create or use command line scripts with too many options. This is a very well known problem. Various solutions exist.

For the purpose of illustration consider the following command from Imagemagick. Note that he site illustrates the version 7 command magick, for version 6 use convert.

convert -size 320x90 canvas:none -stroke snow4 -size 1x90 -tile gradient:white-snow4 \
  -draw 'roundrectangle 16, 5, 304, 85 20,40' +tile -fill snow \
  -draw 'roundrectangle 264, 5, 304, 85  20,40' -tile gradient:chartreuse-green \
  -draw 'roundrectangle 16,  5, 180, 85  20,40' -tile gradient:chartreuse1-chartreuse3 \
  -draw 'roundrectangle 140, 5, 180, 85  20,40' +tile -fill none \
  -draw 'roundrectangle 264, 5, 304, 85 20,40' -strokewidth 2 \
  -draw 'roundrectangle 16, 5, 304, 85 20,40' \( +clone -background snow4 \
  -shadow 80x3+3+3 \) +swap -background none -layers merge \( +size -pointsize 90 \
  -strokewidth 1 -fill red label:'50 %' -trim +repage \( +clone -background firebrick3 \
  -shadow 80x3+3+3 \) +swap -background none -layers merge \) -insert 0 -gravity center \
  -append -background white -gravity center -extent 320x200 cylinder_shaded.png

When executed, it creates an image: cylinder_shaded.png

Imagemagick created shaded cylinder

A single command script

  1. save the cmd to file
  2. make the file executable
  3. execute the file
$ echo $CMD > magick.cmd
$ chmod u+x magick.cmd
$ ./magick.cmd

Alias

Capture your command line in an alias.

$ alias make.cyl=$CMD
$ make.cyl

Possibly save the alias to a file, which can be sourced later.

$ echo 'alias make.cyl=$CMD' > magick.alias
$ source magick.alias
$ make.cyl

Makefile

Simply capture your command line in a Makefile and run the target on the command line, as make cylinder_shaded.png, simply make

cat Makefile

default: all

all: cylinder_shaded.png

cylinder_shaded.png:
    convert -size 320x90 canvas:none -stroke snow4 -size 1x90 -tile gradient:white-snow4 \
    -draw 'roundrectangle 16, 5, 304, 85 20,40' +tile -fill snow \
    -draw 'roundrectangle 264, 5, 304, 85  20,40' -tile gradient:chartreuse-green \
    -draw 'roundrectangle 16,  5, 180, 85  20,40' -tile gradient:chartreuse1-chartreuse3 \
    -draw 'roundrectangle 140, 5, 180, 85  20,40' +tile -fill none \
    -draw 'roundrectangle 264, 5, 304, 85 20,40' -strokewidth 2 \
    -draw 'roundrectangle 16, 5, 304, 85 20,40' \( +clone -background snow4 \
    -shadow 80x3+3+3 \) +swap -background none -layers merge \( +size -pointsize 90 \
    -strokewidth 1 -fill red label:'50 %' -trim +repage \( +clone -background firebrick3 \
    -shadow 80x3+3+3 \) +swap -background none -layers merge \) -insert 0 -gravity center \
    -append -background white -gravity center -extent 320x200 $@