Skip to main content
Became Hot Network Question
added 761 characters in body
Source Link
pfnuesel
  • 6.3k
  • 9
  • 41
  • 64

I have a script, that does nothing useful but execute the positional arguments. (I'm aware of the security risks, and the script does not make anything useful because it's a minimal working example.)

$ cat script
> #!/usr/bin/env bash
>
> eval "${*}"


$ cat "docu ment"
> Lorem ipsum dolor sit amet

What I would like to do is call the script with ./script cat "docu ment", or ./script cat docu\ ment, but the quotes or the escape character vanishes and the script will try cat docu ment, which doesn't work. How would I fix the quoting in such a case?

EDIT: What I really want to do, is invoke a command as many times until it returns a successful exit code, or it tried n times. My script looks like this:

#!/usr/bin/env bash

# Try command several times, until it reports success (exit code 0)
# or I give up (tried n times)

tryMax=10
try=1

# Do until loop in bash
while
    eval "${@}"
    exitcode="${?}"
    [[ "${exitcode}" -ne 0 && "${try}" -lt "${tryMax}" ]]
do (( try++ ))
done

if [[ "${exitcode}" -ne 0 ]]; then
    echo -n "I tried hard, but did not manage to make this work. The exit code "
    echo "of the last iteration of this command was: ${exitcode}."
    exit "${exitcode}"
fi

I have a script, that does nothing useful but execute the positional arguments. (I'm aware of the security risks, and the script does not make anything useful because it's a minimal working example.)

$ cat script
> #!/usr/bin/env bash
>
> eval "${*}"


$ cat "docu ment"
> Lorem ipsum dolor sit amet

What I would like to do is call the script with ./script cat "docu ment", or ./script cat docu\ ment, but the quotes or the escape character vanishes and the script will try cat docu ment, which doesn't work. How would I fix the quoting in such a case?

I have a script, that does nothing useful but execute the positional arguments. (I'm aware of the security risks, and the script does not make anything useful because it's a minimal working example.)

$ cat script
> #!/usr/bin/env bash
>
> eval "${*}"


$ cat "docu ment"
> Lorem ipsum dolor sit amet

What I would like to do is call the script with ./script cat "docu ment", or ./script cat docu\ ment, but the quotes or the escape character vanishes and the script will try cat docu ment, which doesn't work. How would I fix the quoting in such a case?

EDIT: What I really want to do, is invoke a command as many times until it returns a successful exit code, or it tried n times. My script looks like this:

#!/usr/bin/env bash

# Try command several times, until it reports success (exit code 0)
# or I give up (tried n times)

tryMax=10
try=1

# Do until loop in bash
while
    eval "${@}"
    exitcode="${?}"
    [[ "${exitcode}" -ne 0 && "${try}" -lt "${tryMax}" ]]
do (( try++ ))
done

if [[ "${exitcode}" -ne 0 ]]; then
    echo -n "I tried hard, but did not manage to make this work. The exit code "
    echo "of the last iteration of this command was: ${exitcode}."
    exit "${exitcode}"
fi
Source Link
pfnuesel
  • 6.3k
  • 9
  • 41
  • 64

Correct quoting in eval

I have a script, that does nothing useful but execute the positional arguments. (I'm aware of the security risks, and the script does not make anything useful because it's a minimal working example.)

$ cat script
> #!/usr/bin/env bash
>
> eval "${*}"


$ cat "docu ment"
> Lorem ipsum dolor sit amet

What I would like to do is call the script with ./script cat "docu ment", or ./script cat docu\ ment, but the quotes or the escape character vanishes and the script will try cat docu ment, which doesn't work. How would I fix the quoting in such a case?