Results 1 to 4 of 4

Thread: bash script based on lsb-release

  1. #1
    Join Date
    May 2010
    Location
    Kent, UK
    Beans
    106
    Distro
    Ubuntu

    bash script based on lsb-release

    Hey guys,
    I'm new to scripting and I'm trying to work on a script that will call out functions based on the version of ubuntu being run

    What I have so far:

    Code:
    #!/bin/sh
    version='lsb_release -sr'
    #check for version of ubuntu
    $version
    if [ $version == '16.04' ]
    then
        echo "Version of ubuntu is 16.04 running script"
    elif [ $version == '14.04' ]
    then
        echo "Version of ubuntu is 14.04 running script"
    elif [ $version == '12.04' ]
    then
        echo "Version of ubuntu is 12.04 running script"
    fi
    However the bash script just tells me it's too many arguments. I'm most likely going about this the wrong way but happy for someone that knows better that can provide some tips.

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: bash script based on lsb-release

    Code:
    version='lsb_release -sr'
    just assigns the string value - if you want the result of the command (aka command substitution) you need

    Code:
    version=$(lsb_release -sr)
    or the older deprecated equivalent using backticks

    (as a matter of style, I'd suggest a case statement rather than a sequence of if... then clauses)

  3. #3
    Join Date
    May 2010
    Location
    Kent, UK
    Beans
    106
    Distro
    Ubuntu

    Re: bash script based on lsb-release

    steeldriver that case stuff is amazing, looked it up and re-did the script all seems to be working now

    Code:
    #!/bin/sh
    version=$(lsb_release -sr)
    
    #check for version of ubuntu
    
    
    case $version in
    16.04)
        echo "Version of ubuntu is 16.04 running script"
        ;;
    14.04)
        echo "Version of Ubuntu is 14.04 running script"
        ;;
    12.04)
        echo "Version of Ubuntu is 12.04 running script"
    esac
    Thanks alot

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: bash script based on lsb-release

    Looks good

    You could also add a "catch all" like

    Code:
    *)
          echo "Version unrecognized"
          ;;
    Happy to help

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
  •