Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Passing commands to Functions in BASH

  1. #1
    Join Date
    Jun 2010
    Beans
    20

    Passing commands to Functions in BASH

    Code:
    #!/bin/bash
    
    ask()
    {
    $1
    }
    
    ask "echo This is not working >> 2.txt"
    Can anyone enlighten me on this one?
    The echo command works but the re-direction to the text file 2.txt does not work
    Can someone explain why this is the case.

  2. #2
    Join Date
    Feb 2008
    Beans
    5,636

    Re: Passing commands to Functions in BASH

    Try with this
    Code:
    #!/bin/bash
    
    ask()
    {
    $1
    }
    
    ask "echo This is not working" >> 2.txt

  3. #3
    Join Date
    Feb 2012
    Beans
    58

    Re: Passing commands to Functions in BASH

    try:

    ask()
    {
    $1
    }

    ask "echo This is not working" >> 2.txt

    ETA: TeoBigusGeekus, you beat me by a minute

  4. #4
    Join Date
    Aug 2011
    Beans
    27
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Passing commands to Functions in BASH

    Quote Originally Posted by iMetaphysikz View Post
    Code:
    #!/bin/bash
    
    ask()
    {
    $1
    }
    
    ask "echo This is not working >> 2.txt"
    Can anyone enlighten me on this one?
    The echo command works but the re-direction to the text file 2.txt does not work
    Can someone explain why this is the case.
    Try removing ask(), and then as a command, sh ask "echo This is not working" >> 2.txt It works for me.
    I'm sorry, Mr. Gates. I'm afraid I can't do that.

  5. #5
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Passing commands to Functions in BASH

    What do you want to do? Maybe you can use the read command of bash to assign your input to a variable. Test making the file echoer of the following three lines
    Code:
    #! /bin/bash
    
    echo "Input line of text and press Enter"
    read txt
    echo "Echoing $txt"
    and run it with
    Code:
    bash ./echoer

  6. #6
    Join Date
    Jun 2010
    Beans
    20

    Re: Passing commands to Functions in BASH

    The Problem that i now have is that if i want a second command/line of text that I want to pass to the function for example "echo hello" redirects its output to the 2.txt file as-well

    This is

    Code:
    #!/bin/bash
    
    ask()
    {
    $1
    $2
    }
    
    ask "echo This is not working" > 2.txt "echo hello"

  7. #7
    Join Date
    Feb 2012
    Beans
    58

    Re: Passing commands to Functions in BASH

    that works how I'd expect it to.

    Can you clarify what you're trying to so?

  8. #8
    Join Date
    Feb 2012
    Beans
    58

    Re: Passing commands to Functions in BASH

    #!/bin/bash

    ask()
    {
    $2 > $1
    $3
    }

    ask "2.txt" "echo This is not working" "echo hello"

    This prints "This is not working" to file and hello to the console ... is that what you're after?

  9. #9
    Join Date
    Jan 2012
    Beans
    342

    Re: Passing commands to Functions in BASH

    iMetaphyisicz ...

    I think you are 're-using' my function without understanding 'function' and its use.

    Code:
    #!/bin/bash
    
    function ask()
    {   
        echo -n "$@" '[y/n] ' ; read ans
        case "$ans" in
            y*|Y*) return 0 ;;
            *) return 1 ;;
        esac
    }
    
    ask "Do you understand functions?" &&
    echo "YES" > yesweunderstandfunctions.txt
    In your example the "function" is whatever is passed as parameter "${1}" to the script.

    Code:
    % cat param.sh
    #!/bin/bash
    
    echo "Parameter \${1} is ${1}"
    % param.sh yes
    Parameter ${1} is yes
    % param.sh gobbldygook
    Parameter ${1} is gobbldygook
    Not the best way to provide a function ... it might help if you explain what your trying to do. The following perhaps?

    Code:
    #!/bin/bash
    
    if [[ -z ${@} ]]; then
        echo "No question provided."
        echo "Usage: $(basename ${0}) question"
        exit
    fi
    
    function ask()
    {   
        echo -n "$@" '[y/n] ' ; read ans
        case "$ans" in
            y*|Y*) return 0 ;;
            *) return 1 ;;
        esac
    }
    
    ask "${@}" && echo "${@}=YES" > answer.txt
    Just so you understand, functions can do other things other than 'ask' questions.

    Code:
    nd () {
      test -d "${1}" || mkdir -p "${1}" && cd "${1}"
    }
    With this function 'nd newdir' will make a directory 'newdir' and 'cd newdir'.

    Hope that helps clarify things ... khay
    Last edited by Khayyam; February 26th, 2012 at 05:55 PM. Reason: changed "${1}" to "${@}"

  10. #10
    Join Date
    Jun 2010
    Beans
    20

    Re: Passing commands to Functions in BASH

    Thanks Khayyam.

    Essentially I am trying to reuse the ask function to do different things depending on the situation, in some instances I want to append to a log file, in other instances I want to call another function depending on user response.

    In the code below rdx would be another function that the script calls

    Code:
    ask()
    {
            local  i=0
    
            for i in {1..10}
                    do
    
                            echo -n "$1"
                            read ans
                            ans=$(echo $ans | tr '[A-Z]' '[a-z]')
    
                            case $ans in
                                    y)
                                            $2
                                            return 0 ;;
                                    n)
                                            $3
                                            return 0 ;;
                                    *)
                                            echo  "Please enter y or n"
                                            local j=$((10-$i))
                                            echo  "If neither y/n is pressed after $j attempts it will be assumed that $3"
                            esac
    
            done
            echo $3
    }
    
    ask "Please Enter an RDX Cartridge if formatting is required, is the RDX cartridge inserted (y/n)?" "rdx" "RDX Cartridge formatting is not required"
    
    ask "echo Has this script been useful" "echo Very Useful >> feedback.txt" "echo Try again Buddy >> feedback.txt"
    Now using your code with the && will add text to the yesweunderstandfunctions.txt if the user says yes because the function was successfully completed, but if the user says no it will not, I would also have to capture this information to a text file.

    That's why I have used the "echo Very Useful >> feedback.txt"


    Code:
    ask "Do you understand functions?" &&
    echo "YES" > yesweunderstandfunctions.txt
    Note, the code I have written above may not work as it was a rough example, as I ammended it from the script I'm currently writting. However I do not have that script on the computer I'm currently on.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •