calling function with case

Hi I making a shell script and have some problem to start a function.

echo "chose a number to download"
        read i;
        case $i in
            1) head -1 ~/test3 | tail -1 > ~/test4;;
            2) head -2 ~/test3 | tail -1 > ~/test4;;
            3) head -3 ~/test3 | tail -1 > ~/test4;;
            4) head -4 ~/test3 | tail -1 > ~/test4;;
            5) head -5 ~/test3 | tail -1 > ~/test4;;
            6) head -6 ~/test3 | tail -1 > ~/test4;;
            7) head -7 ~/test3 | tail -1 > ~/test4;;
            8) head -8 ~/test3 | tail -1 > ~/test4;;
            9) head -9 ~/test3 | tail -1 > ~/test4;;
            10) head -10 ~/test3 | tail -1 > ~/test4;;
            11) head -11 ~/test3 | tail -1 > ~/test4;;
            12) head -12 ~/test3 | tail -1 > ~/test4;;
            13) head -13 ~/test3 | tail -1 > ~/test4;;
            14) head -14 ~/test3 | tail -1 > ~/test4;;
            15) head -15 ~/test3 | tail -1 > ~/test4;;
            16) head -16 ~/test3 | tail -1 > ~/test4;;
            17) head -17 ~/test3 | tail -1 > ~/test4;;
            18) head -18 ~/test3 | tail -1 > ~/test4;;
            19) head -19 ~/test3 | tail -1 > ~/test4;;
            20) head -20 ~/test3 | tail -1 > ~/test4;;
            21) head -21 ~/test3 | tail -1 > ~/test4;;
            22) head -22 ~/test3 | tail -1 > ~/test4;;
            23) head -23 ~/test3 | tail -1 > ~/test4;;
            24) head -24 ~/test3 | tail -1 > ~/test4;;
            25) head -25 ~/test3 | tail -1 > ~/test4;;
            26) head -26 ~/test3 | tail -1 > ~/test4;;
            27) head -27 ~/test3 | tail -1 > ~/test4;;
            28) head -28 ~/test3 | tail -1 > ~/test4;;
            29) head -29 ~/test3 | tail -1 > ~/test4;;
            30) head -30 ~/test3 | tail -1 > ~/test4;;                                                                                    
*) echo "Answer from 1 to 30" & sleep 2 & rm -f ~/test* & exit;;
        esac

if the choice is *) I want to start from the begining again, so how to make a function and call the function from *) ?

First make a "case" function exectlly same as above (or yours) and call it "Function".Then,

modify :

*) echo "Answer from 1 to 30" & sleep 2 & rm -f ~/test* & exit;;
        esac

To :

*) echo "Answer from 1 to 30" & sleep 2 & rm -f ~/test* & FLAG=1;;
        esac
 
 if [[ $FLAG -eq 1  ]]
then
Function
fi

=>> Hope I am clear to you...

A simply way with a loop is something like this:

.
.
stop=0

while [ $stop -eq 0 ]
do
  stop=1
  echo -n "chose a number to download"
  read i

  case "$i" in
    1) head -1 ~/test3 | tail -1 > ~/test4;;
    2) head -2 ~/test3 | tail -1 > ~/test4;;
    .
    .
    *) echo "Wrong choice"
       stop=0;;
  esac 
done

You mean something like this ?

Function ()
{
echo "chose a number to download"
        read i;
        case $i in
            1) head -1 ~/test3 | tail -1 > ~/test4;;
            2) head -2 ~/test3 | tail -1 > ~/test4;;
            3) head -3 ~/test3 | tail -1 > ~/test4;;
            4) head -4 ~/test3 | tail -1 > ~/test4;;
            5) head -5 ~/test3 | tail -1 > ~/test4;;
            6) head -6 ~/test3 | tail -1 > ~/test4;;
            7) head -7 ~/test3 | tail -1 > ~/test4;;
            8) head -8 ~/test3 | tail -1 > ~/test4;;
            9) head -9 ~/test3 | tail -1 > ~/test4;;
            10) head -10 ~/test3 | tail -1 > ~/test4;;
            11) head -11 ~/test3 | tail -1 > ~/test4;;
            12) head -12 ~/test3 | tail -1 > ~/test4;;
            13) head -13 ~/test3 | tail -1 > ~/test4;;
            14) head -14 ~/test3 | tail -1 > ~/test4;;
            15) head -15 ~/test3 | tail -1 > ~/test4;;
            16) head -16 ~/test3 | tail -1 > ~/test4;;
            17) head -17 ~/test3 | tail -1 > ~/test4;;
            18) head -18 ~/test3 | tail -1 > ~/test4;;
            19) head -19 ~/test3 | tail -1 > ~/test4;;
            20) head -20 ~/test3 | tail -1 > ~/test4;;
            21) head -21 ~/test3 | tail -1 > ~/test4;;
            22) head -22 ~/test3 | tail -1 > ~/test4;;
            23) head -23 ~/test3 | tail -1 > ~/test4;;
            24) head -24 ~/test3 | tail -1 > ~/test4;;
            25) head -25 ~/test3 | tail -1 > ~/test4;;
            26) head -26 ~/test3 | tail -1 > ~/test4;;
            27) head -27 ~/test3 | tail -1 > ~/test4;;
            28) head -28 ~/test3 | tail -1 > ~/test4;;
            29) head -29 ~/test3 | tail -1 > ~/test4;;
            30) head -30 ~/test3 | tail -1 > ~/test4;;
            * ) echo "Answer from 1 to 30" & sleep 2 & FLAG=1;;
        esac

