Skip to Content

bash programming

Substring Replace Strings In A Filename

This nested for loop substring replaces two parts of the filename GagHstqLEOlv5GU5kEDXmmpAoGQ=257-234.msg1. The script was used to restore missing blobs found in a mailbox backup to the live mailstore.


for d in $(find -type d)
do
  for f in $(ls $d)
  do
    f1=${f/*=}
    f2=${f1/msg*/msg}
    echo "mv -f $d/$f $d/$f2"
  done
done

This site was helpful for figuring this out:
http://tldp.org/LDP/abs/html/string-manipulation.html

Replace Newline Characters With Commas Using tr, Not sed

I recently had a 5.0.9 Zimbra mailbox with inconsistencies between metadata (MySQL DB) and blob store (/opt/zimbra/store/...) in which the store was missing blob data referenced in the database.

Once I parsed the zmblobchk report data to retrieve information for the mailbox I wanted to fix, I wanted an efficient way to remove the blob item references from the database.

All commands run as zimbra user.

A snip from zmblobchk's mb8blobcheck.rpt file:

* MailboxGroup=8, mailbox=8, item=397: file not found: 397-2020.msg (delete associated metadata)

Bash Loops

While

Counting to 3 and doing something along the way right on the command line.


# i=1; while [ "$i" -lt 4 ]; do echo $i; i=$(($i+1)); done
1
2
3

The same command in a script:

#!/bin/bash
i=0
while [ "$i" -lt 10 ]
do
   echo $i
   i=$(($i+1))
done

For

Iterate through lines in a file with a for loop on the command line.


# for a in $(cat /tmp/users); do echo $a; done
usera
userb

In a script:

#!/bin/bash
for a in $(cat /tmp/users)
do

Syndicate content