Error running a .sh script when extracted from a jar file.

I am trying to run a script called install.sh as follows:

I get a jar file, and extract it using the command:

unzip filename.jar -D path/to/files

then I navigate to that directory where I extracted the files, and run the command:

sh install.sh

(where install.sh is one of the files that has been extracted in that directory using the above unzip command.)

running this command gives me an error:

'nstall.sh: command not found.'

among other errors in the script ( from that portion of the script that should not even be executed, which is really weird).

However, instead of unzipping the jar file, if I just copied the same install.sh file to that folder, and ran the 'sh install.sh' command, the script runs fine. This makes me think that there is something wrong with unzipping of the jar file, but not sure. I am out of ideas on this. Any help would be appreciated.

P.S. I am using the ksh shell.

Try "sh ./install.sh"

I tried that already, that gives the same error.

---------- Post updated at 04:31 PM ---------- Previous update was at 04:03 PM ----------

As I said the script runs fine if I just copied the file install.sh and ran that command. It works fine even if I extracted the file from jar, deleted it, and brought it back, and then ran that command ( sh install.sh). It is only if I run that command directly after extracting it from the jar, do I get the error.

chmod +x install.sh
sh ./install.sh

Ok. I tried :

chmod +x install.sh
sh ./install.sh,

and that did not solve the problem.

Here is the listing of ls -l on that directory:

[oracle@vmhealthedb02]$ ls -l
total 20
-rw-r--r--  1 oracle oinstall   99 Dec 29 11:41 determineDataPumpDir.sql
-rwxr-xr-x  1 oracle oinstall 4802 Jan  4 15:18 install.sh
drwxr-xr-x  2 oracle oinstall 4096 Jan  4 15:19 META-INF
drwxr-xr-x  2 oracle oinstall 4096 Dec 28 14:25 scripts

could you please give me the output with below command?

sh -x ./install.sh

This is gonna sound silly, but I'm not sure what you're running. You do have /bin/sh right?

Running the above command gives the following output:

[oracle@vmhealthedb02]$ sh -x ./install.sh
+ $'\r'
: command not found2:
++ echo ./install.sh
++ awk -F install.sh '{print $1}'
+ packgeDir=$'./\r'
+ currDir=$'/home/oracle/hin-chd-capability-install-scripts-1.1.0-SNAPSHOT\r'
+ $'\r'
: command not found6:
+ cd $'./\r\r'
: No such file or directory/
+ $'\r'
: command not found8:
'/install.sh: line 23: syntax error near unexpected token `do
'/install.sh: line 23: `  do

---------- Post updated at 03:53 PM ---------- Previous update was at 03:50 PM ----------

[/COLOR]

Yes, I do have the shell. The current shell that I am running under is ksh.

[oracle@vmhealthedb02]$ echo $SHELL
/bin/ksh

Not really understand your script, especially this line:

awk -F install.sh '{print $1}'

can you paste your code here?

cat install.sh

Here is the install.sh script:

#/bin/bash

# determine the directory containing this script
packgeDir=`echo $0 | awk -F 'install.sh' '{print $1}'`
currDir=$PWD

cd "$packgeDir"

if [ -d schema ]
then

  printf "*** creating schema install script ***\n\n"

  script=schema/install.sql
  rm -fr $script

  printf "/* install script generated on " >> $script
  now=`date`
  printf "$now" >> $script
  printf " by my tiny script */\n\n" >> $script
  printf "whenever sqlerror exit failure\n" >> $script
  for file in `ls -1 schema/*.[Dd][Dd][Ll] | sort -g`
  do
    printf "prompt installing $file\n" >> $script
    printf "@" >> $script
    printf "$file\n" >> $script
    printf "prompt installed $file\n" >> $script
  done
  printf "exit success\n" >> $script

  printf "\ncommit;" >> $script
  printf "\nexit;\n" >> $script

  printf "*** done with schema install script ***\n\n"

fi
################################################################################

if [ -d ref-data ]
then

  printf "*** creating ref data install script ***\n\n"

  cd ref-data
  script=install.sh
  rm -fr $script

  printf "#!/bin/bash\n\n" >> $script
  printf "cd ref-data\n\n" >> $script
  for file in `ls -1 *.[Dd][Mm][Pp] | sort -g`
  do
    printf "cp -f $file \$DATA_PUMP_DIR\n" >> $script
    printf "rm -f \$DATA_PUMP_DIR/${file}.log\n" >> $script
    printf "impdp 'userid=\"/ as sysdba\"' logfile=${file}.log " >> $script
    printf "directory=DATA_PUMP_DIR dumpfile=$file " >> $script
    printf "table_exists_action=replace\n\n" >> $script
  done

  printf "cd .." >> $script
  chmod 777 $script
  cd ..

  printf "*** done with ref data install script ***\n\n"

