1

I'm trying to simplify switching between KANBANFILE's using kanban.bash on Windows.

I'm using several aliases such as the following for switching between KANBANFILEs:

alias k.pos="export KANBANFILE=\"/c/Users/ajleer/OneDrive - Name Of SomeOneDrive/kanbandb/.kanban.pos.csv\""

but the following line breaks it in two different places with an ambigious redirect error which means that the file it's trying to read from isn't there:

update_item_status(){
  item="$( cat "${KANBANFILE}" | awk "{ if (NR==$1) print \$0 }" )"
  [[ ${#item} == 0 ]] && echo "item $1 not found" && exit 1 
  if [[ -n "$2" ]]; then  # status change 
    status="$(echo "$item" | awk -F',' '{ print $1 }' | sed 's/"//g' )"
    flags="$(echo "$item"  | awk -F',' '{ print $4 }' | sed 's/"//g' )"
    dates="$(echo "$item"  | awk -F',' '{ print $5 }' | sed 's/"//g' )"
    newflags="$flags${2:0:1}"
    newdates="$dates $(get_current_date)"
    [[ "$2" =~ "DONE" ]] && date="$(get_current_date)"
    newitem="$item"
    newitem="${newitem/$status/$2}"
    newitem="${newitem/$flags/$newflags}"
    newitem="${newitem/$dates/$newdates}"
    KANBANITEMS="$(<$KANBANFILE)"  # THE AMBIGUOUS REDIRECT Error Line
    echo "${KANBANITEMS//"$item"/"$newitem"}" > "${KANBANFILE}"
    echo "$status -> $2"
  fi
}

and also here:

update_item(){
  item="$( cat "${KANBANFILE}" | awk "{ if (NR==$1) print \$0 }" )"
  [[ ${#item} == 0 ]] && echo "item $1 not found" && exit 1 
  status="$(echo "$item" | awk -F',' '{ print $1 }')"
  echo '#
# STATUSES ARE: '${statuses[*]}' 
#
'"$item" > "${TMP}".update
  ${EDITOR} "${TMP}".update
  KANBANITEMS="$(<$KANBANFILE)" # THE AMBIGUOUS REDIRECT Error Line
  newitem="$(cat "${TMP}".update | tail -n1 )" 
  echo "${KANBANITEMS//"$item"/"$newitem"}" > "${KANBANFILE}"
  echo "updated item $1"
}

So how do I rewrite my KABANFILE export alias so that it doesn't break the code above, but so that I still can have spaces in the path pointing to the .kanban.xxx.csv file?

The Ambigious Redirect occurs anytime I use the kanban <task-id> or kanban <task-id> <status> command (even though that's really just editing the csv file with the default editor).

P.S. I am using git-bash on Windows aka MINGW64

leeand00
  • 4,615
  • 1
    Why didn't you quote $KANBANFILE in those lines, like you have quoted elsewhere? – muru Jul 24 '19 at 11:49
  • Use set -x to show what your script does. You might see what's wrong with the problematic line. – Bodo Jul 24 '19 at 11:55
  • @muru You mean in the script lines that are causing the error? I didn’t write the script. I did however notice that those are the only two lines where the ‘$KANBAN’ variable is referenced without quotes. – leeand00 Jul 24 '19 at 11:57
  • Then can you edit the script? If so, fix the quoting there. – muru Jul 24 '19 at 11:58
  • @muru I actually tried that, but that would cause it to have quotes within the double quotes...ala: KANBANITEMS="$(<\"$KANBANFILE\")" it didn't fix the issue, unless the quotes are inside the variable too and that's somehow throwing it off.. – leeand00 Jul 24 '19 at 14:01
  • @Bodo Is set -x to be run from inside the script? – leeand00 Jul 24 '19 at 14:01
  • @Bodo Or do you mean that I should run my alias command to see what's in there? – leeand00 Jul 24 '19 at 14:04
  • 1
    @leeand00 KANBANITEMS="$(<"$KANBANFILE")" – muru Jul 24 '19 at 14:13
  • execute set -x, then run update_item_status or update_item to see what is causing the error – Bodo Jul 25 '19 at 07:13

1 Answers1

2

The quotes outside a command substitution are independent from the quotes inside it. So just quote "$KANBANFILE" like you do elsewhere.

$ filename="foo bar"
$ echo hello > "$filename"
$ echo "$( < $filename )"           #  $filename is not quoted
bash: $filename: ambiguous redirect

$ echo "$( < "$filename" )"         # "$filename" is quoted
hello

That said, in an assignment the outer quotes aren't strictly necessary, so var=$(something) works as well as var="$(something)" (Barring bugs).

See, e.g. Quoting within $(command substitution) in Bash and Do I need to quote command substitutions when assigning their output to a variable?

ilkkachu
  • 138,973