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

Manu Arora
  • 47
  • 1
  • 2
  • 5
  • Can you provide a verifiable example with actual values ? An input and expected output? – Inian Feb 15 '17 at 13:36
  • See [this answer](http://stackoverflow.com/questions/540298/bash-passing-arguments-by-reference/2852445#2852445) – Julian Feb 15 '17 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. – tripleee Feb 15 '17 at 13:47

1 Answers1

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"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • `++` 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`? – Inian Feb 15 '17 at 13:52
  • 1
    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. – chepner Feb 15 '17 at 13:53
  • 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. – Inian Feb 15 '17 at 13:57
  • @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. – Manu Arora Feb 15 '17 at 14:03
  • That's not an edge case; that's your user not using the function properly. – chepner Feb 15 '17 at 14:04