-3

I have written the following shell script. It is executing well in the home dir but now that I have moved this file to another folder and when try to run this file it gives the : not a valid identifier error..

#!/bin/bash
echo "Specify environment(DEV,QA,PROD)"
read environment
upperString="${environment^^}"
export HYBRIS_OPT_CONFIG_DIR=$HOME/hybris5.7/hybris/bin/custom/rockport-shop/Configurations/Environments/config-"$upperString"
echo "Added Configuration ... "

How can I find the problem?

SaintHax
  • 1,875
  • 11
  • 16
  • 2
    Looks like an aberrant DOS carriage return in the script file. – tripleee May 11 '16 at 18:47
  • 3
    A script cannot meaningfully `export` anything to its parent. You can only `export` to child processes. This one doesn't create any, and the variable disappears when the script ends. – tripleee May 11 '16 at 18:48
  • read without -r will mangle backslashes, thought it is irrelevant in your case – sjsam May 11 '16 at 18:51
  • Possible duplicate of [Can I export a variable to the environment from a bash script without sourcing it?](http://stackoverflow.com/questions/16618071/can-i-export-a-variable-to-the-environment-from-a-bash-script-without-sourcing-i) – sjsam May 11 '16 at 18:54
  • You are exporting variable therefore that script is probably getting sourced, not ran. The shebang in that script is ignored. The parent shell sourcing it is probably not a bash nor similar. – alvits May 11 '16 at 18:56
  • Just to add to @tripleee's very correct comment. When you run a shell script from your shell it executes in a child process. Variables exported in that child process are only relevant for that process and any of its children. The parent process (your shell in which you executed the script) doesn't pick up that exported variable. The duplicate mentioned by sjsam should set you in the right direction. – JNevill May 11 '16 at 19:04
  • However, that does nothing to explain the error message. I suppose this is unreproducible as such, but wait for the OP to follow up before voting to close. – tripleee May 11 '16 at 19:07

2 Answers2

1

Your question's data is incomplete, but I'm willing to bet that even though you typed here:

export HYBRIS_OPT_CONFIG_DIR=$HOME/hybris5.7/hybris/bin/custom/rockport-shop/Configurations/Environments/config-"$upperString"

that you actually have

export $HYBRIS_OPT_CONFIG_DIR=$HOME/hybris5.7/hybris/bin/custom/rockport-shop/Configurations/Environments/config-"$upperString"

The error most likely comes from the export, and if it doesn't tell you what identifier is invalid, then the identifier was missing or expanded to null. In this case, your unset variable most likely expanded. Remove the $ from HYBRIS.

SaintHax
  • 1,875
  • 11
  • 16
1

do dos2unix if you have transferred it using dos environment.

dos2unix script_name

Install dos2unix package if you don't find dos2unix command in your system.

ankidaemon
  • 1,363
  • 14
  • 20
  • I checked the execution line by line and i think it get stucked at upperString="${environment^^}" but in the home directory its working fine – Akshay Pawar May 12 '16 at 04:30