LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Return value to shell in script (http://www.linuxquestions.org/questions/linux-newbie-8/return-value-to-shell-in-script-473319/)

mwade 08-12-2006 03:35 PM

Return value to shell in script
 
Hi all, my problem today is that i am trying to do a smart upgrade which SOMETIMES requires an input of y or n to upgrade the packages. I want this inside a script but am unsure on how to return the value when it calls for it. Obviously it will require an if statement to say if it requires a command give it the value. Someone mentioned the expect statement so I looked into that and I think it will look something like

expect "y/n"
send "y"

But the problem is if you do a smart upgrade it will ask for yes or no while its performing the upgrade so putting

smart upgrade
expect "y/n"
send "y"

isnt really going to work because it will execute the expect command after the smart upgrade has finished. Does anyone know how to get round this?
Thanks in advance.

jp-lack 08-12-2006 05:51 PM

is the y/n entered by the user? if so

try

Code:

echo -ne "Do you want to upgrade? (y/n):"
read ans;
if [ "x$ans" == "x" ] || [ "x$ans" == "xn" ]; then
                        return 255;
fi

it will exit if you do NOT want to upgrade

mwade 08-13-2006 11:37 AM

When you "smart upgrade" if there any new packages it says "Confirm changes? (Y/n):" and expects a "y" or "n" to confirm changes. As I am trying to upgrade automatically every hour I want my script to reply "y" when it asks it to confirm changes. What I am trying to do is find a method that would do this sort of thing

If smart upgrade = "Confirm changes? (Y/n):" then return "y/r"

Thanks.

mwade 08-15-2006 11:12 AM

Any ideas anyone?
*bump*

bulliver 08-15-2006 11:58 PM

Quote:

Any ideas anyone?
Sure, I have one. Scripting automatic software updates running as root that can change your system with no human intervention is a terrible idea, and a great way to completely bork your system without knowing why...

Is it really so difficult to check for updates once a day manually? Isn't 1/hour a bit excessive?

If not it is your system and you can do what you wish but I for one will not help you...

If I am misunderstanding what a "Smart upgrade" is do please tell.

mwade 08-16-2006 12:00 PM

Smart just downloads the latest updates, fixes dependencies and generally makes it a hell of a lot easier to install things. I suppose once an hour is a bit excessive but I want to make sure my system is up to date and I can't guarentee what time I'll be on linux. I'm sure there is a way of updating when you boot up, that will do. It isn't difficult to check manually every day for updates but it is a good training excercise to be able to do it. Thanks anyway, I'll wait for my books :) :study:

bulliver 08-16-2006 12:52 PM

Quote:

It isn't difficult to check manually every day for updates but it is a good training excercise to be able to do it.
Ok, fair enough. But do please consider why it might be dangerous to blindly update software on your system.

I will say that 'expect' does seem to be exactly what you need. Have you read the documentation yet? From what you have written it seems you think of expect as a 'command' that you can use, but rather it is like a scripting language used to automate tasks that need user-intervention (like the spot you find yourself in).

Without knowing the specific of your smart update program I can't help much but perhaps this page will get you started with expect:
http://www.csc.calpoly.edu/~dbutler/.../tutorial.html

mwade 08-17-2006 04:52 PM

With that tutorial I nearly had it, the problem being suse doesn't accept the spawn command. So when I execute my script...

Code:

#!./expect -f

smart upgrade
expect "Confirm changes? (Y/n):"
send "y\r"

It will execute the smart upgrade and only after it has finished it will run the expect statement. I have only put it this way because spawn doesnt work. Does anyone know another way to start a program and run the expect and send command half way through it? Thanks.

spirit receiver 08-17-2006 05:06 PM

There's nothing wrong with spawn and SUSE, you just got something wrong there.

mwade 08-17-2006 05:23 PM

mwade:~ # spawn
-bash: spawn: command not found

Code:

#!./expect -f

spawn smart upgrade
expect "Confirm changes? (Y/n):"
send "y\r"
interact

This does not work either. I read somewhere that spawn does not work on SuSE so I thought it didnt. :scratch: Any ideas what I'm doing wrong? Thanks.

spirit receiver 08-17-2006 06:14 PM

Try and invoke expect with the -d switch, it will tell you what's going on. I believe that the expect command will time out before smart asks for a confirmation, so inserting "set timeout -1" into your script should help.

mwade 08-18-2006 09:49 AM

Aha! I've found a way of doing it.

Code:

yes '' | smart upgrade
This is quite dangerous really as it answers yes to anything smart upgrade asks so it's not really advisable but oh well, I'll just have to make a backup script too just in case. One problem with this is it seems to lock smart upgrade in read only mode. As I'll be changing the script to work on system boot this isn't a big problem thought. The other problem is I don't know how to save the output of this to a seperate file. I've tried

Code:

yes '' | smart upgrade > /usr/local/logs/outputfile.txt
but the console seems to pause instead of running the commands. Does anyone know how to get around this? Thanks.


All times are GMT -5. The time now is 10:38 PM.