You are not logged in.

#1 2009-03-08 15:29:46

initbox
Member
Registered: 2008-09-27
Posts: 172

[Solved] [Bash] How can this NOT work?!?!

###### Samsung NC10 Commandcenter ######
#!/bin/bash

command=""
act=""
args=0
param=$2

if [ $# -eq 2 ]; then
    args=2
fi

add () {
if [ args!=2 ]; then
    echo "What do you want to activate?"
    echo -e "1) LAN\t2) WLAN\t3) Bluetooth"
    read act
fi
if [ args=2 ]; then
    act=$param
fi
    case $act in
        1)
        echo "Activating LAN."
        modprobe sky2;
        /etc/rc.d/network start;
        ;;
        2)
        echo "Activating WLAN."
        modprobe ath5k;
        /etc/rc.d/network start;
        ;;
        3)
        echo "Activating Bluetooth."
        modprobe btusb;
        modprobe bluetooth;
        ;;
        *)
        echo "Invalid action '$act'!"
        ;;
    esac
}

remove () {
if [ args!=2 ]; then
    echo "What do you want to inactivate?"
    echo -e "1) LAN\t2) WLAN\t3) Bluetooth"
    read act
fi
if [ args=2 ]; then
    act=$param
fi

    case $act in
        1)
        rmmod sky2;
        /etc/rc.d/network stop;
        ;;
        2)
        rmmod ath5k;
        /etc/rc.d/network stop;
        ;;
        3)
        rmmod btusb;
        rmmod bluetooth;
        ;;
        *)
        echo "Invalid action '$act'!"
        ;;
    esac
}

cmd () {
if [ args!=2 ]; then
    echo "What action do you want to perform?"
    echo -e "1) Suspend(RAM)\t2) Hibernate(HDD)"
    read act
fi
if [ args=2 ]; then
    act=$param
fi
    case $act in
        1)
        echo "Suspending to RAM."
        pm-suspend;
        ;;
        2)
        echo "Hibernating to HDD."
        pm-hibernate;
        ;;
        *)
        echo "Invalid action '$act'!"
        ;;
    esac
}

if [ $# -ne 0 ]; then
    case $1 in
        add)
        command=1
        ;;
        rm)
        command=2
        ;;
        cmd)
        command=3
        ;;
        *)
        command=$1
        ;;
    esac
fi

if [ $# -eq 0 ]; then
    echo -e "Entering interactive mode. \nWhat task would you like to perform? \n1) Activate\t2) Inactivate\t3) Command"
    read command
fi

case $command in
    1)
    add
    ;;
    2)
    remove
    ;;
    3)
    cmd
    ;;
    *)
    echo "Invalid parameter '$command'!"
esac

I'm trying to make a script to control the various functions of my awesome netbook, but even though my design is ingenious tongue, it doesn't work. I've tried everything and I just don't see why it can't work.

If you start it without arguments, it will ask interactively for the arguments and it works. It works if you pass the first argument, it asks you straight for the second one.

But it doesn't work if you give both arguments directly. I've tried pretty much everything and I'm about to start punching in walls.

It's my first real bash script though...

Last edited by initbox (2009-03-08 16:00:20)

Offline

#2 2009-03-08 15:40:24

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: [Solved] [Bash] How can this NOT work?!?!

this: "if [ args!=2 ]; then" (for example) doesn't look like bash...  Try something like:

if [ $args -ne 2 ]; then

Offline

#3 2009-03-08 15:44:11

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] [Bash] How can this NOT work?!?!

I have not tried it out. Here are some tips:
#!/bin/bash must be on line 1 to make any sense, but in this case it doesn't matter, because the default is /bin/sh

if [ x=y ] is assignment and will always succeed, it should be if [ x = y ]

Offline

#4 2009-03-08 15:49:45

initbox
Member
Registered: 2008-09-27
Posts: 172

Re: [Solved] [Bash] How can this NOT work?!?!

Hmm. Never mind. I just realised I need to have $ in the if-variables.

