3

I am trying to use /bin/sh -c to execute a .sh script with a date parameter. The -c option is not optional as it's what the framework my team has uses. I can't get the date epoch parameter to pass to my script properly. The following is an example demonstrating how calling a file causes the parameters not to work whereas passing a command directly works fine. The middle command (without the -c) is the expected output with each number repeating.

root:xxxxx@xxxx:/opt/xxxxxx/software_versions> /bin/sh -c /opt/xxxxxx/software_versions/test2.sh 1 2 3 4
1
2
3
4
root:xxxxx@xxxx:/opt/xxxxxx/software_versions> /bin/sh  /opt/xxxxxx/software_versions/test2.sh 1 2 3 4
11
22
33
44
root:xxxxx@xxxxx:/opt/xxxxxx/software_versions> /bin/sh -c 'echo $1 echo $2' a b c d
b echo c

Script contents:

echo 1$1
echo 2$2
echo 3$3
echo 4$4
KM.
  • 2,224
  • 1
    You have a pre-existing piece of software which you can't modify which runs /bin/sh -c with some arguments and you need access to the positional parameters in that shell. Right, so you don't control the -c part. What do you control? You'll probably have to find a hack to somehow make it introduce the necessary quoting or else add parameters to the /opt/xxxxxx/software_versions/test2.sh script or something, but what solution is possible depends on what part of the command template is under your control and what part is fixed. – Celada May 26 '16 at 21:30
  • I can modify the framework if needed, it's just a lot more to release / distribute. Turns out in this case it will be necessary to get this working properly. – Eekhoorntje May 27 '16 at 15:41

1 Answers1

2

Your first example would work just fine if you called it as

software_versions> /bin/sh -c "/opt/xxxxxx/software_versions/test2.sh 1 2 3 4"

By using -c, you invoke a subshell to run the command that follows it. Additional arguments will not be passed to the subshell in any predictable fashion. You solve this by sending the full string as an argument.

Ryder
  • 284