-1

I am developing a bash script in Windows 7 using a bash emulated terminal, exactly I use bash terminal in MobaXterm 9.1. The reason is that I have a Windows 7 laptop but my script would eventually run on a Jenkins Linux Server.

Spaces and/or ( in path are preventing my script from running successfully.

My script is:

#!/bin/bash
eclexe0="C:\Program Files (x86)\HPCCSystems\6.0.4\clienttools\bin\ecl.exe"
eclexe1="/drives/c/Program Files (x86)/HPCCSystems/6.0.4/clienttools/bin/ecl.exe"
clear

#1
echo "Testing if ecl.exe is available..."
if [ -f "$eclexe0" ]; then
  echo "FOUND." >&2
else
  echo "NOT FOUND." >&2
  exit 1
fi

echo "Executing ecl.exe"

echo 1
eval "$eclexe0"

echo 2

When I run it the output is

Screenshot

As you can see script is not happy with path C:\Program Files (x86). If I move files to another path (C:\oscar\eclfolder) it works. I tried the answers here but they don't work, probably because I am in a Windows 7...

How can I make this script work?

2 Answers2

1

Your script doesn't work because you are running eval on the pathname to the executable file.

In any shell script, you can already use a variable to specify a command to run.

If you don't quote your $variable reference, the contents of the variable will also be split into separate words according to whitespace.

You have quoted your variable reference. However, by running eval, you are parsing the result of the variable expansion a second time. You don't need to.

The following illustration may be instructive:

$ ls
file.txt    file2.txt
$ somevar=ls
$ $somevar
file.txt    file2.txt
$ ls -l
total 0
-rw-r--r--  1 Me  staff  0 Sep  2 16:06 file.txt
-rw-r--r--  1 Me  staff  0 Sep  2 16:06 file2.txt
$ somevar='ls -l'
$ $somevar
total 0
-rw-r--r--  1 Me  staff  0 Sep  2 16:06 file.txt
-rw-r--r--  1 Me  staff  0 Sep  2 16:06 file2.txt
$ "$somevar"
bash: ls -l: command not found
$ 

As you can see, when I attempt to run "ls -l", quoted, the shell looks for a command with that exact name—spaces included.

In my example, that's not what I want.

However, in your example, that is what you want.

Instead of using eval "$eclexe0", just use "$eclexe0".


The Bigger Picture

Free advice: If you plan to run your script on a Linux box, test it on a Linux box. MobaXterm is not a Linux box.

Install Vagrant and VirtualBox (which are both free) and create a virtual machine you can do your tests in.


Further reading:

Wildcard
  • 36,499
-1

You probably need to escape the quotes like this ( "\"string"\" ) according to this post: https://stackoverflow.com/questions/5253782/bash-problem-with-eval-variables-and-quotes

octn
  • 1
  • It would be better simply to avoid using eval in the first place. Unless it's got a magic extension provided by this "bash terminal emulator" it's not needed in the code given by the OP. – Chris Davies Sep 02 '16 at 15:48