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 01-09-2012, 03:50 AM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

Rep: Reputation: 32
how to create this on bash


I'm trying to create a script with the ff array

array 1 - foo@fee.com,fee@fee.com,fyii@fee.com (1 line)

array 2 - paul

what I want is to have the ff output in a text file

Code:
foo@fee.com: paul
fee@fee.com: paul
fyii@fee.com: paul
Could this be done on a bash script?

I'm trying this one

Code:
array2=`echo $alias | sed 's/\,/\n/g'`
echo "$array2: $array1"
But the output is

Code:
fee@fee.com
fee@fee.com
fyii@fee.com: paul
Any suggestions?
 
Old 01-09-2012, 04:23 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Try
Code:
emails=('first@example.com' 'second@example.com' 'third@example.com')
printf '%s: paul\n' "${emails[@]}"
printf is a Bash built-in (meaning that if you use it in a Bash script or Bash command, Bash will handle it internally; it will not execute any external program), but it is also available as a separate command (/usr/bin/printf). Both work the exact same way.

In this case, the printf command is given a format string, which contains only one parameter definition. Note that the extra string you want to add after each parameter must be within the format string, you cannot use another %s for it. The \n adds a newline; printf does not automatically add a newline like echo does. When printf is given more parameters than the one format string can handle, it will repeat the format string for each parameter (group). This is why the output is what you want.
 
Old 01-09-2012, 05:05 AM   #3
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
I tried to applied this on my script but it didn't work. Could you advise what went wrong?

Code:
#!/bin/bash

DB=test
DBUSER=root

while read username alias
do
newalias=`echo $alias | sed 's/,/\n/g'`
printf "${newalias[@]}: $username"
done < <(echo "SELECT value1,value2 from update where status = '0' and action = 'aliason';" | mysql --skip-column-names $DB -u$DBUSER)
The output is still the same

Code:
foo@fee.com
fee@fee.com
fyee@fee.com: ben5
 
Old 01-09-2012, 07:50 AM   #4
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
Ok. I discover the resolution. This could be a little dirty script but it seems it works for me though haven't use it in production


Code:
DB=test
DBUSER=root

while read username alias
do
newalias=`echo $alias | sed 's/,/ /g' > /tmp/alias` # replacing command with white space
newalias2=( `cat /tmp/alias` )
for emails in ${newalias2[@]};do
echo "$emails : $username"
done
done < <(echo "SELECT caluew1,value2 from ms_userupdate where status = '0' and action = 'aliason';" | mysql --skip-column-names $DB
 -u$DBUSER)
 
Old 01-09-2012, 08:58 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well now that you have provided all information (as opposed to what you think we needed to know):
Code:
DB=test
DBUSER=root

while read username emails
do
    set -- ${emails//,/ }
    for email
    do
        echo "$email : $username"
    done
done < <(echo "SELECT caluew1,value2 from ms_userupdate where status = '0' and action = 'aliason';" | mysql --skip-column-names $DB
 -u$DBUSER)
 
Old 01-09-2012, 09:14 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by packets View Post
I tried to applied this on my script but it didn't work. Could you advise what went wrong?
Code:
newalias=`echo $alias | sed 's/,/\n/g'`
newalias=(`echo $alias | sed 's/,/\n/g'`)
## or maybe
IFS=, read -a newalias <<< "$alias"
printf "${newalias[@]}: $username"
printf "%s: $username" "${newalias[@]}"
Quote:
Originally Posted by Nominal Animal
When printf is given more parameters than the one format string can handle, it will repeat the format string for each parameter (group).
Thanks, I hadn't known about this feature.

@grail
Code:

    for email
    do
        echo "$email : $username"
    done
printf "%s: $username\n" "$@"
 
1 members found this post helpful.
Old 01-09-2012, 11:07 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Thanks ntubski Often forget simple is better
 
Old 01-09-2012, 07:36 PM   #8
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by packets View Post
I tried to applied this on my script but it didn't work. Could you advise what went wrong?
The printf,
Quote:
Originally Posted by packets View Post
Code:
printf "${newalias[@]}: $username"
should have been
Code:
printf "%s: $username\n" "${newalias[@]}"
Like I explained, the "${newalias[@]}" will expand to one or more parameters. Because there is only one %s in the printf format string, printf will repeat the output once of each parameter.

(Note that I did not check the rest of the scriptlet for correctness.)
 
  


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
Help me to create a bash script Mr. Alex Linux - Newbie 5 06-14-2011 12:51 AM
[SOLVED] How do you create a BASH shortcut? lupusarcanus Linux - Newbie 26 02-25-2010 05:04 PM
create multiple directories in bash? Count Zero Programming 19 07-06-2008 08:34 AM
Create a bash file trolojik1 Programming 6 05-26-2008 02:48 AM
create variable name in bash kilgor Programming 3 12-31-2005 10:50 AM

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

All times are GMT -5. The time now is 02:40 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