calling function with case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calling function with case
# 1  
Old 05-01-2010
calling function with case

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

Code:
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 *) ?
# 2  
Old 05-01-2010
Quote:
Originally Posted by pelle
Hi I making a shell script and have some problem to start a function.

Code:
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 :
Code:
*) echo "Answer from 1 to 30" & sleep 2 & rm -f ~/test* & exit;;
        esac

To :
Code:
*) 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...

Last edited by Franklin52; 05-01-2010 at 10:45 AM.. Reason: Please use code tags!
# 3  
Old 05-01-2010
Quote:
Originally Posted by pelle
if the choice is *) I want to start from the begining again, so how to make a function and call the function from *) ?
A simply way with a loop is something like this:
Code:
.
.
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

# 4  
Old 05-01-2010
You mean something like this ?

Code:
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
# 5  
Old 05-01-2010
Or lose the case statement with the 31 patterns and use a command using the variable $i, like e.g.:
Code:
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


Last edited by Scrutinizer; 05-01-2010 at 09:10 AM..
# 6  
Old 05-01-2010
Quote:
Originally Posted by pelle
You mean something like this ?

Code:
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

Yaa, Its like this :

Code:
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


Last edited by Franklin52; 05-01-2010 at 10:48 AM.. Reason: Please use code tags!
# 7  
Old 05-01-2010
Thanks a million for all the answers it solved my problem Smilie

---------- 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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Shell Programming and Scripting

Function is calling only once

In my prog if i enter the input for the 1st time it is executing correctly, but for the second time entire script is not executing it just exiting my code is #!/bin/sh checkpo() { echo "Checking the entered PO to create output text file "; IFS=$'\n' set -f var=0 for i in $(cat... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

3. Shell Programming and Scripting

Calling two function

Hi, I need to run start_load function for two tables. Step 1: if HMAX_TBL_ID and GMAX_TBLI_D are same for tab_name1 then echo message "all table ids are processed" Step 2: go back and call start_load for tab_name2 and check if table id are same for table 2 too. Please let me know how to... (5 Replies)
Discussion started by: sandy162
5 Replies

4. Shell Programming and Scripting

Calling variables in case

Hey guys, I got a little problem with calling variables in my case This is a simple version of my script: var1=1 var2=2 while do case $1 in "a") var1=$var2; echo "1"; echo "var1: $var1"; echo "var2: $var2";; "b") var2=5; echo "2"; (1 Reply)
Discussion started by: Miki1579
1 Replies

5. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

6. Shell Programming and Scripting

Calling Function Problem

Hi, I had a scripts which calls two function. One function will call another function, script is working fine but the second function is not calling the first function. Below is the script #!/usr/bin/ksh fun1() { echo $DATETIME >> Test1.ksh return 0 } fun2() { typeset DATETIME=`date... (5 Replies)
Discussion started by: somu_june
5 Replies

7. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

8. UNIX for Dummies Questions & Answers

Calling on function from file??

This may sounds dumb, but can I call on a function from a file? For example, I have a function file full of functions like below (no shell designation): func { echo "blah blah blah 1" } func2 { echo "blah blah blah 2" } func3 { echo "blah blah blah 3" } Am I able to call on any one... (3 Replies)
Discussion started by: douknownam
3 Replies

9. UNIX for Dummies Questions & Answers

Calling a function

I have created a file generic.func and it has lots of functions. One of the functions is this: Check_backup_size() { dsmc q b $BACKUP_DIR/"*.Z" | awk '{print $1}'|sed 's///g' > outputfile X=`awk '{sum += $1} END {print sum }' outputfile'` echo "$X" ls -ltr $BACKUP_DIR/"*.Z" | awk... (5 Replies)
Discussion started by: ashika
5 Replies

10. Shell Programming and Scripting

Calling other file function

Hi, I am pretty new to unix. Lets say i have a program(run_program) that will call another file function(functiona, in same directory): hence, inside that run_program. i will just call "functiona xx xx" to refer and use that function. this run ok until i run this program from another folder.... (3 Replies)
Discussion started by: maldini
3 Replies
Login or Register to Ask a Question