0

I am very new to shell. I need to return multiple values from a shell function that's why I am sending the arguments as parameters to the function like we do in programming languages like C using pointers. I am calling the function like this

splitDate $date day month year

here day month & years are the variable in which I want to store the values. My function definition looks like this

splitDate(){
    export IFS="/"
    declare -a var
    index=0
    for word in $1; do
        var[ $index ]=$word

        ((index++))
    done

    $2=${var[0]}
    $3=${var[1]}
}

When I run this I get this error "day=theValueIWant: command not found" &"month=theValueIWant: command not found" Whats wrong here? test case : If i provide 04/05/2017 as date I expect day to store 04, month to store 05 & year to store 2017

3
  • Can you provide a verifiable example with actual values ? An input and expected output? Commented Feb 15, 2017 at 13:36
  • See this answer Commented Feb 15, 2017 at 13:38
  • Are you really sure that's what you want? Printing the output as a sequence of tokens so you can capture it to variables with set -- or whatever would probably sit better with the overall design of the shell. Commented Feb 15, 2017 at 13:47

1 Answer 1

3

You can just use read. The arguments to read are the names of the variables to populate, which can be produced by parameter expansion just as well as by hard-coding them.

splitDate(){
    if [[ $1 != ??/??/???? ]]; then
        printf '%s\n' "Date not in dd/mm/yyyy format" >&2
        return 1
    fi
    IFS=/ read -r "$2" "$3" "$4" <<< "$1"
}

which raises the question, do you really need a separate function?

# splitDate "$currentDate" day month year
#         vs
# IFS=/ read -r day month year <<< "$currentDate"
Sign up to request clarification or add additional context in comments.

5 Comments

++ for demonstrating simplicity again. Would this have been possible if I split $1 into an array with / de-limiter and trying to define other parameter values from the array using declare and not using eval?
You could; I was going to post something like IFS=/ read -a var <<< "$1"; declare "$2=${var[0]}"; # etc, along with printf -v "$2" '%s' "${var[0]}", until I realized neither did anything that read couldn't do just as well.
Was trying out the same as splitDate(){ local dateValues=( ${1//-/ } ) declare "$2=${dateValues[0]}" declare "$3=${dateValues[1]}" declare "$4=${dateValues[2]}" } for the function and called it as splitDate "$(date +"%d-%m-%Y")" day month year but was not able to make it work. may be I was missing something here.
@chepner thanks for suggestion. This works!! I am not getting what its doing. Can you please extend it for handling an edge case like if the date is 04/05/2012 but user enters it as 0 4/05/2012.
That's not an edge case; that's your user not using the function properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.