Dr. Mark Humphrys

School of Computing. Dublin City University.

Online coding site: Ancient Brain

coders   JavaScript worlds

Search:

Free AI exercises


Sample Shell programs



norm - set permissions as open as possible

chmod  u+rwx,go+rx-w  $*



hide - as hidden as possible

chmod  u+rwx,go-rwx  $*



semihide - just open enough as needed for Web

directories:

chmod  u+rwx,g-rwx,o+x-rw  $*
files (web pages, images, etc):
chmod  u+rwx,g-rwx,o+r-xw  $*
"norm" could of course replace all 3 if you don't mind granting more access than strictly necessary.



lsifexists - silent ls

If we "ls" a list of files - some of which exist and some do not - we get error messages.
e.g. Type this in a Web directory:

 ls -l *html *css *js *php 
But what if one of these groups does not exist.
Can we make a silent ls that does not complain about non-existing files?

Here is a quiet version using a for loop and an if statement:

for i in $*
do
        if test -f $i
        then
         ls -l $i
        fi
done



rmifexists - silent rm

Let's say every day we run a cleanup script that removes old editor .bak files:
rmifexists *.bak
If there are none today, or in this directory, this will give error messages.
Can we make a silent rm that does not complain about non-existing files?

for i in $*
do
        if test -f $i
        then
         rm $i
        fi
done


Question

Why not:

        if test -f $*
        then
         rm $*
        fi


Recursive rm




"Trying out some Deadly Linux Commands".
Includes typing rm -rf / and other scary commands, including the hilarious: mv / /dev/null
See more videos of people typing rm -rf /



A Steam for Linux shell script included a highly dangerous rm -rf line in 2015.
This led, depending how it was run, to entire user filesystems being destroyed.




wipe - clean up editor backup files

rmifexists *%
rmifexists .*% 

rmifexists  *~
rmifexists .*~

rmifexists  *.bak
rmifexists .*.bak

rmifexists  *.BAK
rmifexists .*.BAK
  1. Easier than having to point-and-click each one. Especially if do this for multiple directories.
  2. Safer than typing "rm *bak" every day. One day you will type "rm * bak"
  3. In general, if you regularly type some command that would be dangerous if you make a typo, it would be better to debug it once and put it in a script and never type it directly again.


Exercise

Write a recursive wipe.


Exercise: shake

"shake" - detect if a word exists in Shakespeare
Usage like:
 shake ireland 
  1. Script does a "cd" to the Shakespeare dir
  2. grep (ignoring case) the argument in all the Shakespeare files
  3. send output to junk file or "/dev/null"
  4. check the return code of grep
  5. "if" statement to display if found or not


Command-line image processing

We need libjpeg utilities.
Last time I checked, at DCU this was:

To make 1/4 size versions of 10,000 JPEGs without ever opening an image editor (or doing any work):


  
for i in *jpg
do

 djpeg  -scale 1/4 -bmp $i  > temp.bmp

 cjpeg  temp.bmp            > small.$i

done


JPEG needs to be decoded to a BMP (bitmap), then be re-sized, then re-coded back to JPEG.
Can do this in one line, leaving out the temporary file: Pipe result of djpeg into input of cjpeg.

  


Extract images from PDFs

I was once given an archive of thousands of scanned historical images. They were all inside PDFs.
I automatically extracted all the JPEGs from the PDFs using pdfimages
Last time I checked, at DCU this was:



for i in *pdf					# i = x.pdf
do

  x=`basename "$i" ".pdf"`		# get root filename x (without .pdf bit)

  pdfimages  -j  $i  $x  		# extracts images to x-nnn.jpg

done


# extract images and save as default PPM:
pdfimages file.pdf prefix

# extract images and save as JPEGs:
pdfimages -j file.pdf prefix

# sometimes JPEGs do not extract well, save all images as PNG:
pdfimages -png file.pdf prefix

# just list all images without extract:
pdfimages -list file.pdf  

pdfimages -help

  


Command-line movie processing

You can do command-line movie processing with ffmpeg.

Your Shell script can bulk convert, split, join, rotate and resize thousands of videos. Without opening any window.

Not installed at DCU. Get your own Linux and install it.



ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.      New 250 G VPS server.

Note: Links on this site to user-generated content like Wikipedia are highlighted in red as possibly unreliable. My view is that such links are highly useful but flawed.