LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 12-01-2011, 03:16 PM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 79

Rep: Reputation: 0
Question Need help in writing a shell script


Hey guys

N00b on programming.. trying to write a shell script to run a command via cronjob..

Say there are two variables
Code:
live="a b c"
data=""I=data1" "I=data2" "o=data3""
"a" corresponds to "I=data1"; "b" to "I=data2"; "c" to "o=data3"

I need to run something like this. merge.sh is another script which is run

Code:
merge.sh $LIVE -n $data ~/dumping/$LIVE.dat
I want this command to run like

Quote:
merge.sh a -n "I=data1"
merge.sh b -n "I=data2"
merge.sh c -n "o=data3"
I know how to do it if there was only one variable

Code:
for DB in $LIVE; do
merge.sh ${DB} -n ${DB}
done;
But don't know how to do when two variables are involved.

Please help

Last edited by khandu; 12-01-2011 at 03:20 PM.
 
Old 12-01-2011, 03:29 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi.
Try using bash arrays:
Code:
live=(a b c)
data=("I=data1" "I=data2" "o=data3" )
for((n=0; n<${#live[*]}; n++))
do
  echo merge.sh ${live[$n]} -n "${data[$n]}"
done
 
1 members found this post helpful.
Old 12-01-2011, 03:44 PM   #3
khandu
Member
 
Registered: Sep 2003
Posts: 79

Original Poster
Rep: Reputation: 0
Hi

I am getting an error

Quote:
/test.sh: syntax error at line 5: `live=' unexpected
and that line is

Quote:
live=(a b c)
 
Old 12-01-2011, 03:57 PM   #4
khandu
Member
 
Registered: Sep 2003
Posts: 79

Original Poster
Rep: Reputation: 0
Umm.. nevermind got it working by changing

Quote:
#!/bin/sh
to

Quote:
#!/bin/bash
Thanks.. testing it now
 
Old 12-01-2011, 03:59 PM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi.
You probably run this script via sh:
Code:
$ sh ./test.sh 
./test.sh: 1: Syntax error: "(" unexpected
Use bash instead
Code:
$ bash ./test.sh 
merge.sh a -n I=data1
merge.sh b -n I=data2
merge.sh c -n o=data3
On my system bash used by default. You may need to add '#!/bin/bash':
Code:
#!/bin/bash
live=(a b c)
data=("I=data1" "I=data2" "o=data3" )
for((n=0; n<${#live[*]}; n++))
do
  echo merge.sh ${live[$n]} -n "${data[$n]}"
done
 
Old 12-01-2011, 04:01 PM   #6
khandu
Member
 
Registered: Sep 2003
Posts: 79

Original Poster
Rep: Reputation: 0
nevermind

Last edited by khandu; 12-01-2011 at 04:08 PM.
 
Old 12-01-2011, 07:37 PM   #7
khandu
Member
 
Registered: Sep 2003
Posts: 79

Original Poster
Rep: Reputation: 0
Is there anyway I can do the above in /bin/sh

There is a requirement it seems..
 
Old 12-02-2011, 04:29 AM   #8
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 555

Rep: Reputation: 75
that greatly depends what /bin/sh really is.
In many distributions (clearly not yours) it's actually bash; on others it may be the "real" sh, meaning that there are little to no extras... You could work around that and join those things in a (very inefficient) way like this:

Code:
#check the number of fields of the variables
live="a b c"
data="I=data1 I=data2 o=data3"

numfields_live=$(echo $live | wc -w)
numfields_data=$(echo $data | wc -w)
if [ $numfields_live -ne $numfields_data ]
then
   # the fields for both variables don't match, and as such cannot be reliably paired
   echo error
   exit
fi

count=1

while [ $count -le $numfields_live ]
do
	echo -n $(echo "$live" | cut -d ' ' -f $count)
	echo -n " " # add a space?
	echo -n $(echo "$data" | cut -d ' ' -f $count)
	echo
	count=$(expr $count + 1)
done
might even be that you have to replace $(<command>) by backticks (`<command>`); tried to use no newer specials, so as to have most likely /bin/sh compatability.

/bin/ksh should work as well as /bin/bash for the scripts/hints posted by the others.

Last edited by Ramurd; 12-02-2011 at 04:44 AM. Reason: fixed the code; some stupid mistakes due to haste :-/
 
  


Reply

Tags
programming, shell


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
writing a shell script icecubeflower Linux - Newbie 1 03-29-2009 02:16 PM
Help writing a shell script! smoothdogg00 Programming 5 04-26-2006 12:15 PM
writing a shell script tchoua Linux - General 8 02-24-2005 07:18 PM
help writing shell script np complete Linux - Newbie 6 08-30-2004 10:43 PM
Need Help in Writing a Shell Script. KptnKrill Linux - General 2 05-19-2003 06:44 AM


All times are GMT -5. The time now is 07:24 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration