Results 1 to 9 of 9

Thread: Bash scripting help, can't get functions to operate and not sure why.

  1. #1
    Join Date
    Aug 2008
    Beans
    20

    Bash scripting help, can't get functions to operate and not sure why.

    So, the problem is that only segments of this script work (there is much more to it then what i posted) as you can see i have tried various forms in which to format this, and very little has worked, functions don't seem to work at all, regardless of where they are called from. I have done a bunch of C before but this is doing my head in, ive looked at many examples that are formatted identically and work, but mine does not.

    Please help, going crazy.

    Code:
    #!/bin/bash
    
    
    echo "This script only understands yes and no, please keep to these answers where appropriate."
    
    
    #read saved path if it exists
    
    if [ ! -f ~/.contentpath]; then touch ~/.contentpath; else PATH=[$(head -n 0 ~/.contentpath)]; fi
    
    
    
    Testinstalled () {
    
    if [$PATH=""]; then
    
    Install
    
    else Help
    
    fi
    
    }
    
    
    Help () {
    
    echo "Where * is the name of a app managed by this script."
    
    echo "Usage: content install|help|restart|update*|start*|autostarton|autostartoff"
    
    echo "More to come"
    
    }
    
    #set a path and download files
    
    
    
    Install () {
    
    if [$PATH=""]; then
    
    echo "Please enter a path to install the apps to, ~/ (home) is nominal."
    
    read PATH
    
    else echo "already installed!"
    
    fi
    
    
    
    echo "Install sabnzbd?"
    
    read SABNZBD
    
    echo "Install sickbeard?"
    
    read SICKBEARD
    
    echo "Install couchpotato?"
    
    read COUCHPOTATO
    
    echo "Install mylar?"
    
    read MYLAR
    
    echo "Install headphones?"
    
    read HEADPHONES
    
    
    
    if [$SABNZBD="yes"]
    then updatesabnzbd
    
    else :
    
    fi
    
    
    
    if [$SICKBEARD="yes"]
    
    then updatesickbeard
    
    else :
    
    fi
    
    
    
    if [$COUCHPOTATO="yes"]
    
    then updatecouchpotato
    
    else :
    
    fi
    
    
    
    if [$MYLAR="yes"]
    
    then updatemylar
    
    else :
    
    fi
    
    
    if [$HEADPHONES="yes"]
    
    then updateheadphones
    
    else :
    
    fi
    
    
    
    echo "All selected options have been installed."
    
    }

  2. #2
    Join Date
    Sep 2006
    Beans
    7,341
    Distro
    Lubuntu Development Release

    Re: Bash scripting help, can't get functions to operate and not sure why.

    Don't the functions all have to go first, before the part of the script they are called from?

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,323
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash scripting help, can't get functions to operate and not sure why.

    why on earth is your code so spaced out o.O

    the most obvious failing is improper syntax of conditions. You see, in bash [x=y] is space dependent.
    Code:
    if [_"$COUCHPOTATO"_=_"yes"_]
    [] conditions are not parsed like you would expect in other programming languages, in shells [ is simply a command and it gets a list of parameters just like any other (] is simply a closing marker). Obviously parameters have to be delimited with spaces
    in other words it's "[" "$couchpotato" "=" "yes" "]"
    Given that [ ] is an ordinary command you don't even have to use it with if if you feel like it
    Code:
    $ [ "a" = "a" ] && echo true || echo false
    true
    $ "[" "a" "=" "a" "]" && echo true || echo false
    true


    also always put $var in "". If the value is null or has whitespaces you get unexpected results
    unquoted variable in [ $null = yes ] expands to nothing at all (not empty string) so the result is [ = yes ] which doesn't make sense

    Code:
    $ x=''
    $ [ $x = "a" ] && echo true || echo false
    bash: [: =: unary operator expected
    false
    [ "$x" = "a" ] && echo true || echo false
    false
    Last edited by Vaphell; August 4th, 2013 at 01:54 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Mar 2007
    Location
    Denver, CO
    Beans
    7,554
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash scripting help, can't get functions to operate and not sure why.

    vaphell

    Nice advice. I read somewhere that variables should also include brackets but is this overkill. Let me give you an example using your code above

    $ x=''
    [ "${x}" = "a" ] && echo true || echo false

    Are these brackets necessary?

  5. #5
    Join Date
    Mar 2008
    Beans
    1,219

    Re: Bash scripting help, can't get functions to operate and not sure why.

    Curly brackets are optional unless you need them for disambiguation (i.e. to denote where the variable name ends in a string). Example:
    Code:
    $foo="${test}string"
    Last edited by prodigy_; August 4th, 2013 at 03:22 PM.

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,323
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash scripting help, can't get functions to operate and not sure why.

    yes, mostly optional. To disambiguation stuff I'd add the case of positional parameters. They can go as $1... $9 but ${10} because $10 is treated as $10
    Code:
    $ test_params() { echo "[$1][$10][${10}]"; }
    $ test_params a b c d e f g h i j k
    [a][a0][j]


    curly brackets also are used for many nifty operations on the value
    eg
    Code:
    ${x//a/b} #replace all a's with b's
    ${x^} #capitalize first char
    ${x^^} #capitalize all
    Code:
    $ x=baobab
    $ echo ${x//a/b} ${x^} ${x^^}
    bbobbb Baobab BAOBAB
    http://www.gnu.org/software/bash/man...eter-Expansion

    you could think of ${x} as a no-modifier version of the concept which shorthands to $x for convenience
    Last edited by Vaphell; August 4th, 2013 at 03:44 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Aug 2008
    Beans
    20

    Re: Bash scripting help, can't get functions to operate and not sure why.

    Thanks heaps guys, this should get me going again when i get some time to have another go. You have actually been more helpful than the bash pages.

  8. #8
    Join Date
    Aug 2008
    Beans
    20

    Re: Bash scripting help, can't get functions to operate and not sure why.

    Also it was so spaced out because it was copied from notepad as i was on my windows machine, sorry it is so ugly haha.

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,323
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash scripting help, can't get functions to operate and not sure why.

    You have actually been more helpful than the bash pages.
    well, man pages are a collection of facts, sorted by category, they sort of expect you to be on the same page when it comes to vocabulary and knowledge how the system works and are most useful when you need a detail or two that you know it's there somewhere. Overwhelming amount of meticulously sorted facts does nothing of value to noobs so they are better off with a 'getting started' style of prose where concepts are gradually introduced in plain language and practised. You don't learn English from reading a dictionary, right?
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

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
  •