2

I am trying to initialize a variable in a case statement in a bash script,

function cpfiles(){
case $1 in
        a) echo "a" ; source = ${HOME}/dev/a.zip ; dest = 'PROGRA~2\\a.zip';;
        b) echo "b" ; source = ${HOME}/dev/b.zip ; dest = PROGRA~2\\b.zip;;
        *) echo "INVALID MODULE !" ;;
esac

echo ${source} ${dest}
}

but I am getting this error:

[#] cpfiles a
a
bash: =: No such file or directory
bash: dest: command not found...

What am I missing?

Jens
  • 69,818
  • 15
  • 125
  • 179
Michael Biniashvili
  • 500
  • 1
  • 12
  • 24

4 Answers4

3

Your script contains this :

a) echo "a" ; source = ${HOME}/dev/a.zip ; dest = 'PROGRA~2\\a.zip';;
b) echo "b" ; source = ${HOME}/dev/b.zip ; dest = PROGRA~2\\b.zip;;

The problem :

  • source is a shell builtin
  • You added extra spaces before and after the = sign. You may find that easier to read, but it is not valid shell syntax.

So instead of assigning a value to a variable named source, you are actually calling the source builtin, passing it = as an argument.

Try this instead :

a) echo "a" ; source=$HOME/dev/a.zip ; dest='PROGRA~2\a.zip';;
b) echo "b" ; source=$HOME/dev/b.zip ; dest='PROGRA~2\b.zip';;

Please note that the braces around HOME, while perfectly valid, are not required because there is no ambiguity where the variable name ends (/ is not valid in a variable name, so the shell stops there while parsing). Double quoting would be used by most people on the assignments, but it is not required when the assigned string contains no whitespace (even if its expanded value does).

One last issue... In one of the cases you are single-quoting the assigned value for dest, and also escaping the backslash. This will produce a value containing two backslashes, which I assume is not what you want. Remove either the quotes or one of the backslashes.

Fred
  • 6,590
  • 9
  • 20
1

Assignments in the shell don't take a whitespace around the =. It's completely valid for some command to expect a single = as an argument.

You get =: No such file or directory because source is a shell command that tries to open the named file:

source: source filename [arguments]
    Execute commands from a file in the current shell.

and dest: command not found because the latter part is taken as running command dest.

ilkkachu
  • 6,221
  • 16
  • 30
1

the spaces are important in bash, used to split arguments ; to set a variable

source=${HOME}/dev/a.zip

as source is a command the following command is trying to open the file = which doesn't exist

source = ...
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
0

Two things important in your script which are causing error:

1. source : Its a shell variable and can be used to load any functions file into the current shell script or a command prompt. It read and execute commands from given FILENAME and return.

2. "=" in Unix a=b without space treated as assignment operator and a = b used for comparison of two strings or you can check in condition statement like [[ $str1 == $str2 ]. It is an alternative method for string equality check.

Also $HOME will be enough to fetch the value of variable instead of ${HOME}, but if you are using it it won't through any error. See the below context.

*$ echo ${SHELL}

/bin/bash

$ echo $SHELL

/bin/bash*

rachna garg
  • 273
  • 2
  • 4
  • 11