LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 08-16-2017, 05:18 AM   #1
blason
Member
 
Registered: Feb 2016
Posts: 122

Rep: Reputation: Disabled
Need help on bash script


Hi Guys,

I have a Zimbra server and need to store the username and password using the zmpov command. I am getting the output like below but later I need to convert that into the step provided in no. 2

/tmp/user
abc@xyz.com
pqr@xyz.com
qwe@xyz.com


cat /tmp/user | while read line;do zmprov -l ga $line userPassword;done

I am getting below output

# name abc@xyz.com
userPassword: {SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF

# name pqr@xyz.com
userPassword: {SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF

# name qwe@xyz.com
userPassword: {SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF

2) Then that needs to be converted to like below; so user name will be picked up from first line and password from second line.

zmprov ma abc@xyz.com userPassword '{SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF'

zmprov ma pqr@xyz.com userPassword '{SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF'

zmprov ma qwe@xyz.com userPassword '{SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF'

can someone please help.
 
Old 08-16-2017, 05:37 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
1) I'd rearrange the first part a little since the cat is unnecessary.

Code:
while read line; do zmprov -l ga "$line" userPassword; done < /tmp/user;
2) Then sed can merge matching lines and clean up the contents a little:

Code:
sed -n -e "/^#/!d; s/^.* //; N; s/[[:space:]]/ /g; s/: */'/; s/[^[:alnum:]]*$/'/; p"
The N pushes the next line into the pattern space.

See "man sed"
 
1 members found this post helpful.
Old 08-16-2017, 09:05 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You could do it all in 1:
Code:
while read -r email
do
  zmprov ma "$email" userPassword "$(zmprov -l ga "$email" userPassword | grep -o '{.*')"
done</tmp/user
 
1 members found this post helpful.
Old 08-16-2017, 01:40 PM   #4
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Unfortunately both the tricks did not work

here is the error I got for first one

cat /tmp/pass | sed -n -e "/^#/!d; s/^.* //; N; s/[[:space:]]/ /g; s/: */'/; s/[^[:alnum:]]*$/'/; p"
-bash: !d: event not found


And here is the output for second one

while read -r email;do zmprov ma "$email" userPassword "$(zmprov -l ga "$email" userPassword | grep -v '^$' |tail -n 1 |awk '{print $NF}')";done< /tmp/pass
ERROR: account.NO_SUCH_ACCOUNT (no such account: # name 1@xxx.com)
ERROR: account.NO_SUCH_ACCOUNT (no such account: # name 1@xxx.com)
ERROR: account.NO_SUCH_ACCOUNT (no such account: userPassword: {SSHA}CuBH3js8XM2PJ3/980hAMi8F+yhz/zp3)
ERROR: account.NO_SUCH_ACCOUNT (no such account: userPassword: {SSHA}CuBH3js8XM2PJ3/980hAMi8F+yhz/zp3)
ERROR: account.NO_SUCH_ACCOUNT (no such account: )
 
Old 08-16-2017, 01:52 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Ok. sed has trouble with the single quotes in the substitution. It works fine in a script, but manually in the shell you'll have to hide them:

Code:
sed -n -e '/^#/!d; s/^.* //; N; s/[[:space:]]/ /g; s/: */\x27/; s/[^[:alnum:]]*$/\x27/; p' < /tmp/pass.txt
 
Old 08-16-2017, 02:10 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
I don't fully understand the problem... but this might help.

With this InFile ...
Code:
# name abc@xyz.com
userPassword: {SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF
# name pqr@xyz.com
userPassword: {SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF
# name qwe@xyz.com
userPassword: {SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF
... this awk ...
Code:
awk '{ol="zmprov ma " $3" "$4;
      getline;
      print ol $1,"\47"$2"\47"}'  \
$InFile >$OutFile
... produced this OutFile ...
Code:
zmprov ma abc@xyz.com userPassword: '{SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF'
zmprov ma pqr@xyz.com userPassword: '{SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF'
zmprov ma qwe@xyz.com userPassword: '{SSHA}QfN0yApdHJHWNSm+AgFGoC8ozcBWDrnF'
Daniel B. Martin
 
1 members found this post helpful.
Old 08-17-2017, 03:31 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by blason
And here is the output for second one

while read -r email;do zmprov ma "$email" userPassword "$(zmprov -l ga "$email" userPassword | grep -v '^$' |tail -n 1 |awk '{print $NF}')";done< /tmp/pass
ERROR: account.NO_SUCH_ACCOUNT (no such account: # name 1@xxx.com)
ERROR: account.NO_SUCH_ACCOUNT (no such account: # name 1@xxx.com)
ERROR: account.NO_SUCH_ACCOUNT (no such account: userPassword: {SSHA}CuBH3js8XM2PJ3/980hAMi8F+yhz/zp3)
ERROR: account.NO_SUCH_ACCOUNT (no such account: userPassword: {SSHA}CuBH3js8XM2PJ3/980hAMi8F+yhz/zp3)
ERROR: account.NO_SUCH_ACCOUNT (no such account: )
Firstly, any reason why it all has to be on 1 line?

Second, if you are going to introduce additional commands, it is hard to fix anything, hence I take no responsibility for your errors.

So why did you add:
Code:
| grep -v '^$' |tail -n 1
 
Old 08-17-2017, 01:24 PM   #8
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Firstly, any reason why it all has to be on 1 line?

Second, if you are going to introduce additional commands, it is hard to fix anything, hence I take no responsibility for your errors.

So why did you add:
Code:
| grep -v '^$' |tail -n 1
That is added to remove whiteline and picked last line which contains the interesting string.
 
Old 08-17-2017, 02:39 PM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please place your code and runtime message snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.
 
Old 08-18-2017, 12:22 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by blason View Post
That is added to remove whiteline and picked last line which contains the interesting string.
And what I am saying is that none of that is needed as the grep will only return the string required.
 
1 members found this post helpful.
  


Reply



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
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

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

All times are GMT -5. The time now is 12:58 AM.

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
Open Source Consulting | Domain Registration