Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: bash scripting

  1. #1
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    bash scripting

    Is it possible with bash scripting to make a list of installed packages with
    Code:
    dpkg -l |grep (package name)
    or with some other (better) command.

    and then compare it to a list, that i have defined.

    The purpose of this would be to see if a system has the right packages installed, by comparing it to a list i have defined. That would be the "correct package list".

    So i quickly can see if an external system has all the correct packages or correct package versions, or if it needs an upgrade.

    Does this make any sence?

    Also i'am reading some guides and tutorials. And their keeps being examples of the $...... what does this do in bash? Does this still means it a variable?

    Thanks on advance.

  2. #2

    Re: bash scripting

    You can do it with dpkg or apt but I am not sure of the arguments required.
    Noli illegitimi carborundum

  3. #3
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash scripting

    Maybe its just me.

    But when i try to do the following

    Code:
    #!/bin/bash
    
    $ echo "I am $logname"
    
    $ echo "My computer is $(hostname)"
    i make the script executable. Run it, and then it says that neither of the commands are found......... why... since logname and hostname are commands i can run in the terminal.

  4. #4

    Re: bash scripting

    http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
    http://tldp.org/LDP/abs/html/index.html

    Have a look through those they are very good resources.
    Noli illegitimi carborundum

  5. #5
    Join Date
    Dec 2009
    Location
    germany
    Beans
    1,020
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash scripting

    hi
    try this as a script:
    #!/bin/bash
    dpkg --get-selections | grep -v deinstall > allepakete.`date +%y.%m.%d.%H.%M`
    exit $?

    and to get all the packets ( from a spezial date ) back:
    dpkg --set-selections < allepakete.yy.mm.dd.hh.mm

    ( answer with: i )
    ciao
    Last edited by rnerwein; June 3rd, 2010 at 11:11 AM.
    "What is the robbing of a bank compared to the FOUNDING of a bank?" Berthold Brecht

  6. #6
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: bash scripting

    Quote Originally Posted by leg View Post
    They are not, at least not for beginners. They don't teach you good practice and in some cases they're just plain wrong. I recommend http://mywiki.wooledge.org/BashGuide

    @Drenriza

    The $ just represents the bash prompt that you see in an interactive bash shell. The default prompt in ubuntu looks something like:

    Code:
    username@hostname:/current/dir$
    The prompt can be customized quite extensively, so guides commonly just use a $ to represent it. You do not actually type in that $ at the start of a line, neither in an interactive shell nor a script.

    What is the format of the file you have? Is it just package names, one package per line, or is it dpkg -l output?

  7. #7
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash scripting

    The $ just represents the bash prompt that you see in an interactive bash shell
    Hehe, ive spent hours thinking about this while i also did other stuff. About why it didnt work. And no guides have "told" that the $ is not actually to be typed. Ty

    Code:
    cristian@cristian-laptop:~/Dokumenter/scripts$ ./script\ 1.0 
    I am 
    My computer is cristian-laptop
    cristian@cristian-laptop:~/Dokumenter/scripts$
    And now its working. THANKS alot mate. This actually made my day. From being a day to be a day


    What is the format of the file you have? Is it just package names, one package per line, or is it dpkg -l output?
    I'm not quite sure what you mean.

    I would like to make a static list of packages that should be installed on a system. I would then like to compare external systems to this static list. To make sure that all necessary packages are installed. Or if the system needs to be upgraded.

    For me to take a decision on weather the system needs an upgrade or not, i need the script to tell me if their a packages that are on the static list that arnt on the external system. And then tell me what packages that are missing.

    Hope it makes sence.
    Last edited by Drenriza; June 3rd, 2010 at 02:07 PM.

  8. #8
    Join Date
    May 2010
    Location
    Carmarthenshire, Wales
    Beans
    18
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: bash scripting

    If you go to /var/cache/apt/archives ,unless you have deleted it ,you will find a list all installed package files there .

    If you use vim you could go into the file and do :w filename_you_like ; This will copy all this into a new
    file for you , if the file already exists use : w >> filename to append an to existing file .
    Last edited by P.ap G; June 3rd, 2010 at 04:46 PM. Reason: Typo

  9. #9
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: bash scripting

    Code:
    aptitude -F "%?p" search "~i"
    will list all installed packages. Unfortunately aptitude appends a lot of spaces after each packagename to make each line of equal length. There might be a way to tell aptitude not to do that, but at the present I don't feel like sifting through the documentation (it's quite large), so instead we'll just pipe it through tr to remove all the spaces.

    Now, let's say you want vim, hello and coreutils to be installed, then put those in a file, one package per line. E.g. packagelist.txt:
    Code:
    vim
    hello
    coreutils
    The following grep command will output all lines in packagelist.txt that did not appear in installed_packages.txt
    Code:
    aptitude -F "%?p" search "~i" | tr -d ' ' > installed_packages.txt
    grep -Fxv -f installed_packages.txt packagelist.txt
    You can also use comm, but it only operates on sorted files, so
    Code:
    comm -13 <(sort installed_packages.txt) <(sort packagelist.txt)
    See http://mywiki.wooledge.org/BashFAQ/036

  10. #10
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Arrow dpkg, grep, awk, diff

    The answer is quite complicated:


    List packages:
    To get the lists of packages use this command:
    Code:
    dpkg -l | grep ^ii | awk '{print $2}' > list-of-packages
    Code:
    dpkg -l - list package info
    grep ^ii - ignore lines that don't have packages on
    awk '{print $2}' - get the second column (the package name)
    > list-of-packages - output file (can be anything/anywhere you want)

    Compare packages:
    Once you have your two package lists you can compare them using this command:
    Code:
    diff correct-packages my-packages | grep -E '^>|^<'
    Code:
    diff - find the difference between the files
    correct-packages - list of packages you want
    my-packages - list of packages you actually have
    grep -E '^>|^<' - list only the changed packages
    The output:
    Code:
    > - extra package
    < - missing package

    Note:
    A lot of system packages change from version to version, so this might prove troublesome. I would just make a list of your favorite packages and then write a script to automatically install them for you.
    I would also recommend http://www.linuxcommand.org/ - I learnt bash from that site and it explains both the interactive terminal and scripts.
    Last edited by Penguin Guy; June 4th, 2010 at 02:54 PM.

Page 1 of 2 12 LastLast

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
  •