PDA

View Full Version : Check if a package exist in the current repos



ryanrio95
June 12th, 2012, 09:57 AM
Okay, i have a text file with packages in each line.
Now i know how to go through each line and get the lines package.


cat packages.txt | while read line; do
echo $line >> packages2.txt
done

But i want to have a if script if the package exists in the current repos iam using.

Example
if package exists; then
echo $line >> packages2.txt
fi

Thanks in Advance.

roelforg
June 12th, 2012, 10:21 AM
apt-cache policy <package>
and see if it return anything?

ryanrio95
June 14th, 2012, 11:42 PM
does that also work with an if command?

regards,

cortman
June 15th, 2012, 01:11 AM
does that also work with an if command?

regards,

Set the ouput of the apt-cache policy <package> command as a string variable and use an if statement to see if the variable has non-zero length.

Vaphell
June 15th, 2012, 01:19 AM
general tip:
cat is unnecessary here, while loop can read files just fine. Also you can grab the output of the whole loop at once and not bother with appending individual lines


while read line
do
if [ ... ]
then
echo ...
fi
done < packages.txt > packages2.txt

ryanrio95
June 15th, 2012, 08:28 PM
thanks for the help guys.

i will try it now

regards,

Edit: How can i use apt-cache policy in a if command.
I know that there is a output.
Should i use a if command to check if the output has more characters than for example 100?
And , how can i do this then?

cortman
June 15th, 2012, 09:37 PM
Like I said earlier, set a variable-


pack=(apt-cache policy zaphod)
if [ -n $pack ] ;
then keep_doing_stuff ;
else
echo "package does not exist in repository"
fi

This is not necessarily syntactically correct but should give you the idea.