Results 1 to 8 of 8

Thread: help with a script

  1. #1
    Join Date
    Sep 2010
    Beans
    208
    Distro
    Ubuntu 10.04 Lucid Lynx

    help with a script

    I need to implement in a larger script of mine, bash code that will check if some packages are installed, if yes, do something, if not do something else.
    can someone give me an example code of how should I do it?

  2. #2
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: help with a script

    You can use dpkg(1) to test for installed packages:
    Code:
    #!/bin/sh
    pkglist="dpkg foobar"
    for PKG in $pkglist
    do
        dpkg -l $PKG >/dev/null 2>/dev/null
        if [ $? -eq 0 ]; then
            echo "Package $PKG is installed"
        else
            echo "Package $PKG is NOT installed"
        fi
    done
    Code:
    $ ./pkgtest.sh 
    Package dpkg is installed
    Package foobar is NOT installed

  3. #3
    Join Date
    Sep 2010
    Beans
    208
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: help with a script

    Thank you! this is just what I needed!

  4. #4
    Join Date
    Sep 2010
    Beans
    208
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: help with a script

    hey, just another quick question.
    In the script I have a variable that contains a string with 4 letters.
    How can I split it into to two variables, one that contains the first three letters and another that contains only the last letter?
    edit: nevermind, I used offsets.
    Last edited by Avidanborisov; June 25th, 2011 at 12:28 PM.

  5. #5
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,573
    Distro
    Ubuntu

    Re: help with a script

    Code:
    $ f="abcd"
    $ g="${f:0:3}"
    $ h="${f:3:1}"
    $ echo "$g"
    abc
    $ echo "$h"
    d
    ***Edit Oh you got it. cool

  6. #6
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: help with a script

    My mind automatically thought of using sed(1). Offsets are bash-specific and not part of the standard shell, so make sure you use the proper she-bang (#!/bin/bash).

  7. #7
    Join Date
    Sep 2010
    Beans
    208
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: help with a script

    For the end - My script is done. Can anyone suggest me a good place to host it with option to edit it and to be accessible via wget?

  8. #8
    Join Date
    Sep 2010
    Beans
    208
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: help with a script

    BTW, how do I upload files to launchpadlibrarian?

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
  •