LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Need help in writing a shell script (http://www.linuxquestions.org/questions/programming-9/need-help-in-writing-a-shell-script-916608/)

khandu 12-01-2011 03:16 PM

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

firstfire 12-01-2011 03:29 PM

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


khandu 12-01-2011 03:44 PM

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)

khandu 12-01-2011 03:57 PM

Umm.. nevermind got it working by changing

Quote:

#!/bin/sh
to

Quote:

#!/bin/bash
Thanks.. testing it now :)

firstfire 12-01-2011 03:59 PM

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


khandu 12-01-2011 04:01 PM

nevermind :)

khandu 12-01-2011 07:37 PM

Is there anyway I can do the above in /bin/sh

There is a requirement it seems..

Ramurd 12-02-2011 04:29 AM

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.


All times are GMT -5. The time now is 02:03 AM.