Tag Info

New answers tagged

1

The easiest thing is to put this at the top of your script: set -- $* This will then re-expand the parameter list, so any spaces in the parameters will become separators between new parameters. There may be some complicated behavior if you have very convoluted quoting in your parameters (e.g., quoted quotes), but my guess is that that is not likely to ...


2

The OS X version of date uses the -f option to parse a formatted date/time: date -j -f '%Y%m%d' "$1" +'%Y%m%d' The -j option causes it to just print the time, not try to set the system clock. So the full script would be: #!/bin/bash echo "$1"; startd=$(date -j -f '%Y%m%d' "$1" +'%Y%m%d'); echo "$startd"; Here's a transcript: $ ./testdate.sh 20151010 ...


1

If you are trying to format date on OS X, you can try this: date -j -f "%Y%m%d" "20151010" I get the following output: Sat Oct 10 17:27:28 CDT 2015


2

Just use another function: fn(){ pass(){ shift "$shift" "$other" "$@" } other=fn2 shift=3 pass "$@" unset -f pass } It would be better, though, to iterate over the args in an organized way. Like... while [ "$#" -gt 0 ] do : something with "$1" shift;done ..or.. for arg do : something with "$arg"; done ...but it's ...


5

"${@:4}" works for me in bash. You can also assign to another array and do indexing on it: foo=("$@") second_function "${foo[@]:4}"


2

You want to shift three times and pass $@ Full code is: do_test () { function_under_test=$1;shift line_number=$1;shift expected="$1";shift echo "ppppppppp" $@ $function_under_test "$@" if [ $result -eq $expected ]; then printf '.' else printf 'F' error_messages=$error_messages"Call to '$function_under_test $param1' failed: $result ...


0

You can always pass the array to the function and rebuild it as an array within the function: #!/usr/bin/env bash foo () { ## Read the 1st parameter passed into the new array $_array _array=( "$1" ) ## Do something with it. echo "Parameters passed were 1: ${_array[@]}, 2: $2 and 3: $3" } ## define your array array=(a 2 3 4 5 6 7 8 7 6 5 4 ...


0

You can pass only strings as arguments. But you could pass the name of the array: highest_3 () { arrayname="$1" test -z "$arrayname" && exit 1 # this doesn't work but that is the idea: echo "${!arrayname[1]}" eval echo '"${'$arrayname'[1]}"' }


3

You could read the arguments inside the function as an array. And then invoke the function with those arguments. Something like this worked for me. #!/bin/bash highest_3 () { number_under_test=("$@") max_of_3=0 for ((i = 0; i<$((${#number_under_test[@]}-2)); i++ )) { test=$((number_under_test[i] + number_under_test[i+1] + number_under_test[i+2])) ...



Top 50 recent answers are included