You are not logged in.

#1 2009-10-31 05:41:26

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Passing bash arguments and variables

Ok, so I've been staring at this script for the past hour with no avail. Basically, I want to pass arguments to a C program I wrote, both with arguments of the bash script and variables calculated in the bash script. For some reason it's not working, here's the script where it is right now:

#!/bin/bash

# Wrapper script for finding modes, outputting data as Frequency vs Amplitude

NUMPOS=$(cat $1 | awk '/^position/' | wc -l)
LINES=$[$(cat $1 | wc -l)-5-$NUMPOS]
NUMFREQ=$[$LINES/$NUMPOS]

mkdir split_$1
cp -t split_$1 drumdata $1
cd split_$1
./drumdata $1 $NUMPOS $NUMFREQ
rm drumdata $1

This gives me the error: ./getModes.sh: line 19: 26946 Segmentation fault      ./drumdata $1 $NUMPOS $NUMFREQ

So I tried a different method with array variables

#!/bin/bash

# Wrapper script for finding modes, outputting data as Frequency vs Amplitude

NUMPOS=$(cat $1 | awk '/^position/' | wc -l)
LINES=$[$(cat $1 | wc -l)-5-$NUMPOS]
NUMFREQ=$[$LINES/$NUMPOS]

args[0]=$1
args[1]=$NUMPOS
args[2]=$NUMFREQ

mkdir split_$1
cp -t split_$1 drumdata $1
cd split_$1
./drumdata ${args[@]}
rm drumdata $1

Which gives me the error: ./testModes: line 22:  7945 Segmentation fault      ./drumdata "${args[@]}"

Which makes me want to cry because I wrote a script to test array variable behavior:

File Edit Options Buffers Tools Sh-Script Help                                  
#!/bin/bash                                                                     

args[0]=test
args[1]=getModes.sh
args[2]=drumdata.c

cp -t asdf ${args[@]}

And it works sad. Why aren't my variables being evaluated?

Offline

#2 2009-10-31 08:50:46

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: Passing bash arguments and variables

Moved to General Programming.

Offline

Board footer

Powered by FluxBB