0

I'm trying to change code , to code . in bash, but I am getting a bunch of errors. This should work.

code() {
  if [[ $1 == ,]];
  then
    code .
  else
    code $1
  fi
  return 1
}

It gives me parse error because of the semi colon

if I take the semi colon out, it gives me condition expected: $1

if I add quotes to the , and add the semi colon back in, if i get parse error near `;'

if I keep the quotes and remove the semi colon I get condition expected: $1

if I remove the semi colon and escape the , it literally just closes the terminal window.

what is going on here? all the documentaion online syas this should work.

Swish
  • 167
  • 1
  • 13
  • 1
    You need a space between `,` and `]]`. – Barmar Dec 06 '18 at 22:47
  • 6
    Paste your code into shellcheck.net. – Barmar Dec 06 '18 at 22:47
  • I pasted it in shellcheck.net and added the space and I'm getting this error: Line 1: code() { ^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang. Line 6: code $1 ^-- SC2086: Double quote to prevent globbing and word splitting. – Swish Dec 07 '18 at 06:13
  • You are asking specifically about Bash; why is this also tagged [tag:zsh]? – tripleee Dec 07 '18 at 11:26
  • Hey tripleeee what was the duplicate that you found? can you link to it? – Swish Dec 07 '18 at 15:26
  • @tripleee you marked as duplicate but you didn't have an answer... – Swish Dec 07 '18 at 15:28
  • Did you read the answers to the linked duplicate question? Which part of them are you having trouble understanding or applying? If you can [edit] this question to make clear that it is not a duplicate, we can vote to reopen it. – tripleee Dec 07 '18 at 16:02

1 Answers1

0
$ cat foo.sh
#!/bin/bash        # added shebang, bash since you have [[
code() {
  if [[ $1 == , ]] # removed the ; and added a space after ,
  then
    echo then      # added this
    code .
  else
    echo else      # and this
    code $1
  fi
  return 1
}

code $1

Run it:

$ bash foo.sh , | head -3
then
else
else
$ bash foo.sh . | head -3
else
else
else
James Brown
  • 31,411
  • 6
  • 31
  • 52