Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Bash - General programming - How to send zypper output as a line by line string

  1. #1
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,307

    Default Bash - General programming - How to send zypper output as a line by line string

    Hello.
    I want to do some work on output of a zypper command.
    It seems that the zypper output is not simple lines terminated by /n.

    I miss end of lines for what zypper has found.
    And in the function, the program does not wait for user to press a key.

    here is my program :
    Code:
    #! /bin/sh
    ########################################
    #                                                    #
    #    dnla_test_pre_requisite_dnla            #
    #                                                    #
    ########################################
    
    #
    #ensure running as root
    #
    if [ "$(id -u)" != "0" ]; then
      exec sudo "$0" "$@"
    fi
    
    function DO_IT () {
    
        local NB_VAR
        local DOLLAR_0
        local DOLLAR_ARO
        local B_VAR1
    
        NB_VAR="$#"
        DOLLAR_0="$0"
        DOLLAR_ARO="$@"
        B_VAR1="$1"
    
        echo
        echo "=================================================="
        echo
        echo
        echo "   -     NB_VAR=$NB_VAR"
        echo "   -   DOLLAR_0=$DOLLAR_0"
        echo "   - DOLLAR_ARO=$DOLLAR_ARO"
        echo
        echo
        echo "   - B_VAR1=$B_VAR1"
        echo
        echo
        echo
        echo "=================================================="
        echo
        echo
    
        echo
        echo "Type a key to continue"
        read MY_TEMP
        echo
    
    }
    
    export -f DO_IT
    
    
    
    echo .
    echo .
    echo .
    
    for A_LIB in libexif libjpeg libid3tag libFLAC libvorbis libsqlite3 libavformat ; do    # (libavformat : the ffmpeg libraries)
    
    #    zypper se -s $A_LIB | while read -r line; do DO_IT "$line"; done  
    
    #    zypper se -s $A_LIB | xargs -0 -n1 -I '{}' bash -c 'DO_IT "{}"' 
    
    #    zypper se -s $A_LIB | xargs -0 -L 1 -I '{}' bash -c 'DO_IT "{}"' 
    
        zypper se -s $A_LIB | xargs -I '{}' bash -c 'DO_IT "{}"' 
    
    done
    I have tried different things but i did not success.

    Any help is welcome
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.2 -- ASUS G731GV KDE leap 15.2 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  2. #2
    Join Date
    Aug 2008
    Location
    Mexico and Sweden
    Posts
    1,659

    Default Re: Bash - General programming - How to send zypper output as a line by line string

    If you direct the zypper output to a file as with
    Code:
    sudo zypper search xxx > filename
    each line will terminate with a 0a hex which you can change to what ever you want with "tr".

  3. #3

    Default Re: Bash - General programming - How to send zypper output as a lineby line string

    As is often the case, it may help if you explain a few quick things:

    1. What is the use/business case for this?

    2. What is the exact output you are trying to manipulate? Paste it using
    the CODE/# tags using the '#' button in the web UI.

    3. How would you like the output to look?

    If we have the before/after we can perhaps come up with a way to script
    it, or if we can understand the business case then perhaps wee can find a
    better way to do what you are after.


    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below.

    If you want to send me a private message, please let me know in the
    forum as I do not use the web interface often.

  4. #4
    Join Date
    Jun 2009
    Location
    Mangfall, Germany
    Posts
    1,558

    Default Re: Bash - General programming - How to send zypper output as a line by line string

    if a list of all installed packages is require try,

    rpm -qa | sort -df > file

    the output will be something like,
    Code:
    aaa_base-13.2+git20140911.61c1681-27.2.x86_64
    aaa_base-extras-13.2+git20140911.61c1681-27.2.x86_64
    aaa_base-malloccheck-13.2+git20140911.61c1681-27.2.x86_64
    accountsservice-0.6.42-6.4.x86_64
    accountsservice-lang-0.6.42-6.4.noarch
    acl-2.2.52-10.15.x86_64
    acpica-20170119-1.14.x86_64 
    ..... etc.

  5. #5
    Join Date
    Sep 2012
    Posts
    7,090

    Default Re: Bash - General programming - How to send zypper output as a line by line string

    Quote Originally Posted by jcdole View Post
    It seems that the zypper output is not simple lines terminated by /n.
    What is "/n"?

    And in the function, the program does not wait for user to press a key.
    Because "read" does not "wait for user" - it waits for a line on standard input. You do realize that pipe connects standard output of one program to standard input of your program? And that read simply gets whatever you pipe into this function?

  6. #6
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    13,295
    Blog Entries
    2

    Default Re: Bash - General programming - How to send zypper output as a line by line string

    IIRC "/n" is one of those "features" inserted at the end of the line when a Windows text editor saves the file... and usually then has to be removed before you can properly edit the file in Linux.

    You might append the output each time you run your loop...

    ie.
    function >> outputfile

    Don't simply redirect as follows or you'll over-write the file each time instead of appending

    Don't
    function > outputfile

    Similarly,
    If you wish to print to stdout, then echo the function's result as a last line in each loop.

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  7. #7

    Default Re: Bash - General programming - How to send zypper output as a line by line string

    Hi,

    Like what some folks already said. If you can provide us the output or if you can try to explain what are you trying to achieve.
    Also you have the sh shebang but calling bash -c in your script? Please explain what are you trying do.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  8. #8

    Default Re: Bash - General programming - How to send zypper output as a line by line string

    Hi,

    zypper can handle multiple args i.e. packages so the loop is not needed unless of course you need the loop (I hope not).

    Code:
    zypper se -s libexif libjpeg libid3tag libFLAC libvorbis libsqlite3 libavformat
    In bash you can use an array to store the package names. Note you can change packages to anything you want since it is just an array name.

    Code:
    packages=(libexif libjpeg libid3tag libFLAC libvorbis libsqlite3 libavformat)
    Code:
    zypper se -s "${packages[@]}"
    In sh one can (ab)use the "$@" which the only array.

    Code:
    set -- "$@" libexif libjpeg libid3tag libFLAC libvorbis libsqlite3 libavformat
    and print the output

    Code:
    printf '%s\n' "$@"
    using zypper

    Code:
    zypper se -s "$@"
    Just guessing what are you trying to do here...
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  9. #9
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    13,295
    Blog Entries
    2

    Default Re: Bash - General programming - How to send zypper output as a line by line string

    Without looking too hard at exactly what is happening, the core of the code appears to be the following loop which evaluates various libraries, extracts some codec information and then performs a zypper source search.
    Code:
    for A_LIB in libexif libjpeg libid3tag libFLAC libvorbis libsqlite3 libavformat ;do    # (libavformat : the ffmpeg libraries)
        zypper se -s $A_LIB | xargs -I '{}' bash -c 'DO_IT "{}"' 
    
    
    done
    I wouldn't know if the syntax is flawed or not, but the higher level structure suggests he's building a list so likely wants to print that list either to stdout or a file.

    So, I suggested that this "For" loop should re-direct output in the ways I described to stdout or file each time the loop executes.

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  10. #10
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,307

    Default Re: Bash - General programming - How to send zypper output as a line by line string

    Quote Originally Posted by ionmich View Post
    If you direct the zypper output to a file as with
    Code:
    sudo zypper search xxx > filename
    each line will terminate with a 0a hex which you can change to what ever you want with "tr".
    I will try it.
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.2 -- ASUS G731GV KDE leap 15.2 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •