LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-09-2014, 11:15 AM   #1
mhlevy
LQ Newbie
 
Registered: Sep 2014
Posts: 5

Rep: Reputation: Disabled
Unhappy Hoping for help with a shell script that will allow me to display all shared folders


Hi All,

If you're not familiar with Zimbra, it's a collection of Open Source software, all integrated together into a single Email & Collaboration package. It includes Apache, Jetty, Java, Postfix, Amavis, ClamAV, MySQL, Lucene, OpenLDAP, and more...

While most day to day management is done through a graphical admin console, there are many tasks that are better and easier to do using the CLI fron Linux. While I can use the admin console to list all the shared folders, calendars, and contact lists for a user, the problem is that I need to perform this for roughly 1500 mailboxes. I'm having some problems getting the output formatted properly. First off, a quick note on the commands being used from the command line & their output:

For instance, to get a list of all the folders in a mailbox (including calendars and contact lists), the command is "zmmailbox ... gaf" "Get All Folders"
Code:
$ zmmailbox -z -m pop3.test@domain.name gaf
        Id  View      Unread   Msg Count  Path
----------  ----  ----------  ----------  ----------
         1  unkn           0           0  /
        16  docu           0           0  /Briefcase
        10  appo           0           0  /Calendar
        14  mess           0           0  /Chats
         7  cont           0           1  /Contacts
         6  mess           0           0  /Drafts
        13  cont           0           3  /Emailed Contacts
         2  mess           8           8  /Inbox
       386  mess           7           9  /Inbox/Shared With Mark
         4  mess           0           0  /Junk
         5  mess           0           7  /Sent
        15  task           0           0  /Tasks
       340  appo           0           7  /Test1
         3  unkn           0           5  /Trash
       366  mess           2           2  /Imap Test's Folder 1 (imap.test@domain.name:280)
       367  mess           2           2  /Imap Test's Folder 2 (imap.test@domain.name:281)
84bc3505-5  mess           7           9  /Imap Test's Folder 2/Folder 2a
       365  cont           0          83  /Imap Test's Gmail Contacts (imap.test@domain.name:3227)
       364  appo           0        1460  /Imap Test's Imported Calendar (imap.test@domain.name:283)
       399  appo           0           1  /mhlevy's Calendar Shared With Pop3 (mhlevy@domain.name:158627)
The folders, such as "/Imap Test's Folder 1 (imap.test@domain.name:280) indicate this is another user's folder (imap.test) that was shared with this user (pop3.test).

Once we have the list of folders, then each folder can be checked to see if share rights have been granted, for example, the folder "/Inbox/Shared With Mark" was shared, and the folder share grants can be displayed with the GFG (Get Folder Grants) option:

Code:
$ zmmailbox -z -m pop3.test@domain.name gfg "/Inbox/Shared With Mark"
Permissions      Type  Display
-----------  --------  -------
     rwidxa   account  mhlevy@domain.name
