Bash Skript vervollständigen

T

tecker2010

Hallo, bin sehr unerfahren was Bash angeht, daher dann doch der Entschluss hier zu fragen. Wir sollen den Quelltext (vergleich.sh)vervollständigen um damit 2 .cpp Datein zu kompilieren di im gleichen Ordner liegen zu starten und die Ergebnisse dann mit gnuplot grafisch darstellen. Die .cpp`s (normal.cpp und process.cpp)sind im Grunde Schleifen bei denen ich angebe wie oft diese durchlaufen werden.
Aber irgendwie muss ich ja die PARAMETER noch eingeben in dem Bash Script. Habe schon einiges versucht wie z.B. for i in `find /opt/*.cpp`oder so sowas aber ich bekomme immer den Fehler:
'ergleich.sh: line 11:syntax error near unexpected token
'ergleich.sh: line 11: `do

Ausführen tue ich die Shell mit bash vergleich.sh

Jemand eine Idee??

Code:
#!/bin/bash

if [ $# = 0 ]; then
  echo "Usage: $0 file1.cpp file2.cpp ..."
  exit 1
fi

for PARAMETER
do
  if [[ $PARAMETER != *.cpp ]]; then
    echo "Only \"*.cpp\" Files!"
    exit 2
  fi
  if [ ! -f $PARAMETER ]; then
    echo "Sorry, the \"$PARAMETER\" doesn't exist!"
    exit 3
  fi
done

for PARAMETER
do
  echo "compile: $PARAMETER"
  c++ -o ./${PARAMETER%.cpp} ./$PARAMETER -lpthread -Wno-deprecated
done

echo -n "parameter loop1 (<= 500): "
read RANGE1
echo -n "parameter loop2 (<= 5000): "
read LOOP2
COUNT=1
echo "wait ..."

# create here document for gnuplot
echo "#!/bin/bash" > plot.tmp
echo "gnuplot -persist <<ende" >> plot.tmp
echo "set title \" Processes vs. Threads\"" >> plot.tmp
echo "set xlabel \"loop1 [loop2=$LOOP2]\"" >> plot.tmp
echo "set ylabel \"Time in [s]\"" >> plot.tmp
echo "plot \"./$1.tmp\" using 1:2 smooth unique title \"$1\"" >> plot.tmp

for PARAMETER
do
  for ((LOOP1=5; LOOP1<=$RANGE1; LOOP1=LOOP1+5))
  do 
    for ((i=1; i<=$COUNT;i=i+1))
    do 
      echo -n "$LOOP1 " >> ./$PARAMETER.tmp
      /usr/bin/time -f %e -a -o $PARAMETER.tmp ./${PARAMETER%.cpp} $LOOP1 $LOOP2 
    done
  done 
  if [ $PARAMETER != $1 ]; then
  echo "replot \"./$PARAMETER.tmp\" using 1:2 smooth unique title \"$PARAMETER\"" >> plot.tmp 

  fi
  rm -f ${PARAMETER%.cpp}

done

# .png or .ps picture
FILENAME=`date +%Y%m%d-%H%M%S`
echo "set output \"$FILENAME.png\"" >> plot.tmp
echo "set terminal png"  >> plot.tmp
#echo "set terminal postscript color" >> plot.tmp
echo "set notitle" >> plot.tmp
#echo "replot \"./$PARAMETER.tmp\" using 1:2 smooth unique title \"$PARAMETER\"" >> plot.tmp 
echo "replot" >> plot.tmp
echo "ende" >> plot.tmp
chmod +x ./plot.tmp
./plot.tmp

rm -f plot.tmp
for PARAMETER
do
  rm -f $PARAMETER.tmp
done
 
hi.

hört sich irgendwie nach hausaufgabe an? dat solltest du aber schön selbst erarbeiten, mh? :)

also die parameter in einem bash-script bekommst du mit $1, $2, $n, wobei n für die position des parameters steht. also gibt dir ein
Code:
echo $1
den ersten parameter aus.
 
:rtfmb:
Dazu gibts den knuffigen Befehl
Code:
man bash
sowie
Code:
man bash-builtins
:D
Das hat schon einigen Leuten geholfen! :)

Im Ernst: es ist nicht schwer, in einem Shellskript mit Parametern zu arbeiten.

Ein Startpunkt wurde bereits genannt: die Übergabeparameter in der Variablen $[0-9] (Aufgabe: wie greife ich auf Parameter #12 zu und wie komme ich an die Anzahl der übergebenen Parameter?) sowie als eine Art Array in $* und $@ (wo liegt der Unterschied?). Desweiteren lohnt sich ein Blick auf 'shift'.

Anderer Ansatz: Auslesen der Parameter aus einer Datei. Sehr mächtige Konstruktion, wenn sie richtig eingesetzt wird!

Gruß und viel Erfolg
XL
 
Also mal ungetestet und ohne groß nachzudenken, hab ich Dir mal zumindest die Stellen markiert bei denen ein "in" fehlt, siehe ' in ##### hier fehlt ein in'
Code:
#!/bin/bash

if [ $# = 0 ]; then
  echo "Usage: $0 file1.cpp file2.cpp ..."
  exit 1
fi

for PARAMETER in          ##### hier fehlt ein in
do
  if [[ $PARAMETER != *.cpp ]]; then
    echo "Only \"*.cpp\" Files!"
    exit 2
  fi
  if [ ! -f $PARAMETER ]; then
    echo "Sorry, the \"$PARAMETER\" doesn't exist!"
    exit 3
  fi
done

for PARAMETER in          ##### hier fehlt ein in
do
  echo "compile: $PARAMETER"
  c++ -o ./${PARAMETER%.cpp} ./$PARAMETER -lpthread -Wno-deprecated
done

echo -n "parameter loop1 (<= 500): "
read RANGE1
echo -n "parameter loop2 (<= 5000): "
read LOOP2
COUNT=1
echo "wait ..."

# create here document for gnuplot
echo "#!/bin/bash" > plot.tmp
echo "gnuplot -persist <<ende" >> plot.tmp
echo "set title \" Processes vs. Threads\"" >> plot.tmp
echo "set xlabel \"loop1 [loop2=$LOOP2]\"" >> plot.tmp
echo "set ylabel \"Time in [s]\"" >> plot.tmp
echo "plot \"./$1.tmp\" using 1:2 smooth unique title \"$1\"" >> plot.tmp

for PARAMETER in          ##### hier fehlt ein in
do
  for ((LOOP1=5; LOOP1<=$RANGE1; LOOP1=LOOP1+5))
  do 
    for ((i=1; i<=$COUNT;i=i+1))
    do 
      echo -n "$LOOP1 " >> ./$PARAMETER.tmp
      /usr/bin/time -f %e -a -o $PARAMETER.tmp ./${PARAMETER%.cpp} $LOOP1 $LOOP2 
    done
  done 
  if [ $PARAMETER != $1 ]; then
  echo "replot \"./$PARAMETER.tmp\" using 1:2 smooth unique title \"$PARAMETER\"" >> plot.tmp 

  fi
  rm -f ${PARAMETER%.cpp}

done

# .png or .ps picture
FILENAME=`date +%Y%m%d-%H%M%S`
echo "set output \"$FILENAME.png\"" >> plot.tmp
echo "set terminal png"  >> plot.tmp
#echo "set terminal postscript color" >> plot.tmp
echo "set notitle" >> plot.tmp
#echo "replot \"./$PARAMETER.tmp\" using 1:2 smooth unique title \"$PARAMETER\"" >> plot.tmp 
echo "replot" >> plot.tmp
echo "ende" >> plot.tmp
chmod +x ./plot.tmp
./plot.tmp

rm -f plot.tmp
for PARAMETER in          ##### hier fehlt ein in
do
  rm -f $PARAMETER.tmp
done
 

Ähnliche Themen

Switche abfragen über Script

script sshpass

Verschlüsseltes Backup-Script mit rsync

Telefoninterview (Wie sich auf Bash-Shell-Fragen vorbereiten?)

Shell Skript beschleunigen

Zurück
Oben