2

I am making a search and install PPA script for Ubuntu. This script with zenity works 80%, the problem is when this script searches; displays only the first line. I need all lines

#!/bin/sh

# simple search and install PPA
# by David Vásquez


if [ $# -gt 0 ] ; then
    echo "$*"
else
    echo "No input"
exit
fi

code=$*

cat /dev/null > /tmp/ppa
cat /dev/null > /tmp/ppa-url-tmp


mojito=$(curl https://launchpad.net/ubuntu/+ppas?name_filter=$code | grep -e '+archive/' | grep "$code" | awk -F'<td><a href="/~' '{print $2}' | awk -F'">' '{print $1}' | uniq | tr -d '~')

echo $mojito | tr ' ' '\n' | tee -a /tmp/ppa-url-tmp




file="/tmp/ppa-url-tmp"
while IFS= read -r line; do
        # display $line or do somthing with $line
title=$(curl https://launchpad.net/~$line | grep -e '<title>'  | awk -F '<title>' '{print $2}' | awk -F '</title>' '{print $1}' | sed 's/^/"/' | sed 's/$/"/')


description=$(curl https://launchpad.net/~$line | grep -e 'content='  | awk -F 'content="' '{print $2}' | awk -F '.' '{print $1}' | tr -d '/>' | tr -d '"' | sed -e :a -e N -e 's/\n/ /' -e ta | sed 's/^/"/' | sed 's/$/"/') 


support=$(curl https://launchpad.net/~$line | grep -e '<option value='  | grep [0-9] | awk -F '(' '{print $2}' | awk -F ')' '{print $1}' | sed -e :a -e N -e 's/\n/ /' -e ta | awk '{print $1, $2, $3}' | sed 's/^/"/' | sed 's/$/"/' )


echo $title $description $support $line | uniq -u | tee -a /tmp/ppa

zenity --list --radiolist --title="Package installation." --text="Select package to be installed" --width=800 --height=500 --column=In --column=Name --column=Description --column=Compatible --column=PPA "in" "$title" "$description" "$support" "$line"

if [[ "$?" != 0 ]]; then
exit
else

CHECK_INST=$(echo $CHECK | awk -F'|' '{print $5}')

sudo apt-add-repository $CHECK_INST
sudo apt-get -y update
sudo apt-get install $code
fi

done <"$file"

My other attempt here (same results)

From terminal a example

myscript pipelight

enter image description here

2

From your description and code it sounds like you're looking for something like this:

    ss #1

To get a dialog that's constructed like this your original script needed to be augmented in a few key ways. For starters you need to move the call to construct the zenity dialog so that it's outside your while loop that's collecting all the data about the various PPAs.

The other thing lacking was a data structure to store all the bits about the PPAs that you were collecting. For this a Bash array makes the perfect "container" for this data.

lines=("${lines[@]}" "FALSE" "$title" "$description" "$support" "$line")

This will continue to append the results that you collect via the while loop to an array called lines. This array is then given to zenity, it contains the column data.

Complete script

Here's the whole thing put together, which produced the above screenshot of the dialog.

#!/bin/bash

file="ppa-url-tmp"

lines=()
while IFS= read -r line; do
        # display $line or do somthing with $line
title=$(curl https://launchpad.net/~$line | grep -e '<title>'  | awk -F '<title>' '{print $2}' | \
    awk -F '</title>' '{print $1}' | sed 's/^/"/' | sed 's/$/"/')


description=$(curl https://launchpad.net/~$line | grep -e 'content='  | awk -F 'content="' '{print $2}' | \
    awk -F '.' '{print $1}' | tr -d '/>' | tr -d '"' | sed -e :a -e N -e 's/\n/ /' -e ta | sed 's/^/"/' | sed 's/$/"/') 


support=$(curl https://launchpad.net/~$line | grep -e '<option value='  | grep [0-9] | awk -F '(' '{print $2}' | \
    awk -F ')' '{print $1}' | sed -e :a -e N -e 's/\n/ /' -e ta | awk '{print $1, $2, $3}' | sed 's/^/"/' | sed 's/$/"/' )

echo $title $description $support $line | uniq -u | tee -a /tmp/ppa
lines=("${lines[@]}" "FALSE" "$title" "$description" "$support" "$line")

done <"$file"

zenity --list --radiolist --title="Package installation." --text="Select package to be installed" \
    --width=800 --height=500 --column=In --column=Name --column=Description --column=Compatible --column=PPA "${lines[@]}"

if [[ "$?" != 0 ]]; then
  exit
else

  CHECK_INST=$(echo $CHECK | awk -F'|' '{print $5}')

  sudo apt-add-repository $CHECK_INST
  sudo apt-get -y update
  sudo apt-get install $code
fi

Additional debugging tip

When you're unclear what something is doing in a Bash script reach for the commands set -x and set +x. These will enable and disable Bash's verbosity which can really help shed light on what's going on. Here's my zenity command with just verbosity turned up for it:

set -x
zenity --list --radiolist --title="Package installation." --text="Select package to be installed" \
    --width=800 --height=500 --column=In --column=Name --column=Description --column=Compatible --column=PPA "${lines[@]}"
set +x

And here's what it looks like when I run ./myscript ...:

+ zenity --list --radiolist '--title=Package installation.' '--text=Select package to be installed' --width=800 --height=500 --column=In --column=Name --column=Description --column=Compatible --column=PPA FALSE '"pipelight-daily : Michael Müller"' '"This PPA provides daily builds of the Pipelight project pipelight-daily "' '"14.04 13.10 13.04"' mqchael/+archive/pipelight-daily FALSE '"Pipelight : Michael Müller"' '"Pipelight allows one to run Silverlight inside a Linux browser using Wine Pipelight "' '"14.04 13.10 13.04"' mqchael/+archive/pipelight FALSE '"pipelight-experimental : “Pipelight Dev Team” team"' '"Experimental packages for Pipelight  pipelight-experimental "' '"14.04 13.10 13.04"' pipelight/+archive/experimental FALSE '"pipelight-daily : “Pipelight Dev Team” team"' '"pipelight-daily "' '"14.10 14.04 13.10"' pipelight/+archive/daily FALSE '"pipelight-stable : “Pipelight Dev Team” team"' '"pipelight-stable "' '"14.10 14.04 13.10"' pipelight/+archive/stable FALSE '"libva : “Pipelight Dev Team” team"' '"libva "' '"13.10 12.10 12.04"' pipelight/+archive/libva
+ set +x

The above reveals how all the column components are expanded, properly quoted, by the use of "${lines[@]}".

  • Thanks my friend! it worked, thanks for explain. This script going to help to much people. – davidva Jun 1 '14 at 8:27
  • 1
    @davidva - glad to have been of help and thanks for the interesting Q. Good luck! – slm Jun 1 '14 at 12:19
  • I attempted in Xubuntu doesn't read 'lines=("${lines[@]}" "FALSE" "$title" "$description" "$support" "$line")" . This is the output: 'Syntax error: "(" unexpected (expecting "done").' I do not understand. But in Fedora, Arch Linux works :/ – davidva Jun 2 '14 at 4:56
  • Solved! only change the first line #!/bin/sh to #!/bin/bash – davidva Jun 2 '14 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.