fi

################################################################################

if [ -d functions ]
then

  printf "*** creating functions install script ***\n\n"

  script=functions/install
  rm -fr ${script}.sql

  printf "/* install script generated on " >> $script
  now=`date`
  printf "$now" >> $script
  printf " by my tiny script */\n\n" >> $script
  printf "whenever sqlerror exit failure\n" >> $script
  for file in `ls -1 functions/*.[Ss][Qq][Ll] | sort -g`
  do
    printf "prompt installing $file\n" >> $script
    printf "@" >> $script
    printf "$file\n" >> $script
    printf "prompt installed $file\n" >> $script
  done
  printf "exit success\n" >> $script

  printf "\ncommit;" >> $script
  printf "\nexit;\n" >> $script
  mv $script ${script}.sql
  printf "*** done with functions install script ***\n\n"

fi

################################################################################

if [ -d scripts ]
then

  printf "*** creating stored procedures install script ***\n\n"

  script=scripts/install
  rm -fr ${script}.sql

  printf "/* install script generated on " >> $script
  now=`date`
  printf "$now" >> $script
  printf " by my tiny script */\n\n" >> $script
  printf "whenever sqlerror exit failure\n" >> $script
  for file in `ls -1 scripts/*.[Ss][Qq][Ll] | sort -g`
  do
    printf "prompt installing $file\n" >> $script
    printf "@" >> $script
    printf "$file\n" >> $script
    printf "prompt installed $file\n" >> $script
  done
  printf "exit success\n" >> $script

  printf "\ncommit;" >> $script
  printf "\nexit;\n" >> $script
  mv $script ${script}.sql
  printf "*** done with stored procedures install script ***\n\n"

fi

################################################################################

printf "*** determining data_pump_dir ***\n\n"
DATA_PUMP_DIR=`sqlplus / as sysdba @determineDataPumpDir.sql | grep "dpdump"`
export DATA_PUMP_DIR

if [ -d schema ]
then
  printf "*** installing schema ***\n\n"
  sqlplus / as sysdba @schema/install.sql
  if [ "$?" -eq 0 ]
  then
    echo "**********          schema install sucess           **********"
  else
    echo "**********          schema install failure          **********"
  fi
fi

echo

if [ -d ref-data ]
then
  printf "*** installing ref data ***\n\n"
  ref-data/install.sh
fi

echo

if [ -d functions ]
then
  printf "*** installing functions ***\n\n"
  sqlplus / as sysdba @functions/install.sql
  if [ "$?" -eq 0 ]
  then
    echo "**********          functions install sucess           **********"
  else
    echo "**********          functions install failure          **********"
  fi
fi

echo

if [ -d scripts ]
then
  printf "*** installing stored prcedures ***\n\n"
  sqlplus / as sysdba @scripts/install.sql
  if [ "$?" -eq 0 ]
  then
    echo "**********          scripts install sucess           **********"
  else
    echo "**********          scripts install failure          **********"
  fi
fi

printf "*** all done :) ***\n\n"

cd "$currDir"

I just read the first lines

#/bin/bash

# determine the directory containing this script
packgeDir=`echo $0 | awk -F 'install.sh' '{print $1}'`
currDir=$PWD

cd "$packgeDir"

seems you need run the script by bash, then run the script by absolute path of install.sh

such as:

bash /abc/efg/hij/install.sh

Third, you can change your script to

packgeDir=`dirname $0`
currDir=$PWD

cd $packgeDir

The shebang is also wrong.

#/bin/bash

should be

#!/bin/bash

I tried using

packgeDir=`dirname $0'

in the script as you suggested.. and still get the same errors...

I also tried running with the entire path:

sh /abc/efg/install.sh

but no luck with that either...

---------- Post updated at 01:16 PM ---------- Previous update was at 01:15 PM ----------

I tried using the correct shebang as you suggested:

#!/bin/bash

but got no success with that.

---------- Post updated at 01:20 PM ---------- Previous update was at 01:16 PM ----------

Even if i pretend to make some changes in the file.... like for example opening install.sh, then putting a space somewhere in the file, then deleting that space, and saving that file,.... then it works when I type the command:

sh install.sh

It is only when I am trying to run it directly after unzipping it from the jar file, that I am not able to run it successfully.

So, is it corrupting it while unzipping it, or maybe not recognizing the file is in the directory, until I pretend to move or change it?... not sure.