And now it actually works! Yay.

Last edited by initbox (2009-03-08 15:56:01)

Offline

#5 2009-03-08 15:54:27

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] [Bash] How can this NOT work?!?!

What is happening is that args is uninitialized. So it interprets:

[ -ne 2 ]

What you should do is:
[ -n "$args" -a $args -ne 2 ]

So it interprets first
[ -n "" ]

Which fails (edit: Which is false, rather)

Also mind the quotes. This is not what you want:

[ -n ]

That is always true.

Last edited by Procyon (2009-03-08 15:55:16)

Offline

#6 2009-03-08 15:55:32

moljac024
Member
From: Serbia
Registered: 2008-01-29
Posts: 2,676

Re: [Solved] [Bash] How can this NOT work?!?!

if [[ $args != 2 ]]; then

The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner.
--------------------------------------------------------------------------------------------------------------
But if they tell you that I've lost my mind, maybe it's not gone just a little hard to find...

Offline

#7 2009-03-08 15:58:14

initbox
Member
Registered: 2008-09-27
Posts: 172

Re: [Solved] [Bash] How can this NOT work?!?!

Now it works!

All I had to do was add $ in the if-variables and add spaces into the if-conditions.

Thanks guys. smile

Use it if you find it useful, don't care about credits or w/e.

Finished script:

#!/bin/bash
###### Samsung NC10 Commandcenter ######
command=""
act=""
args=0
param=$2

if [ $# -eq 2 ]; then
    args=2
fi

add () {
if [ $args -ne 2 ]; then
    echo "What do you want to activate?"
    echo -e "1) LAN\t2) WLAN\t3) Bluetooth"
    read act
fi
if [ $args = 2 ]; then
    act=$param
fi
    case $act in
        1|lan)
        echo "Activating LAN."
        modprobe sky2;
        /etc/rc.d/network start;
        ;;
        2|wlan)
        echo "Activating WLAN."
        modprobe ath5k;
        /etc/rc.d/network start;
        ;;
        3|bt)
        echo "Activating Bluetooth."
        modprobe btusb;
        modprobe bluetooth;
        ;;
        *)
        echo "Invalid action '$act'!"
        ;;
    esac
}

remove () {
if [ $args -ne 2 ]; then
    echo "What do you want to inactivate?"
    echo -e "1) LAN\t2) WLAN\t3) Bluetooth"
    read act
fi
if [ $args = 2 ]; then
    act=$param
fi

    case $act in
        1|lan)
        /etc/rc.d/network stop;
        rmmod sky2;
        ;;
        2)
        /etc/rc.d/network stop;
        rmmod ath5k;
        ;;
        3)
        rmmod btusb;
        rmmod bluetooth;
        ;;
        *)
        echo "Invalid action '$act'!"
        ;;
    esac
}

cmd () {
if [ $args -ne 2 ]; then
    echo "What action do you want to perform?"
    echo -e "1) Suspend(RAM)\t2) Hibernate(HDD)"
    read act
fi
if [ $args = 2 ]; then
    act=$param
fi
    case $act in
        1)
        echo "Suspending to RAM."
        pm-suspend;
        ;;
        2)
        echo "Hibernating to HDD."
        pm-hibernate;
        ;;
        *)
        echo "Invalid action '$act'!"
        ;;
    esac
}

if [ $# -ne 0 ]; then
    case $1 in
        add)
        command=1
        ;;
        rm)
        command=2
        ;;
        cmd)
        command=3
        ;;
        *)
        command=$1
        ;;
    esac
fi

if [ $# -eq 0 ]; then
    echo -e "Entering interactive mode. \nWhat task would you like to perform? \n1) Activate\t2) Inactivate\t3) Command"
    read command
fi

case $command in
    1)
    add
    ;;
    2)
    remove
    ;;
    3)
    cmd
    ;;
    *)
    echo "Invalid parameter '$command'!"
esac

Last edited by initbox (2009-03-14 12:12:26)

Offline

Board footer

Powered by FluxBB