0

I won't bore you with the details but I need to pass a command to a file without spaces, with each space it considers the next text to be a variable which is not the case. I need to be able to create a command with another character other than space or use hex characters to overcome this. Unfortunately I am completely locked to using this method as this file exists on 1000s of devices and I would need to change the file manually on each device, if I can workaround it's limitations then this would be the best solution for me.

Does anyone have any ideas if this is possible?

edit Hi guys for some more info, lets say I have the file test.sh, it has a function called echo_var, if I use the command for example

test.sh echo_var helloworld

The function will simply echo helloworld

If I say use the command

test.sh echo_var hello world

Only hello gets echoed as it considers "hello world" as two strings and only acknowledges the first one, the function takes each separated string as a variable and only uses the first variable ie hello. I need to make hello world one variable

The command I want to run is to restart a service ie

test.sh echo_var hello&&service apache2 restart

So both hello will be echoed and the service restarted. But after the space after service, the command is cut.

Here is the file (test.sh) I have to interact with, again this is on 1000s of devices and I need to work with it:

case "$1" in
    echo_var)
        echo "$2"
        ;;
4
  • 1
    I don't understand what you want to do. What does "pass a command to a file mean"? Give us detailed examples how the files look, what you want to do etc.
    – Sven
    Jan 8, 2015 at 11:56
  • @Sven I have added an edit that will hopefully clear things up Jan 8, 2015 at 12:01
  • Did you try to "quote" your hello world string? Kind of need to see the real example of your script to see what you're trying to do.
    – Optichip
    Jan 8, 2015 at 12:06
  • The command I want to run is to restart a service ie test.sh echo_var hello&&service apache2 restart. So both hello will be echoed and the service restarted. But after the space after service, the command is cut. Jan 8, 2015 at 12:09

3 Answers 3

3

Use double quote around your string

Below one should work fine

test.sh echo_var "hello world"
0
#!/bin/sh
echo_var="hello world"
echo  $echo_var

Are you looking for something like this?

#!/bin/sh
echo_var="Apache Server Restarted"
echo  $echo_var >> /var/log/ApacheRestart.log
service apache2 restart

So something like this might help you out.

3
  • I have put the bash file function I am trying to interact with Jan 8, 2015 at 12:14
  • Have you looked at stackoverflow.com/questions/13254425/…
    – Optichip
    Jan 8, 2015 at 12:19
  • I didn't write the sh file, it came on a device I am using. There are 1000s of these devices in use all with the same file. I am trying to interact with the existing file but pass it a second command ie to restart a service. So I need to know how to either substitute a space for another character or somehow be able to pass a command ie service apache restart with the first arugment Jan 8, 2015 at 12:25
0

The solution for me was the following:

./test.sh echo_var test1;what="service restart apache2";$what

This means the spaces are never caught by the script

Not the answer you're looking for? Browse other questions tagged .