HiveBrain v1.2.0
Get Started
← Back to all entries
patternshellMinor

Shell Script Image Replication

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptreplicationimageshell

Problem

I have a shell script which is being used on an embedded human machine interface (HMI). This script is used to copy a few files from a USB stick to a different place on the device, but with multiple instances of the same file under different names.

If it helps:

  • OS is Unix-like (BusyBox v1.11.2)



  • Commands available are located here



  • The filesystem is JFFS2



Any thoughts on improvements/optimizations would be appreciated.

`#! /bin/sh
echo "
**Project Customisation Daemon STARTING**
"
echo "Main Background:"
# First, check the new image exists
if [ -s "/disk/usbsda1/New_Main.png" ]
then
echo " -> Found new file to be used!"
# Check if our directory is already present
if [ -d "/opt/pclient/projekte/Main/" ]
then
# If our directory exists, remove the files
echo " -> Found old directory, removing contents!"
rm -rf /opt/pclient/projekte/Main/*
else
# If the directory isnt present, create it!
echo " -> Creating new directory!"
mkdir -p /opt/pclient/projekte/Main/
fi
# Now copy our files!
echo " -> Copying new files, please wait!"
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page2.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page3.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page4.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page5.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page6.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page7.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page8.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page9.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page10.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main/Main_Page11.png
cp /disk/usbsda1/New_Main.png /opt/pclient/projekte/Main

Solution

A few comments. You make many copies of the files but JFFS2 seems to support hard links, so I would use links instead (see man ln and the ln command in the example below). Also you copy all files into temporary directories and then copy them to the target directory. This is unnecessary - just copy one file to the target directory and then link that to the necessary target file names.

I would probably write it using a loop, if your shell allows that. And I would pass the source and maybe target directories into the script, so you would call it as:

myscript /disk/usbsda1/  /opt/pclient/projekte/default_prj/terminal_files/


My scripting is rusty, but here is a naive script using a loop. Script experts can doubtless improve this. Note that the -x on the first line causes the shell to print each command as it is executed.

#! /bin/sh -x

DISK=$1
TARGET=$2

copy_files()
{
    # called as: copy_files sourcedir sourcefile destdir destfile1 destfile2 ...
        SRCDIR=$1
        SRC=$2
        DESTDIR=$3
        shift; shift; shift
        cp $SRCDIR/$SRC $DESTDIR
        for f in $@
        do
                ln $DESTDIR/$SRC $DESTDIR/$f
        done
        rm $DESTDIR/$SRC
}

copy_files $DISK New_Main.png $TARGET Main_Page.png \
    Main_Page2.png \
    Main_Page3.png

Code Snippets

myscript /disk/usbsda1/  /opt/pclient/projekte/default_prj/terminal_files/
#! /bin/sh -x

DISK=$1
TARGET=$2

copy_files()
{
    # called as: copy_files sourcedir sourcefile destdir destfile1 destfile2 ...
        SRCDIR=$1
        SRC=$2
        DESTDIR=$3
        shift; shift; shift
        cp $SRCDIR/$SRC $DESTDIR
        for f in $@
        do
                ln $DESTDIR/$SRC $DESTDIR/$f
        done
        rm $DESTDIR/$SRC
}

copy_files $DISK New_Main.png $TARGET Main_Page.png \
    Main_Page2.png \
    Main_Page3.png

Context

StackExchange Code Review Q#51217, answer score: 5

Revisions (0)

No revisions yet.