batmancn
batmancn

Reputation: 477

How to run shell/python script in shell script?

I'm testing my shell script and python script together, so I write a shell script to call these two script in loop like this:

while ((1)) ; do
    /usr/local/bin/ovs-appctl dpdk/vhost-list
    sleep 10
    /usr/local/bin/ovs-appctl dpdk/virtio-net-show n-f879ac2f
    sleep 10
    /usr/local/bin/ovs-appctl dpdk/virtio-net-show n-434ab558
    sleep 10
    /usr/local/bin/ovs-appctl dpdk/virtio-net-show i-brpri-p
    sleep 10
    `sh ./env_check`
    `cat output.txt`
    sleep 10
    `python vm_qga_tool /opt/cloud/workspace/servers/6608da87-e374-4796-adb1-8faa29f49e9a/qga.sock connect:172.16.0.1`
    sleep 10
done

but the result report error:

test-dpdk-virtio-net-show.sh: line 13: Checking: command not found
test-dpdk-virtio-net-show.sh: line 15: {"session":: command not found

Line13 is cat output.txt, line15 is python vm_qga_tool ....

the output.txt is the result of line12 sh ./env_check, and the output of vm_qga_tool is like this:

{"session"...

so how to fix this? How to cat result of the shell script in shell script?

Upvotes: 1

Views: 281

Answers (2)

Anubis
Anubis

Reputation: 7425

sh ./env_check line will be replaced by the output of the script ./env_check. Current shell will try to execute them as commands. In your case it fails as, luckily, bash couldn't interpret them as commands. This could have been a disaster. For example, if your script produced an output like rm -rf *, you can imagine what would happen.

If you want to display the result of ./env_check you have to prepend the line with echo, so that the output of ./env_check will be fed to echo.

echo `sh ./env_check`

Or if you want to capture the output into a variable you do the following.

out=`sh ./env_check`

Same goes to cat ... and python ...

However use of back-ticks is discouraged. Instead use $(...). (Read more here.)

...
sleep 10
echo $(sh ./env_check)
echo $(cat output.txt)
sleep 10
echo $(python vm_qga_tool /opt/cloud/workspace/servers/6608da87-e374-4796-adb1-8faa29f49e9a/qga.sock connect:172.16.0.1)
sleep 10
...

However, you don't need to do any of that anyway. If you don't need to capture the command output, you can directly invoke the scripts, instead of invoking through a sub-shell.

...
sleep 10
sh ./env_check
cat output.txt
sleep 10
python vm_qga_tool /opt/cloud/workspace/servers/6608da87-e374-4796-adb1-8faa29f49e9a/qga.sock connect:172.16.0.1
sleep 10
...

Upvotes: 2

Toandd
Toandd

Reputation: 320

The issues occurred because your shell accepted output of sh ./env_check and python vm_qga_tool ... as commands, so it will try to run those commands. To fix it you should eliminate "" symbol fromsh ./env_checkorpython vm_qga_tool ...`

Upvotes: 0

Related Questions