Bash script to update Debian type systems, a couple of questions
Hi everyone
I have written a script to update Debian type systems and it works until the end where it just stops. I also have it installing Debdelta so it can use debdelta to minimise download times. My questions are, can I get the script to work out if it needs to install Debdelta and then continue on if it doesn't? Also can anyone see why it just stops at the end without actually upgrading? Code:
#!/bin/bash |
Hi,
you can use something like 'dpkg -l <package>' to find out if a given package is installed. If you indent the script properly I think you'll see you've got an extra fi. Cheers, Evo2. |
Opps, sorry, not 'dpkg -i', 'dpkg -s'.
Eg. Code:
dpkg -s debdelta > /dev/null 2>&1 Evo2. |
Hi Evo
Thanks for your reply. I see how that would work on its own, yes I tried it after reading the man page (should have read that before lol) but I can't see how to add it to the script. Should I use grep or something similar? |
Hi,
well, first test if it is installed then if it is not you can ask the user if they want to install it, or otherwise exit. Something like: Code:
dpkg -s debdelta > /dev/null 2>&1 Evo2. |
Quote:
You can also make it a little simpler (as your using bash): Code:
if ! dpkg -s debdelta > /dev/null 2>&1 |
Quote:
|
Quote:
Quote:
|
Quote:
The later version. Code:
if ! dpkg -s debdelta > /dev/null 2>&1 |
Ok the adddition is now working, sort of, in the script. One thing is wrong, everytime I run it it still wants to install debdelta which is already installed.
|
I think I know what's going wrong with the debdelta installation, I haven't given any options in the script so it is still going to install debdelta because the script says to. There isn't any conditional to say hey if it isn't installed install it but if it is installed move to the next section.
I'll have to do some more reading, I don't know at the moment how to use the /dev/null to say move to the next section cause you already have debdelta or if I need to modify the next section. EDIT: Also what's the proper format for indentation? I have been using the tab key. |
Indentation is a personal thing and tab is fine if you like the look of it :)
As for how to utilise the code simply place install part inside if. |
Don't worry too much about /dev/null for your script. It is only a "bin" device that acts like a black hole. If you run a command but don't want the output clogging up your screen, you send the output to /dev/null. This does not change the result of the command, just the output. The example also adds 2>$1 which says to also send stderr to /dev/null.
|
Thanks guys, I have the debdelta part working perfectly now but the upgrade still isn't working as it should. There is a error at the end but it flashes by and then the terminal closes. Is there a way to create a readable log file from what is on the terminal screen?
My current file looks like this Code:
#!/bin/bash |
Well, evo pointed out in his first post that you have to look at how many times you open / close your if statements.
Also I cannot understand how you new if statement works? Code:
if ! dpkg-query -l debdelta = i > /dev/null 2>&1 Code:
dpkg-query -l debdelta = i |
I put it in the terminal and it said no output matching. Should have tested that better than I did. Stupid thing is the script works through that ok for some reason. I'll take it out and see what happens.
|
It will always work because you have said when this command fails, which it will every time, to then run what is inside the if statement.
Also, your system must be quite different to mine, here is what I get: Code:
$ dpkg-query -l debdelta = i |
Sorry if I'm slow in replying there's a huge storm outside and it is knocking my net access about a bit.
Here is the exact out from that query Code:
root@michael-laptop:/home/michael# dpkg-query -l debdelta = i Quote:
EDIT: This is the query without the -i Code:
root@michael-laptop:/home/michael# dpkg-query -l debdelta |
grail is right. You don't need the "= i" in the statement at all.
if ! dpkg-query -l debdelta > /dev/null 2>&1 will give you a true/false return code (0 or 1). If the return code is 1 (false) then the script will go into the "then" block (this is because of the ! after the if). What is probably happening in your script is that you are getting an error due to the "= i" and therefore a return code of non-zero. This will always execute the "then" block. As for your question about seeing the output, you can execute your script using "script > log.txt" |
Try using -s instead of -l
|
I understand the mistake I made.
Quote:
Code:
root@michael-laptop:/home/michael/Desktop# ./update_upgrade > log.txt |
Quote:
Thank you all again, I appreciate your assistance. |
Quote:
Run with "./update_upgrade > log.txt 2>&1" so that you get both sent to your log file. As for the error, its probably still a mismatch between the if statements and their corresponding fi's . |
Hi,
ok, I couldn't help myself, here is a version of the script (with very verbose comments). Note that I _did_not_ screw around with the sudo setup since I think doing such a thing is prone to causing errors. Code:
#!/bin/bash very simple) therefore it may contain undocumented features (AKA bugs). Cheers, Evo2. |
Hi Evo, thanks for this I appreciate your input. I'll take the script for a test drive and see how it goes.
The script I wrote has worked but only when I had it output to the log and when very little if anything showed in the terminal. This is something I don't understand but am looking at today. I also believe that logging the process to file would be a good feature so people can see how much download they are saving using debdelta, how would I achieve that with your script? It needs to be a part of the script itself not part of the command to run it. Thanks again for your help and patience. I have only tried scripting once before (I have a small 1 line script that removes duplicate entries from a host file) a few years ago so it's a huge learning curve for me. |
All times are GMT -5. The time now is 01:57 PM. |