Running a function from a case statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running a function from a case statement
# 1  
Old 12-06-2011
Running a function from a case statement

Hi,
I have the below script that should take the command line option and run the desired script on another server. Only it doesn't seem to run the function, infact it just returns back to the command line.


Code:
 
   case $1 in
           1) msgbacklog() ;;
           2) jobstatus() ;;
      esac
msgbacklog () 
{
ssh BLAH@SERVER1 > output 2>/dev/null  <<_EOF
/home/BLAH/backlog.ksh
_EOF
}
jobstatus () 
{
ssh BLAH@SERVER1 > output 2>/dev/null  <<_EOF
/home/BLAH/jobstatus.ksh
_EOF
}

Any ideas?
# 2  
Old 12-06-2011
Pretty sure you need to declare the functions above the case statement, not below.

Don't call or declare functions with () after them. This is shell, not C. They should be declared like:
Code:
function msgbacklog
{
        ssh BLAH@SERVER1 > output 2>/dev/null /home/BLAH/backlog.ksh
}

I don't think you needed the here-document.

Last edited by Corona688; 12-06-2011 at 11:21 AM..
# 3  
Old 12-06-2011
Quote:
Originally Posted by Corona688
Don't call or declare functions with () after them.
With all due respect to Corona688, declaring the function with
Code:
foo() {
...
}

is OK, at least in bash and ksh.
But call a function without parens.
# 4  
Old 12-06-2011
OK, progress made. The script now looks like this:


Code:
 
function msgbacklog
{
ssh BLAH@SERVER1 > output 2>/dev/null  <<_EOF
/home/BLAH/backlog.ksh
_EOF
}

function jobstatus 
{
ssh BLAH@SERVER1 > output 2>/dev/null  <<_EOF
/home/BLAH/jobstatus.ksh
_EOF
}
 
      case $1 in
           1) msgbacklog ;;
           2) jobstatus ;;
      esac

It works for the first function, unfortunately for the second it returns the following error:

$ ./scriptlist.ksh: line 19 jobstatus: not found
# 5  
Old 12-06-2011
Works fine here... Is that the complete script? What's your system? What's your shell?
# 6  
Old 12-06-2011
Only missing #!/usr/bin/ksh at the top.

System is Sun Solaris 10

Shell is bash
# 7  
Old 12-06-2011
Quote:
Originally Posted by chris01010
Only missing #!/usr/bin/ksh at the top.
Shell is bash
It can't be both. Which is it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass values to case statement in a function korn shell

I'm in the process of writng a function that consists of a case statement is there a way of calling the function and passing a value to it? ie function1 () { case opt1 do ..... opt2 do..... esac } function opt1 I'm aware the syntax is not correct, but you get the general idea. (1 Reply)
Discussion started by: squrcles
1 Replies

2. Homework & Coursework Questions

Case Statement

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hey, guys I really need some help with a project. "Write a shell program that examines the command line... (8 Replies)
Discussion started by: sk192010`
8 Replies

3. Shell Programming and Scripting

Case Statement

Hey, guys I really need some help with a project. "Write a shell program that examines the command line arguments, counts and collects the number of options. Basically it has to collect and count the arguments that start with a "-" and the one's that don't start with a - I know I have to use... (2 Replies)
Discussion started by: sk192010`
2 Replies

4. Shell Programming and Scripting

Case statement

Hello, The standard case statement :- case "$1" in "IE0263") commands;; "IE0264") commands;; esac is it possible to have :- case "$1" in "IE0263" OR "IE0878") commands;; "IE0264") commands;; esac Thanks (4 Replies)
Discussion started by: jmahal
4 Replies

5. Shell Programming and Scripting

help with case statement

I am writing a script to pull diskspace information from our servers. Here is the script that I wrote: #!/bin/ksh for host in `cat /oper/hosts/esc.misc` do ssh -q -o ConnectTimeout=10 operator@$host df -h|grep "/dev/" |egrep '8%|9%|100%' | awk '{print H " " "at " $5 " with " $4 "... (1 Reply)
Discussion started by: rkruck
1 Replies

6. Shell Programming and Scripting

Using Subroutine / Function in case statement

I have issue running functions under case statement #!/bin/bash single() { Commands } multiple() { Commands } until ; do echo -e " \t \t M A I N - M E N U Perforce delete script \n" (1 Reply)
Discussion started by: sriram003
1 Replies

7. UNIX for Dummies Questions & Answers

CASE statement

Hi, I am writing a bash shell script. My script has a few user defined parameters. When the script runs the first thing it does is make sure that these parameters are valid. One of the parameters is called YEAR. A valid input for YEAR can be 1997-2000. One way I have come up with to ensure... (3 Replies)
Discussion started by: msb65
3 Replies

8. Shell Programming and Scripting

case statement

Hi all, I think i'm asking a sqtupid question here.. i'm using case sttament, what is the syntax or symbol for "or"? I thought was || here a quick sample of my case statment echo "Would you like to update your detail ?" read response case $response in ... (2 Replies)
Discussion started by: c00kie88
2 Replies

9. Shell Programming and Scripting

Case Statement

Can anyone please tell me why this wont work! Thanks so much! #!/bin/sh for file do case $file in *.*.*) echo Cannot have more than 1 dot exit ;; *'**'*) echo Cannot have more than 1 asterisk exit ;; *'*'*|?.) echo this is a target (19 Replies)
Discussion started by: Zeta_Acosta
19 Replies

10. Shell Programming and Scripting

case statement

Hi all, is it possible to create a 'dynamic' case statement. ie select option in `ls` do case satement depending on results of the above `ls` done I hope I have explained this ok! Thanks Helen (1 Reply)
Discussion started by: Bab00shka
1 Replies
Login or Register to Ask a Question