I have written a script to reduce/lower the version number stored in opkg status file using small shell script, This will enable us to force the opkg to install same version of a Debian package again.

Versions stored in the opkg files are as follows,

Version: 00.03.00a002
Version: 2013.07.001
Version: 2013.07.001
Version: 3.12.002

And my script is as shown below, This script would run on the embedded board which is having busybox based sh shell. I have put bash tag to reach more people.

Script edits either 1st version number or 2nd version number..I didn't take third because sometimes that would contain alphanumeric value and I am uncomfortable with handling hex subtraction.


#Small script to Lower firmware version 

stamp=$(date +%s)

#Creating backup file so that we can get back actual version if we want
echo "${BACKUP_OPKG_STATUS_FILE} file created"

#clear original status file

while IFS=' ' read -r line || [ -n "${line}" ];do
    ver_found=$(echo "${line}" | grep -i "Version")

    if [ ! -z "${ver_found}" ];then
        for i in 1 2;do
            ver_extracted=$(echo "${line}" | grep -i "Version" | cut -d ':' -f 2 | cut -d '.' -f ${i})
            if [ ! -z "${ver_extracted}" ];then
                #reduce version number
                if [ "${ver_extracted}" -gt 0 ];then
                    new_version=$(printf "%02d %02d" "${ver_extracted}" "01" | awk '{ printf "%.0f", $1 - $2 }')
                    new_line=$(echo ${line} | sed -e "s:${ver_extracted}:${new_version}:I")
    echo "${line}" >> "${OPKG_STATUS_FILE}"

I wanted to ask

  1. if there is a better method for doing this.
  2. One problem what I observed with my script is 03 is reduced and replaced with 2 instead of 02
    I need to keep versions digits same, any suggestion for that ?
  • \$\begingroup\$ The problem you describe sounds like a bug, which would make this off-topic for CR. \$\endgroup\$
    – BCdotWEB
    Commented Jan 8, 2016 at 13:28
  • \$\begingroup\$ how about better/simpler method any suggestions? \$\endgroup\$
    – ART
    Commented Jan 8, 2016 at 13:57

1 Answer 1


Since you're already using awk in there, it would be better to rewrite all the echo - grep - sed processing with a single awk. In particular, the entire while loop in the middle can be replaced with this:

awk -F '[:. ]' '/^Version: / {
  num1 = $3;
  num2 = $4;
  if (num1 > 0) {
    gsub(num1, num1 - 1);
  } else if (num2 > 0) {
    gsub(num2, num2 - 1);

This produces slightly different (I think better) output than your original, and with the number of sub-processes greatly reduced, it's a lot more efficient.

  • \$\begingroup\$ this doesn't work on busybox shell. :( awk: cmd. line:2: Call to undefined function \$\endgroup\$
    – ART
    Commented Jan 8, 2016 at 16:02
  • 1
    \$\begingroup\$ Hm, that's probably because of the strtonum function I used. I removed it, it's probably not really needed (not for your example data anyway) \$\endgroup\$
    – janos
    Commented Jan 8, 2016 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.