LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-26-2010, 08:16 AM   #1
qwertyjjj
Senior Member
 
Registered: Jul 2009
Location: UK
Distribution: Cent OS5 with Plesk
Posts: 1,013

Rep: Reputation: 30
get variable from bash from mysql or pass from php


I am not parsing on a webserver so is it possible to have both
#! /usr/bin/php &
#!/bin/bash

in the same script?

Alternatively, I have a current bash script that I need to get some variables from mysql and not sure how to get mysql results in bash:

Quote:
mysql -h server.net -u username1 -paaa -e "USE squid; SELECT email, usern FROM TABLE WHERE blah blah;"
emailadd="resultfrom above"
usern="resultfromabove"
 
Old 04-26-2010, 10:48 AM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
This works for 2 values. the -N in the mysql command prevents the column headings, and we are only asking for values from one record.

Code:
data=$(mysql -N -h localhost -u username -ppassword -e "USE DBNAME;select field1,field2 from table where field3 = something")

set -- $data

field1=$1
shift
field2=$@

echo "$field1"
echo "$field2"
Of course you wouldn't echo the values but use them elsewhere.

If you want to get more values, then use shift and $1 for each value

ie.

Code:
set -- $data

field1=$1
shift
field2=$1
shift
field3=$1
shift
field4=$1

echo "$field1"
echo "$field2"
echo "$field3"
echo "$field4"

Last edited by smoker; 04-26-2010 at 11:00 AM.
 
Old 04-26-2010, 11:06 AM   #3
qwertyjjj
Senior Member
 
Registered: Jul 2009
Location: UK
Distribution: Cent OS5 with Plesk
Posts: 1,013

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by smoker View Post
This works for 2 values. the -N in the mysql command prevents the column headings, and we are only asking for values from one record.

Code:
data=$(mysql -N -h localhost -u username -ppassword -e "USE DBNAME;select field1,field2 from table where field3 = something")

set -- $data

field1=$1
shift
field2=$@

echo "$field1"
echo "$field2"
Of course you wouldn't echo the values but use them elsewhere.

If you want to get more values, then use shift and $1 for each value

ie.

Code:
set -- $data

field1=$1
shift
field2=$1
shift
field3=$1
shift
field4=$1

echo "$field1"
echo "$field2"
echo "$field3"
echo "$field4"
Should that be field2=$2 ?
I have a different keyboard so not sure where your at sign is.
 
Old 04-26-2010, 11:13 AM   #4
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Quote:
Originally Posted by qwertyjjj View Post
Should that be field2=$2 ?
I have a different keyboard so not sure where your at sign is.
What difference does my/your keyboard make ?
If I typed an @ that's what appears.

in the line concerned $@ returns whatever is left in the variable we are parsing, $1 returns the next input only.
 
Old 04-26-2010, 11:33 AM   #5
qwertyjjj
Senior Member
 
Registered: Jul 2009
Location: UK
Distribution: Cent OS5 with Plesk
Posts: 1,013

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by smoker View Post
What difference does my/your keyboard make ?
If I typed an @ that's what appears.

in the line concerned $@ returns whatever is left in the variable we are parsing, $1 returns the next input only.
Well, I thought $@ might be a typo because $2 made logical sense also and your @ sig might have been above the number 2

I used this and it seemed to work
field1=$1
shift
field2=$1
shift
field3=$1
 
Old 04-26-2010, 01:09 PM   #6
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Here is what the @ is for.
imagine a database with 3 fields per record.
Code:
|	firstname	|	lastname	|	hobbies		|
-------------------------------------------------------------------------
|	fred		|	basset		|	i eat bones	|
If you use this code
Code:
set -- $data

field1=$1
shift
field2=$1
shift
field3=$1

echo "$field1"
echo "$field2"
echo "$field3"
the output would be :
fred
basset
i

But if you use this code
Code:
set -- $data

field1=$1
shift
field2=$1
shift
field3=$@

echo "$field1"
echo "$field2"
echo "$field3"
The output would be :
fred
basset
i eat bones

