3

I'm using optirun from bumblebee. It is designed to start my 2nd GPU, run the command given, and shut down the 2nd GPU at the end.

A simplified example:

optirun echo test | cat

However there's a bug in optirun that requires that I now run a follow up command to force the GPU to shut down.

Can I easily wrap some complex command such as echo test | cat in a shell script such that I can run optirun, and then follow that up at the end with command (my workaround to the bug)?

The quoting and all seems to be an issue preventing me from doing this with a simple shell script.

  • 1
    But what is the actual problem you are suffering? You told us that you can write a shell script that has some complex command, and that you have some problem with quoting. Therefore, you actually can't write the script. Maybe it will be better to post the script that you have and describe why you believe it does not work. – grochmal Dec 23 '16 at 2:27
  • I need to run an rmmod command after optirun to get around optirun not shutting down the GPU (a bug that will be fixed in the next version). optirun takes any complex command such as the one I suggested. So I'd like my script just to call optirun with whatever I send it, and then when it's done execute the rmmod workaround. I could be overthinking this. – David Parks Dec 23 '16 at 2:31
5

I'm confident you're simply after $@, the argument list to a script.

Trivial example:

$ cat >cc.sh <<EOF
#!/bin/sh
head "\$@"
echo I AM DONE
EOF
$ chmod 755 cc.sh

Works with arguments:

$ ./cc.sh cc.sh 
#!/bin/sh
head "$@"
echo I AM DONE
I AM DONE

Works with STDIN/STDOUT

$ cat cc.sh | ./cc.sh | tail -n 2
echo I AM DONE
I AM DONE

Works with a mixture:

$ cat cc.sh | ./cc.sh -n 2 | tail -n 2
head "$@"
I AM DONE

Therefore:

#!/bin/sh
optirun "$@"
rmmod <mod> [perhaps >/dev/null 2>&1 if you need to ignore errors from rmmod]
  • 1
    The $@ should be inside double quotes, to preserve spaces etc in the arguments. – icarus Aug 2 '17 at 23:16
  • @icarus - you're 100% right! I've just tested this, thanks for making me learn yet another details – grochmal Aug 4 '17 at 11:16
  • @grochmal: you may want to read : mywiki.wooledge.org/BashPitfalls (to learn about quoting, and a lot of other very very useful things to know about) (for almost every shells, not just bash) – Olivier Dulac Aug 4 '17 at 12:56
4

If in your example of optirun echo test | cat you just want the echo test to be given to optirun, which is what happens when you type the example at the command line then

#!/bin/sh
optirun "$@"
rmmod workaround

is all you need. Put in a script called "myopti" and you can use myopti echo hello | cat and get the cleanup.

  • 1
    Lol, 7 seconds of difference (but you were first) – grochmal Dec 23 '16 at 2:41
  • 1
    @grochmal, but you wrote so much more! I must take a typing lesson or 20. I think I can even earn a disciplined badge if I delete a +3 answer (hint hint) and you can get your just reward for your more comprehensive answer. – icarus Dec 23 '16 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.