1

I have a bit of an issue and i've tried several ways to fix this but i can't seem to.

So i have two shell scripts.

background.sh: This runs a given command in the background and redirect's output.

#!/bin/bash

if test -t 1; then
  exec 1>/dev/null
fi

if test -t 2; then
  exec 2>/dev/null
fi

"$@" &

main.sh: This file simply starts the emulator (genymotion) as a background process.

#!/bin/bash
GENY_DIR="/home/user/Documents/MyScript/watchdog/genymotion"
BK="$GENY_DIR/background.sh"
DEVICE="164e959b-0e15-443f-b1fd-26d101edb4a5"
CMD="$BK player --vm-name $DEVICE"
$CMD

This works fine when i have NO spaces in my directory. However, when i try to do: GENY_DIR="home/user/Documents/My Script/watchdog/genymotion"

which i have no choice at the moment. I get an error saying that the file or directory cannot be found. I tried to put "$CMD" in quote but it didn't work.

You can test this by trying to run anything as a background process, doesn't have to be an emulator.

Any advice or feedback would be appreciated. I also tried to do.

BK="'$BK'"

or

BK="\"$BK\""

or

BK=$( echo "$BK" | sed 's/ /\\ /g' )

2
  • Your directory which might have empty spaces is assigned to your GENY_DIR variable so I'd think when you quote CMD it has no effect. Have you tried to quote your ${GENY_DIR} and see how it behaved ? Commented Jul 22, 2016 at 20:59
  • Thanks guys, storing it in an array as suggested by @that other guy worked like a charm :) Commented Jul 22, 2016 at 21:26

2 Answers 2

4

Don't try to store commands in strings. Use arrays instead:

#!/bin/bash
GENY_DIR="$HOME/Documents/My Script/watchdog/genymotion"
BK="$GENY_DIR/background.sh"
DEVICE="164e959b-0e15-443f-b1fd-26d101edb4a5"
CMD=( "$BK" "player" --vm-name "$DEVICE" )
"${CMD[@]}"

Arrays properly preserve your word boundaries, so that one argument with spaces remains one argument with spaces.

Due to the way word splitting works, adding a literal backslash in front of or quotes around the space will not have a useful effect.

John1024 suggests a good source for additional reading: I'm trying to put a command in a variable, but the complex cases always fail!

Sign up to request clarification or add additional context in comments.

7 Comments

./main.sh: line 5: 'home/user/Documents/My Script/watchdog/genymotion/background.sh': No such file or directory is the error message returned when trying to run as an array. I confirmed that the file exists :)
I copied your code, I don't know where your files are. Are you sure there wasn't supposed to be a / in front of that?
Yes sorry, there is a / in front of home, i use the $HOME variable but i typed it out for this.
Ok. It's tricky to debug by proxy when you're running one version and posting another. What is the actual error message? The error you posted said line 5, but that would not happen on line 5 as posted in this solution.
|
-2

try this:

GENY_DIR="home/user/Documents/My\ Script/watchdog/genymotion"

You can escape the space with a backslash.

1 Comment

./main.sh: line 5: '/home/user/Documents/My\: No such file or directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.