The thing to remember is that the order of the fields in the variable is the same as the order of the fields in the select statement. So if you have a field that has spaces in the contents, always select it last, because if you select it first then use $@ in the first assignment, this happens
Code:
set -- $data

field3=$@
shift
field2=$1
shift
field1=$1

echo "$field1"
echo "$field2"
echo "$field3"
output would be :
eat
bones
i eat bones fred basset

which is not expected or required.
 
Old 04-28-2010, 11:21 AM   #7
qwertyjjj
Senior Member
 
Registered: Jul 2009
Location: UK
Distribution: Cent OS5 with Plesk
Posts: 1,013

Original Poster
Rep: Reputation: 30
If the data set is empty, how can I exit the script after the first sql check.
ie what's the check for the dataset?
 
Old 04-28-2010, 12:07 PM   #8
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Using the fred basset example above :

Code:
data=$(mysql -N blah blah blah)

if [ "$data" ]

then
    set -- $data
    firstname=$1
    shift   
    blah
    blah 
    blah

else

    exit 0

fi

Last edited by smoker; 04-28-2010 at 12:38 PM. Reason: changed exit 1 to exit 0 as you are not returning anything
 
Old 04-28-2010, 12:22 PM   #9
qwertyjjj
Senior Member
 
Registered: Jul 2009
Location: UK
Distribution: Cent OS5 with Plesk
Posts: 1,013

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by smoker View Post
Using the fred basset example above :

Code:
data=$(mysql -N blah blah blah)

if [ "$data" ]

then
    set -- $data
    firstname=$1
    shift   
    blah
    blah 
    blah

else

    exit 1

fi
Is there another in bash to do it such as a NOT check?
I have a lot of code in between so could I do:

if ![ "$data" ] then exit 1
else {all code below}

What is fi above?
 
Old 04-28-2010, 12:31 PM   #10
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Surely it makes no difference. Both sets of code have to go in somewhere. Presumably you were hoping to get some results otherwise it's all a bit pointless.

fi is the end of the if section.

done is the end of a do section

esac is the end of a case section

http://tldp.org/LDP/abs/html/

BTW I changed the exit code in the example, because you are not using a return value from the script.

It now reads
exit 0

You can change it to your method by using
Code:
if [ -n "$data" ]
then
    exit 0
else
    the rest 
    of the
    code
fi
If you really wanted you could put the main code in a subroutine so you would end up with
Code:
function process {
    the rest 
    of the
    code
    return
}

data=$(mysql -N blah blah blah)

if [ -n "$data" ]
then
    exit 0
else
    process
fi

Last edited by smoker; 04-28-2010 at 12:48 PM.
 
Old 04-28-2010, 12:58 PM   #11
qwertyjjj
Senior Member
 
Registered: Jul 2009
Location: UK
Distribution: Cent OS5 with Plesk
Posts: 1,013

Original Poster
Rep: Reputation: 30
Just tried this but it seemed to run anyway when it should have exited:

Quote:
#!/bin/bash
data=$(mysql -N -h server.net -u squid -paaa -e "USE DB;select email_address, user,customers_id FROM TABLE WHERE enabled=1 AND
UpdateOnNextCycle=1 LIMIT 0,1;")

if [ -n "$data" ]
then
exit 0
else

set -- $data
sqlemailadd=$1
shift
sqluserstring=$1
shift
sqlcustid=$1
emailadd=$sqlemailadd
usern=$sqluserstring
custid=$sqlcustid
#echo $emailadd
#echo $usern

cd /etc/openvpn/
cd /etc/openvpn/easy-rsa/2.0/
source ./vars

etc.

etc.

fi
 
Old 04-28-2010, 03:30 PM   #12
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
sorry, should be -z not -n

read the link I gave you.
 
  


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
bash how to pass value to variable in this function? babag Programming 10 04-02-2008 11:42 PM
How to pass a c variable to a bash script? daYz Programming 3 09-28-2007 08:30 AM
pass javascript variable to php ALInux Programming 6 01-06-2006 06:20 AM
How to pass mysql query to a variable? chynna_v Programming 4 09-03-2004 05:09 AM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 03:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:47 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
Open Source Consulting | Domain Registration