How to return a string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to return a string?
# 8  
Old 10-28-2010
Its still not working Smilie

Code:
function print_daemon_options {
    echo "Start Daemons - Please enter one or a combination of the following:"
    
    if isDatasubEnabled && isReconEnabled; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 5 6 7"
    
    elif isDatasubEnabled && isReconEnabled=0; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 6 7"

    elif isDatasubEnabled=0 && isReconEnabled; then
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 3 4 5 6 7"
        
    else
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 3 4 6 7"
    fi
    
    echo $(allowedInput)
}

function stop_daemons {
    print_daemon_options

    echo $(allowedInput)
}

All I want it to do is print the options and then echo the allowedInput numbers Smilie

Start Daemons - Please enter one or a combination of the following:
1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit
print_daemon_options: ./wmfo_menu.sh[398]: allowedInput: not found

stop_daemons: ./wmfo_menu.sh[398]: allowedInput: not found
# 9  
Old 10-28-2010
You seem to change things with each iteration you post. What you had originally seemed OK, except for the spaces in the variable declarations.

Code:
function print_daemon_options {
    echo "Start Daemons - Please enter one or a combination of the following:"
    
    if isDatasubEnabled && isReconEnabled; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 5 6 7"
    
    elif isDatasubEnabled && isReconEnabled=0; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 6 7"

    elif isDatasubEnabled=0 && isReconEnabled; then
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 3 4 5 6 7"
        
    else
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 3 4 6 7"
    fi
}

function stop_daemons {
    print_daemon_options

    echo $allowedInput
}


This is command substitution:

Code:
echo $(allowedInput)

meaning it will (try to) execute a program called "allowedInput" and return it's output to echo. That's why it gives you an error.
# 10  
Old 10-28-2010
Thanks Scott. I have a further problem now.

Code:
# Validate users input when they start/stop daemons
function validateInput {
    input = "${$1}"
    allowedInput = "${2}"
    
    for allowedOption in ${allowedInput}
    do
        if [ ${input} -eq ${allowedOption} ]
        then
            return 0
        fi
    done
    
    return -1
}

function print_daemon_options {
    echo "Start Daemons - Please enter one or a combination of the following:"
    
    if isDatasubEnabled && isReconEnabled; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 5 6 7"
    
    elif isDatasubEnabled && isReconEnabled=0; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 6 7"

    elif isDatasubEnabled=0 && isReconEnabled; then
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 3 4 5 6 7"
        
    else
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 3 4 6 7"
    fi
}


function stop_daemons {

    print_daemon_options
    echo "$allowedInput"
    
    read daemonOption
    
    if [ "$daemonOption" = '7' ] ; then
           return
   
       else
       
           validateInput "$daemonOption" "$allowedInput"
           
           if [ ${?} -eq 0 ] ; then
               echo "good!"
           fi
    fi
}

It now echos the numbers that the user can enter. When I enter a number I get the error

validateInput: ./wmfo_menu.sh[427]: : bad substitution

any ideas guys?
# 11  
Old 10-28-2010
You have spaces again, and an erroneous $ (not to mention lots of superfluous curly braces and quotes).
Code:
    input = "${$1}"
    allowedInput = "${2}"

Should be:

Code:
    input=$1
    allowedInput=$2

Interesting that your error is on line 427! Are you going to drip-feed us the whole script piece by piece?
# 12  
Old 10-28-2010
Thanks Scott that worked. The line number it was erroring at was the last line in the file for some reason.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to pass int and return string in C?

hi I want to write a function which takes int as input and returns a string like this. char GetString(int iNo) { switch(iNo) { case 0: return "Zero"; break; case 1: return "One"; break; } } void main() { int i; printf("Enter... (1 Reply)
Discussion started by: atharalikhan
1 Replies

2. Shell Programming and Scripting

return part of a string in bash

Hi, I would like to return the last part of a string in an array of strings in bash. The array contains in each position the content below: and I would like to return only the last part of each string. The correct result would be: How can I do that in bash using AWK?... (7 Replies)
Discussion started by: anishkumarv
7 Replies

3. UNIX for Dummies Questions & Answers

search for string and return substring

Hi, I have a file with the following contents: I need to create a script or search command that will search for this string 'ENDC' in the file. This string is unique and only occurs in one record. Once it finds the string, I would like it to return positions 101-109 ( this is the date of... (0 Replies)
Discussion started by: Lenora2009
0 Replies

4. UNIX for Dummies Questions & Answers

Search for string and return lines above and below

Hi, I have a file with the following contents: COMPANY ABCD TRUCKER JOE SMITH TRUCK 456 PLATE A12345678 TRUCKER BILL JONES TRUCK 789 PLATE B7894561 I need to create a script or search command that will search for this string '789' in the file. This string is unique and only... (7 Replies)
Discussion started by: doza22
7 Replies

5. Shell Programming and Scripting

Search string and return only the 5th line

I have looked at almost every posting on here regarding, grep, sed, awk, nawk, perl methods to perform this function, but they are just not returning the results I hope to see. Example File: abcdef 123456 ghijkl 7890123 7890123 7890123 7890123 mnopqrs 456789 This is the most... (4 Replies)
Discussion started by: doza22
4 Replies

6. Shell Programming and Scripting

KSH Functions String return

Hy everybody I'm trying to write a function like function(){ final=$1 return $final } but I get following error: "return: bad non-numeric arg ''" what shall I do? Isn't it possible to return strings?:confused: thanks lucae (2 Replies)
Discussion started by: lucae
2 Replies

7. Shell Programming and Scripting

return string in functions

Hi friends I need to return string value in functions can anyone help me out to return string values rather than integer. #!/bin/bash add_a_user() { USER=$1 COMPANY=$2 shift; shift; echo "Adding user $USER ..." echo "$USER working in $COMPANY ..." ret_type=YES return... (1 Reply)
Discussion started by: kittusri9
1 Replies

8. Shell Programming and Scripting

deleting 'carriage return' from string

hi there I'm getting a string from a sqlplus query, and I need to compare it with another string Problem here, is that the string i get from query brings a 'carriage return' with it, and comparing throws always false value. i need to delete all carriage retun charactres in string. how... (6 Replies)
Discussion started by: viko
6 Replies

9. Shell Programming and Scripting

String search and return value from column

Dear All I had below mention file as my input file. 87980457 Jan 12 2008 2:00AM 1 60 BSC1 81164713 Jan 12 2008 3:00AM 1 60 BSC2 78084521 Jan 12 2008 4:00AM 1 60 BSC3 68385193... (3 Replies)
Discussion started by: jaydeep_sadaria
3 Replies

10. Shell Programming and Scripting

return line below string grepp'ed for

Hi there Does anybody know how to get grep to return the line underneath the string i am grepping for ie, if i have this file hello everybody how are you and i issue #cat file | grep how I would like it to return the line below (in this case the string "are") Is this... (2 Replies)
Discussion started by: hcclnoodles
2 Replies
Login or Register to Ask a Question