Results 1 to 3 of 3

Thread: Bash script help

  1. #1
    Join Date
    May 2007
    Location
    Philadelphia
    Beans
    Hidden!
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Bash script help

    Since my ACPI handler.sh script isn't working like it should, I need to write my own. What it needs to do is test whether `acpi -a` equals "Adapter 0: on-line" or "Adapter 0: off-line". Here's what I've tried...

    Code:
    !#/bin/sh
    
    power_source = `acpi -a`
    if [$power_source = "Adapter 0: on-line"]
    then
        echo "Using AC power"
    else
        echo "Using battery"
    fi
    What it does, however, is print out "Using battery" even when acpi -a says the AC adapter is on-line. So I assume it's a problem with my if statement, right? What's wrong with it though, I haven't been able to figure out.

    So, any help would be greatly appreciated. Thanks!

  2. #2
    Join Date
    Jan 2007
    Beans
    66
    Distro
    Ubuntu Development Release

    Re: Bash script help

    Quote Originally Posted by Yes View Post
    Since my ACPI handler.sh script isn't working like it should, I need to write my own. What it needs to do is test whether `acpi -a` equals "Adapter 0: on-line" or "Adapter 0: off-line". Here's what I've tried...

    Code:
    !#/bin/sh
    
    power_source = `acpi -a`
    if [$power_source = "Adapter 0: on-line"]
    then
        echo "Using AC power"
    else
        echo "Using battery"
    fi
    What it does, however, is print out "Using battery" even when acpi -a says the AC adapter is on-line. So I assume it's a problem with my if statement, right? What's wrong with it though, I haven't been able to figure out.

    So, any help would be greatly appreciated. Thanks!
    $power_source is most likely empty, you shouldn't put spaces around the equal sign in variable assignment.

    Also, using the $power_source variable like that in the test statement doesn't preserve spaces. You should enclose the variable in double quotes. And also put spaces around the brackets in the test statement.

  3. #3
    Join Date
    May 2007
    Location
    Philadelphia
    Beans
    Hidden!
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Bash script help

    Ah I hadn't realized whitespace was so important in bash. Thank you!

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
  •