Jump to content

check exit status


rubing

Recommended Posts

Yo homeys.

 

I am a linux programming noob, greener than your lawn, although i do know php, so not a complete rube.  I  am hoping I can get some help on writing my first script.

 

I am using a blackberry to get internet on my laptop (tethering).  I am damn damn sick and damn damn infuriated at having to always type the commands manually.  So, I wrote the following script:

#!/bin/bash
bcharge
pppd call barry-sprint&

 

This works, but is not really robust.  First, I really need to be able to check whether or not bcharge finds a phone plugged in.  If it doesn't detect anything then i'd want to wait a couple secs and try again, before failing gracefully.  Well, you don't need to hear about all that.  Basically I just want to know how to check whether the phone is plugged in.  Is this what they call exit status??

 

Link to comment
Share on other sites

You can check the exit status of the last command executed by looking at the $? variable. 0 meens success, anything else is some sort of error. eg;

 

#!/bin/bash
bcharge
if [ "$?" -eq 0 ] ; then
  echo "bcharge successfull"
  pppd call barry-sprint &
fi

 

You can also chain commands together using && (and) or || (or). eg;

 

#!/bin/bash
bcharge && pppd call barry-sprint &

 

This will only execute pppd call barry-sprint & if bcharge is successfull.

 

As for whether or not bcharge finds a phone plugged in, the first thing I would do would be to manually look for it within your /dev directory.

Link to comment
Share on other sites

Hey Thorpe!

 

How's the php 5.3 install working?

 

well, I changed that script to read as follows:

 

Script blacknet:

#!/bin/bash

#start bcharge up and send any output to the void

bcharge &> /dev/null

THERE=$?

echo "checking for phone..."

if [ $THERE -eq 0 ];

then

echo "yay, we found you're phone"

#kill any running instances of ppp, before starting it up

pkill ppp*

pppd call barry-sprint&

else

echo "boo, we didn't find jack"

#this needs to sleep and check again first

fi

 

It works, but it runs entirely in the background.  So that if I type blacknet.  I get returned to a prompt and then as commands get executed they pop up.  I would rather the program take away the prompt until its finished running.

Link to comment
Share on other sites

You can kill it by setting it to an empty string. Seems a hack though.

 

PS1=""

 

You might want to store the current prompt in a temp var so you can restore it when your done. eg;

 

TMP="$PS1"
PS1=""
## do your thing
PS1="$TMP"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.