LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-11-2012, 02:31 AM   #1
zer0signal
Member
 
Registered: Oct 2010
Location: Cleveland
Distribution: Slackware, Fedora, RHEL (4,5), LFS 6.7, CentOS
Posts: 258

Rep: Reputation: 29
Multiple Variables from a BASH Command


Hey all...

I am kinda stuck, limited by the amount of variables I can get from the (set) command.. I am kinda looking for another way to get multiple variables from one command, and then those variables are passed to SQL on an (insert). This is a weekend project for me to learn a little about database's, and interacting w/ them (BASH,PHP,MYSQL).

Any suggestions on how I should approach this..?

PHP Code:
set $(hostname -fsar -q 1 1 |tail -|egrep -'Average|RESTART')

mysql -h database -u sar -"INSERT INTO SAR.LOAD (ID, SERVER, DATE_TIME, RUNQ, PLIST, LOAD1, LOAD5, LOAD15) VALUES (NULL, '$1', NOW(), '$4', '$5', '$6', '$7', '$8');" 
 
Old 11-11-2012, 05:21 AM   #2
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
well i cant get your question exactly... can you please explain what exactly what you need.
Do you want a command to output $1 $2 $3 $4 in some random variables from a command line.
 
Old 11-11-2012, 10:02 AM   #3
zer0signal
Member
 
Registered: Oct 2010
Location: Cleveland
Distribution: Slackware, Fedora, RHEL (4,5), LFS 6.7, CentOS
Posts: 258

Original Poster
Rep: Reputation: 29
by executing this command:

set $(hostname -f; sar -q 1 1 |tail -2 |egrep -v 'Average|RESTART')

i get:

hostname-whatever
10:56:37 AM 0 430 0.41 0.45 0.29 0

and set creates variables for each field

$1 = hostname
$2 = 10:56:37
$3 = AM

Etc...

But w/ 'set' I am limited to $1-9 so if my output is bigger than 9 fields i cannot use set in that scenario... What I am looking to try and do is find another way to get each field into a variable (where i am not limited), and then I would select only the variables I want to pass to the database.
 
Old 11-11-2012, 10:10 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
An array should work:
Code:
array=( $(hostname -f; sar -q 1 1 |tail -2 |egrep -v 'Average|RESTART') )
 
1 members found this post helpful.
Old 11-11-2012, 11:34 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by zer0signal View Post
But w/ 'set' I am limited to $1-9 so if my output is bigger than 9 fields i cannot use set in that scenario...
You can access the 10th field with ${10}.
 
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] replace multiple variables in file? bash nuser Programming 15 10-21-2012 01:02 PM
[SOLVED] Bash script to read multiple variables from a list then output to command diplo707 Programming 6 04-23-2012 12:46 PM
[SOLVED] Bash gurus: Using 'cut' to assign multiple shell variables? forbin Programming 9 08-08-2010 11:15 AM
How do I create multiple variables from a list in Bash? Passions Programming 12 05-23-2009 05:23 PM
bash: random creation of multiple variables vicious1 Programming 3 04-23-2007 03:34 AM

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

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