But I'm having trouble just getting an accurate list of folders. I've tried to come up with a script (for testing, just using this single test user, though eventually I'll loop through all the users in the email system), and quite frankly, the embedded spaces are vexxing me!

I considered trying to replace the embedded spaces with '_' (underscores), that I could later substitute back to a space using sed, but then I learned that many users use the underscore in their folder names.

Code:
#!/bin/bash
#  getFolderGrants
echo Getting Folder Share Grants...
for FOLDER in `zmmailbox -z -m pop3.test@domain.name  gaf | awk -v nr=5 '{ for (x=nr; x<=NF; x++) {printf $x " "; }; print " " }' | egrep -v ':|Count|----------'`
do
echo The folder is $FOLDER
done

$ ./getFolderGrants 
Getting Folder Share Grants...
The folder is /
The folder is /Briefcase
The folder is /Calendar
The folder is /Chats
The folder is /Contacts
The folder is /Drafts
The folder is /Emailed
The folder is Contacts
The folder is /Inbox
The folder is /Inbox/Shared
The folder is With
The folder is Mark
The folder is /Junk
The folder is /Sent
The folder is /Tasks
The folder is /Test1
The folder is /Trash
The folder is /Imap
The folder is Test's
The folder is Folder
The folder is 2/Folder
The folder is 2a
** For instance, what I'm getting is:
Code:
The folder is /Inbox/Shared
The folder is With
The folder is Mark
** But what I'm trying to get is:
Code:
The folder is /Inbox/Shared With Mark
I thought of embedding the zmmailbox command in double quotes, but that didn't really help either, printing out everything on a single line...

The problem seems to be that awk is seeing the embedded spaces as cause to begin a new line, and no matter what I try, I seem to be getting the same thing; For instance:

Code:
#!/bin/bash
echo Getting Folder Share Grants...
for FOLDER in `zmmailbox -z -m pop3.test@domain.name  gaf | egrep -v ':|Count|----------' | awk '{ for (i=5; i<=NF; i++) printf("%s "),$i; print""}'`
do
echo The folder is $FOLDER
done

$ ./getFolderGrants
Getting Folder Share Grants...
The folder is /
The folder is /Briefcase
The folder is /Calendar
The folder is /Chats
The folder is /Contacts
The folder is /Drafts
The folder is /Emailed
The folder is Contacts
The folder is /Inbox
The folder is /Inbox/Shared
The folder is With
The folder is Mark
The folder is /Junk
The folder is /Sent
The folder is /Tasks
The folder is /Test1
The folder is /Trash
The folder is /Imap
The folder is Test's
The folder is Folder
The folder is 2/Folder
The folder is 2a
$
Any help would be VERY much appreciated!

Thanks!

Mark

Last edited by mhlevy; 10-09-2014 at 02:40 PM. Reason: revised with code tags for clarity
 
Old 10-09-2014, 11:20 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,211

Rep: Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612
I can help with this. Can you edit that post and wrap your code in the CODE tags to make it easily readable?

Code:
# like this
cat something
 
Old 10-09-2014, 11:30 AM   #3
mhlevy
LQ Newbie
 
Registered: Sep 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Sorry about that...

If you're not familiar with Zimbra, it's a collection of Open Source software, all integrated together into a single Email & Collaboration package. It includes Apache, Jetty, Java, Postfix, Amavis, ClamAV, MySQL, Lucene, OpenLDAP, and more...

While most day to day management is done through a graphical admin console, there are many tasks that are better and easier to do using the CLI fron Linux. While I can use the admin console to list all the shared folders, calendars, and contact lists for a user, the problem is that I need to perform this for roughly 1500 mailboxes. I'm having some problems getting the output formatted properly. First off, a quick note on the commands being used from the command line & their output:

For instance, to get a list of all the folders in a mailbox (including calendars and contact lists), the command is "zmmailbox ... gaf" "Get All Folders"
Code:
$ zmmailbox -z -m pop3.test@domain.name gaf
Id View Unread Msg Count Path
---------- ---- ---------- ---------- ----------
1 unkn 0 0 /
16 docu 0 0 /Briefcase
10 appo 0 0 /Calendar
14 mess 0 0 /Chats
7 cont 0 1 /Contacts
6 mess 0 0 /Drafts
13 cont 0 3 /Emailed Contacts
2 mess 8 8 /Inbox
386 mess 7 9 /Inbox/Shared With Mark
4 mess 0 0 /Junk
5 mess 0 7 /Sent
15 task 0 0 /Tasks
340 appo 0 7 /Test1
3 unkn 0 5 /Trash
366 mess 2 2 /Imap Test's Folder 1 (imap.test@domain.name:280)
367 mess 2 2 /Imap Test's Folder 2 (imap.test@domain.name:281)
84bc3505-5 mess 7 9 /Imap Test's Folder 2/Folder 2a
365 cont 0 83 /Imap Test's Gmail Contacts (imap.test@domain.name:3227)
364 appo 0 1460 /Imap Test's Imported Calendar (imap.test@domain.name:283)
399 appo 0 1 /mhlevy's Calendar Shared With Pop3 (mhlevy@domain.name:158627)
The folders, such as "/Imap Test's Folder 1 (imap.test@domain.name:280) indicate this is another user's folder (imap.test) that was shared with this user (pop3.test).

Once we have the list of folders, then each folder can be checked to see if share rights have been granted, for example, the folder "/Inbox/Shared With Mark" was shared, and the folder share grants can be displayed with the GFG (Get Folder Grants) option:

Code:
$ zmmailbox -z -m pop3.test@domain.name gfg "/Inbox/Shared With Mark"
Permissions Type Display
----------- -------- -------
rwidxa account mhlevy@domain.name
But I'm having trouble just getting an accurate list of folders. I've tried to come up with a script (for testing, just using this single test user, though eventually I'll loop through all the users in the email system), and quite frankly, the embedded spaces are vexxing me!

I considered trying to replace the embedded spaces with '_' (underscores), that I could later substitute back to a space using sed, but then I learned that many users use the underscore in their folder names.

Code:
#!/bin/bash
# getFolderGrants
echo Getting Folder Share Grants...
for FOLDER in `zmmailbox -z -m pop3.test@domain.name gaf | awk -v nr=5 '{ for (x=nr; x<=NF; x++) {printf $x " "; }; print " " }' | egrep -v ':|Count|----------'`
do
echo The folder is $FOLDER
done

$ ./getFolderGrants 
Getting Folder Share Grants...
The folder is /
The folder is /Briefcase
The folder is /Calendar
The folder is /Chats
The folder is /Contacts
The folder is /Drafts
The folder is /Emailed
The folder is Contacts
The folder is /Inbox
The folder is /Inbox/Shared
The folder is With
The folder is Mark
The folder is /Junk
The folder is /Sent
The folder is /Tasks
The folder is /Test1
The folder is /Trash
The folder is /Imap
The folder is Test's
The folder is Folder
The folder is 2/Folder
The folder is 2a
** For instance, what I'm getting is:
Code:
The folder is /Inbox/Shared
The folder is With
The folder is Mark
** But what I'm trying to get is:
Code:
The folder is /Inbox/Shared With Mark
I thought of embedding the zmmailbox command in double quotes, but that didn't really help either, printing out everything on a single line...

The problem seems to be that awk is seeing the embedded spaces as cause to begin a new line, and no matter what I try, I seem to be getting the same thing; For instance:

Code:
#!/bin/bash
echo Getting Folder Share Grants...
for FOLDER in `zmmailbox -z -m pop3.test@domain.name gaf | egrep -v ':|Count|----------' | awk '{ for (i=5; i<=NF; i++) printf("%s "),$i; print""}'`
do
echo The folder is $FOLDER
done

$ ./getFolderGrants
Getting Folder Share Grants...
The folder is /
The folder is /Briefcase
The folder is /Calendar
The folder is /Chats
The folder is /Contacts
The folder is /Drafts
The folder is /Emailed
The folder is Contacts
The folder is /Inbox
The folder is /Inbox/Shared
The folder is With
The folder is Mark
The folder is /Junk
The folder is /Sent
The folder is /Tasks
The folder is /Test1
The folder is /Trash
The folder is /Imap
The folder is Test's
The folder is Folder
The folder is 2/Folder
The folder is 2a
$
Any help would be VERY much appreciated!

Thanks!

Mark
 
Old 10-10-2014, 01:45 PM   #4
mhlevy
LQ Newbie
 
Registered: Sep 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
I had to use a temp file in order to make it work

Due to the way the "for" loop tended to treat whitespace as a delimiter, the only way I was able to figure out how to do this was to create a temp file: Once I did that, I was able to use the "read" command to deal with the entire line as the variable.

Code:
#!/bin/bash
clear
echo Getting Folder Share Grants...
for USER in pop3.test@domain.net imap.test@domain.net
do
  echo
  zmmailbox -z -m $USER gaf > /tmp/shares$$
  echo Mailbox folders for $USER
  cat /tmp/shares$$
  echo ; echo Accepted shared folders from others:
  grep : /tmp/shares$$ | cut -c 43-
  echo
  zmmailbox -z -m $USER gaf | egrep -v ':|Count|----------' | cut -c 43- > /tmp/shares$$
  cat /tmp/shares$$ |while read FOLDER 
    do
      echo Grants for $USER: $FOLDER:
      zmmailbox -z -m $USER gfg "$FOLDER"
      echo
    done
  echo
done
rm -f /tmp/shares$$
Unfortunately, the Zimbra "zmmailbox" command doesn't return an exit code, I am forced to list the folders with no shares granted, so the output is a bit "wordy." I was also forced to use cut, assuming fixed width fields, which hopefully will not cause problems. Here's the output I was able to come up with, which I'll trim down for the production code

Code:
Getting Folder Share Grants...

Mailbox folders for pop3.test@domain.net
        Id  View      Unread   Msg Count  Path
----------  ----  ----------  ----------  ----------
         1  unkn           0           0  /
        16  docu           0           0  /Briefcase
        10  appo           0           0  /Calendar
        14  mess           0           0  /Chats
         7  cont           0           1  /Contacts
         6  mess           0           0  /Drafts
        13  cont           0           3  /Emailed Contacts
         2  mess           8           8  /Inbox
       386  mess           7           9  /Inbox/Shared With Mark
         4  mess           0           0  /Junk
         5  mess           0           7  /Sent
        15  task           0           0  /Tasks
       340  appo           0           7  /Test1
         3  unkn           0           5  /Trash
       366  mess           2           2  /Imap Test's Folder 1 (imap.test@domain.net:280)
       367  mess           2           2  /Imap Test's Folder 2 (imap.test@domain.net:281)
84bc3505-5  mess           7           9  /Imap Test's Folder 2/Folder 2a
       365  cont           0          83  /Imap Test's Gmail Contacts (imap.test@domain.net:3227)
       364  appo           0        1460  /Imap Test's Imported Calendar (imap.test@domain.net:283)
       399  appo           0           1  /Mark's Calendar Shared With Pop3 (mark@domain.net:158627)

Accepted shared folders from others:
/Imap Test's Folder 1 (imap.test@domain.net:280)
/Imap Test's Folder 2 (imap.test@domain.net:281)
/Imap Test's Gmail Contacts (imap.test@domain.net:3227)
/Imap Test's Imported Calendar (imap.test@domain.net:283)
/Mark's Calendar Shared With Pop3 (mark@domain.net:158627)

Grants for pop3.test@domain.net: /:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Briefcase:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Calendar:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Chats:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Contacts:
Permissions      Type  Display
-----------  --------  -------
          r   account  mark@domain.net
          r   account  imap.test@domain.net

Grants for pop3.test@domain.net: /Drafts:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Emailed Contacts:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Inbox:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Inbox/Shared With Mark:
Permissions      Type  Display
-----------  --------  -------
     rwidxa   account  mark@domain.net

Grants for pop3.test@domain.net: /Junk:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Sent:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Tasks:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Test1:
Permissions      Type  Display
-----------  --------  -------
      rwidx   account  imap.test@domain.net

Grants for pop3.test@domain.net: /Trash:
Permissions      Type  Display
-----------  --------  -------

Grants for pop3.test@domain.net: /Imap Test's Folder 2/Folder 2a:
Permissions      Type  Display
-----------  --------  -------



Mailbox folders for imap.test@domain.net
        Id  View      Unread   Msg Count  Path
----------  ----  ----------  ----------  ----------
         1  unkn           0           0  /
        16  docu           0           0  /Briefcase
        10  appo           0           0  /Calendar
        14  mess           0           0  /Chats
         7  cont           0           0  /Contacts
         6  mess           0           0  /Drafts
        13  cont           0           1  /Emailed Contacts
      3227  cont           0          83  /Gmail Contacts
       283  appo           0        1460  /Imported Calendar
         2  mess           5           8  /Inbox
       280  mess           2           2  /Inbox/Folder 1
       281  mess           2           2  /Inbox/Folder 2
       282  mess           7           9  /Inbox/Folder 2/Folder 2a
         4  mess           0           0  /Junk
       259  mess           0           0  /Junk E-mail
         5  mess           0           6  /Sent
        15  task           0           0  /Tasks
         3  unkn           0           1  /Trash
      3343  appo           0           7  /Pop3 Test's Test1 (pop3.test@domain.net:340)

Accepted shared folders from others:
/Pop3 Test's Test1 (pop3.test@domain.net:340)

Grants for imap.test@domain.net: /:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Briefcase:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Calendar:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Chats:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Contacts:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Drafts:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Emailed Contacts:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Gmail Contacts:
Permissions      Type  Display
-----------  --------  -------
      rwidx   account  pop3.test@domain.net

Grants for imap.test@domain.net: /Imported Calendar:
Permissions      Type  Display
-----------  --------  -------
      rwidx   account  pop3.test@domain.net

Grants for imap.test@domain.net: /Inbox:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Inbox/Folder 1:
Permissions      Type  Display
-----------  --------  -------
      rwidx   account  pop3.test@domain.net

Grants for imap.test@domain.net: /Inbox/Folder 2:
Permissions      Type  Display
-----------  --------  -------
      rwidx   account  pop3.test@domain.net

Grants for imap.test@domain.net: /Inbox/Folder 2/Folder 2a:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Junk:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Junk E-mail:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Sent:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Tasks:
Permissions      Type  Display
-----------  --------  -------

Grants for imap.test@domain.net: /Trash:
Permissions      Type  Display
-----------  --------  -------

$
 
Old 10-10-2014, 02:09 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,337

Rep: Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499
You can pipe the output directly into the while loop:
Code:
zmmailbox -z -m $USER gaf | egrep -v ':|Count|----------' | cut -c 43- | while read FOLDER
Looking at http://wiki.zimbra.com/wiki/Zmmailbox, I see you can get what appears to be JSON output by passing the verbose flag (this is pretty strange meaning for a verbose flag): http://wiki.zimbra.com/wiki/Zmmailbo...ons_for_a_User. You could use a tool like jq to work with that.
 
  


Reply

Tags
awk, zimbra


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to compare folders,Sub-Folders and Sub-Sub-Folders unix_72427 Programming 8 08-08-2012 02:51 PM
Urgent- shell script to create sub folders chetan_linux Programming 2 07-09-2012 04:35 PM
Shell Script Compare Folders corteplaneta Programming 8 09-18-2010 01:32 AM
access shared windows folders via CLI / script pirhana Linux - Networking 4 01-11-2008 06:27 AM
Syncronizing folders through ftp shell script mcrosby Linux - General 12 06-01-2006 02:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration