Results 1 to 6 of 6

Thread: Bash Script, lsusb

  1. #1
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,126
    Distro
    Ubuntu 10.04 Lucid Lynx

    Bash Script, lsusb

    Hi all I have a usb device that I want to poll periodically.
    the device is listed as:
    Code:
    lsusb | grep "Tiger Jet Network"
    Bus 002 Device 004: ID 06e6:c200 Tiger Jet Network, Inc. Internet Phone
    all I want to know is when its plugged in or not plugged in.
    how might I do that?

    Im just starting to read about polling, lsusb tells me what I want to know, but I dont know how to put it into a true false statement in a bash script.
    Code:
    #!/bin/bash
    
    result=0
    result=`lsusb | grep -e "Tiger Jet Network, Inc. Internet Phone"`
    if $result !=0
    then 
    	echo "Great execute some code" 
    		else
    			echo "Device was unpluged"
    			exit
    fi
    That is what I had tried thus far, but it doesnt work.
    can someone point me in the direction of being able to execute code if
    lsusb says my device is plugged in?
    Last edited by conradin; October 5th, 2012 at 02:29 PM. Reason: cant spell / code
    ~Conradin~

  2. #2
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Bash Script, lsusb

    Not sure if this is exactly correct, but I think you can just use the status of the shell exit code in that conditional statement (also have a look at your conditional statement construct...not sure that's quite correct):

    Code:
    #!/bin/bash
    
    result=$(lsusb | grep -e "Tiger Jet Network, Inc. Internet Phone")
    
    if [ $? -ne 0 ]; then
            printf "Device is unplugged\n"
    else
            printf "Great execute some code\n"
    fi
    Give that a shot to see if it works as you expect

  3. #3
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,126
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash Script, lsusb

    That does work exactly as I want, Thank you very much!
    I think I have a better understanding of $ now too.
    But there are persistently things I dont understand about $. Like, what is the meaning of "$?" ..?



    Quote Originally Posted by drmrgd View Post
    Not sure if this is exactly correct, but I think you can just use the status of the shell exit code in that conditional statement (also have a look at your conditional statement construct...not sure that's quite correct):

    Code:
    #!/bin/bash
    
    result=$(lsusb | grep -e "Tiger Jet Network, Inc. Internet Phone")
    
    if [ $? -ne 0 ]; then
            printf "Device is unplugged\n"
    else
            printf "Great execute some code\n"
    fi
    Give that a shot to see if it works as you expect
    ~Conradin~

  4. #4
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    special variables

    There are special variables that contain various pieces of useful information. $? provides the exit status of the last program run. $$ is the process ID of the current script.

    Scroll down this page to see more:
    http://tldp.org/LDP/abs/html/internalvariables.html

  5. #5
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: special variables

    Quote Originally Posted by Lars Noodén View Post
    There are special variables that contain various pieces of useful information. $? provides the exit status of the last program run. $$ is the process ID of the current script.

    Scroll down this page to see more:
    http://tldp.org/LDP/abs/html/internalvariables.html
    That link contains inaccurate informations. $BASH or $whatever is not an internal variable or builtin variable. It's a parameter expansion. BASH is a shell variable and whatever is a parameter

    See:
    Code:
    man bash | less +/" +  Parameter Expansion"
    $ is the expansion character. It's a special character or metacharacter used in substitutions: parameter and variable expansion, command substitution and arithmetic expansion.


    ? and $ (yep, whatever includes the $ sign too ) are special parameters.

    $? expands to the exit code of the most recently completed foreground command.

    $$ expands to the PID (process ID number) of the current shell. The first $ sign is a the expansion character and the second is the special parameter.
    Last edited by sisco311; October 6th, 2012 at 08:56 AM.

  6. #6
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: special variables

    Quote Originally Posted by sisco311 View Post
    That link contains inaccurate informations.
    Thanks. The e-mail address of the author can be found on this link: http://tldp.org/LDP/abs/html/
    Could you report the mistake to him?

    In perl, these are actual special variables Possibly it is the same in other languages and part of the mix up.
    Last edited by Lars Noodén; October 6th, 2012 at 09:31 AM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •