0

I am trying to create a simple bash script that will launch an app from the command line and redirect STDOUT and STDERR output to /dev/null. I also want to include functionality that provides feedback if the script fails.

The script almost works, but I can't get apps to launch in the background. I have tried using nohup, disown, wrapping the if statement in "(if...fi)&", wrapping the else statement in "{... ;}&", but everything I've tried has either introduced new problems or not worked at all.

Any suggestions?

Here is a basic version of what I'm doing:

#!/bin/bash
read -p "Enter program name: " APP
if 
  $APP 2>&1 | grep -q "command not found"
then
  echo "That didn't work."
else
  $APP >/dev/null 2>&1 &  
fi
metaflops
  • 115
  • 9
  • so "can't get apps to launch in background" means that it starts, but the shell script hangs until you stop the app OR that apps just don't lauch at all? Also, are you talking about a userAPP like an Xwindows progam with a GUI or ?? Good luck. – shellter Sep 27 '12 at 21:35
  • You are running the application twice. The first time it is not run in the background (in the `if $APP` part). `type` might be more suitable for this kind of a condition. – choroba Sep 27 '12 at 21:39

1 Answers1

2

Well, you're launching the application twice and I think the fist one is the one that's hanging, when you're checking if it exists by piping it's output to grep. You probably want to do something like this Check if a program exists from a Bash script to check if it exists first instead of launching the application.

Community
  • 1
  • 1
PherricOxide
  • 15,493
  • 3
  • 28
  • 41
  • Yep! That was it thanks! And thanks to choroba too! Works like a charm now. Here was my final code:`#!/bin/bash read -p "Enter program name: " APP if type $APP &>/dev/null; then $APP >/dev/null 2>&1 & else echo "That didn't work." fi` – metaflops Sep 27 '12 at 23:09
  • Here's a version with semicolons acting as line breaks for clarity: `#!/bin/bash read -p "Enter program name: " APP; if type $APP &>/dev/null; then $APP >/dev/null 2>&1 &; else echo "That didn't work."; fi` – metaflops Sep 27 '12 at 23:22