if [[$FLAG -eq 1]]
then
Function
fi
{

But I get a syntax error: unexpected end of file

Thanks for the quick reply

Or lose the case statement with the 31 patterns and use a command using the variable $i, like e.g.:

echo "chose a number to download"
while : ; do
  read i
  if [ $i -ge 1 ] && [ $i -le 30 ]; then
    break
  fi
  echo "Answer from 1 to 30"
done
sed -n ${i}p ~/test3 > ~/test4

Yaa, Its like this :

Function ()
{
echo "chose a number to download"
        read i;
        case $i in
            1) head -1 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            2) head -2 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            3) head -3 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            4) head -4 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            5) head -5 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            6) head -6 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            7) head -7 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            8) head -8 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            9) head -9 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            10) head -10 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            11) head -11 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            12) head -12 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            13) head -13 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            14) head -14 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            15) head -15 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            16) head -16 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            17) head -17 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            18) head -18 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            19) head -19 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            20) head -20 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            21) head -21 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            22) head -22 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            23) head -23 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            24) head -24 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            25) head -25 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            26) head -26 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            27) head -27 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            28) head -28 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            29) head -29 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            30) head -30 ~/test3 | tail -1 > ~/test4 & FLAG=0;;
            * ) echo "Answer from 1 to 30" & sleep 2 & FLAG=1;;
        esac
 }                   ------->>>>>  Notice this.
 
if [[ $FLAG -eq 1 ]]   ------>>> And space between "[[" and "$FLAG " & "]]" and "1".
then
Function
fi

Thanks a million for all the answers it solved my problem :slight_smile:

---------- Post updated at 07:19 AM ---------- Previous update was at 07:15 AM ----------

But how to do if the list differs in number each time you run the script ?
Some time you get a from 1-5 can I call a text file whit the numbers to set as a variable?

I didn't get you...
Could you explain it a bit more...

say that now I can chose from 1-30 , but the script have a search function and the number to chose from the list depends of what you search.

---------- Post updated at 08:09 AM ---------- Previous update was at 08:01 AM ----------

Maybe this would make you understand what I mean,
the script is to get files from an ftp server

echo "1 search file"
echo "2 exit"
rm -f ~/test

while : ; do
  read i;
  if [ $i -eq 1 ]; then
  break

  else
  if [ $i -eq 2 ]; then
  exit

  else
  echo "chose between 1 or 2 "

  fi
  fi
done

echo "type file name" & read f
lftp -e "site search $f > ~/test & exit" ftp.xxx.xxx -u pelle,xxx -p 121
sleep 8

if [ -f ~/test ]
then
        cat ~/test | grep -E 'incoming|archive|requests' > ~/test1
        cut -b 6-100 ~/test1 > ~/test2
else
    echo "server down" & sleep 2 & exit
fi

#numbered list
cat -n ~/test2 > ~/test3
cat ~/test3

#Chose from list
echo "chose a number to download"
while : ; do
  read i;
  if [ $i -ge 1 ] && [ $i -le 30 ]; then
    break
  fi
  echo "Answer from 1 to 30"
done
sed -n ${i}p ~/test3 > ~/test4

So as you can see the length of the list depends of what file you search

echo "1 search file"
echo "2 exit"
rm -f ~/test
 
while : ; do
  read i;
  if [ $i -eq 1 ]; then
  break
 
  else
  if [ $i -eq 2 ]; then
  exit
 
  else
  echo "chose between 1 or 2 "
 
  fi
  fi
done
 
echo "type file name" & read f
lftp -e "site search $f > ~/test & exit" ftp.xxx.xxx -u pelle,xxx -p 121
sleep 8
 
if [ -f ~/test ]
then
        cat ~/test | grep -E 'incoming|archive|requests' > ~/test1
        cut -b 6-100 ~/test1 > ~/test2
else
    echo "server down" & sleep 2 & exit
fi
 
#numbered list
cat -n ~/test2 > ~/test3
cat ~/test3
 
#Chose from list
echo "Enter your search"
read srch
echo "chose a number to download"
while : ; do
  read i;
  if [ $i -ge 1 ] && [ $i -le "$srch" ]; then
    break
  fi
  echo "Answer from 1 to $srch."
done
sed -n ${i}p ~/test3 > ~/test4