Gå til innhold

Loope en haug med read, echo statements


Anbefalte innlegg

Okey, så jeg holder på med litt bash skripting, og jeg trenger litt hjelp.

 

Her er koden:

echo "Okey, type every month in your language"
read lmonth1
read lmonth2
read lmonth3
read lmonth4
read lmonth5
read lmonth6
read lmonth7
read lmonth8
read lmonth9
read lmonth10
read lmonth11
read lmonth12
echo "Okey, here is your typed months:
$lmonth1, $lmonth2, $lmonth3, $lmonth4, $lmonth5, $lmonth6, $lmonth7, $lmonth8, $lmonth9, $lmonth10, $lmonth11, $lmonth12"
echo "Are they correctly written? Y or N."
read correctly
if [ "$correctly" = "Y" ]; then
 echo "Okey, write the path to your date-list"
   read path
elif [ "$correctly" = "N" ]; then


else
   echo "Bye!"
  break
fi
done

 

Hvis jeg skriver N, så vil jeg at den skal loope alt fra echo "Okey, type every month in your language" og ned til echo "Okey, here is your typed months:

$lmonth1, $lmonth2, $lmonth3, $lmonth4, $lmonth5, $lmonth6, $lmonth7, $lmonth8, $lmonth9, $lmonth10, $lmonth11, $lmonth12"

 

Så hvis jeg skriver Y etter echo "Are they correctly written? Y or N."

så skal den gjøre Y kommandoene.

 

Noen som kan hjelpe meg litt?

Videoannonse
Annonse

Det beste er nok å strukturere logikken i funksjoner for hver bolk:

 

read_months() {
echo "Okey, type every month in your language";
read lmonth1;
read lmonth2;
read lmonth3;

echo "Okey, here is your typed months: $lmonth1, $lmonth2, $lmonth3"
echo "Are they correctly written? Y or N."
read correctly

if [ "$correctly" = "Y" ]; then
	read_path;
elif [ "$correctly" = "N" ]; then
	read_months;
else
	echo "Bye!"
fi
}

read_path() {
echo "Okey, write the path to your date-list"
read path
}

read_months;

Bash støtter arrays, så det kan man bruke:

 

#!/bin/bash
m=1
while [ $m -le 12 ]
do
	echo -n "Type name of month $m: "
	read month[$m]
	m=$((m + 1))
done
m=1
while [ $m -le 12 ]
do
	echo "Month $m is ${month[$m]} "
	m=$((m + 1))
done

 

edit: Dette bør da pakkes inn i funksjoner slik som fleskesvor anbefaler

Endret av tomsi42

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...