LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 08-12-2006, 03:35 PM   #1
mwade
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.10
Posts: 43

Rep: Reputation: 15
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.
 
Old 08-12-2006, 05:51 PM   #2
jp-lack
Member
 
Registered: Mar 2005
Location: NJ - US
Distribution: Slackware
Posts: 93

Rep: Reputation: 15
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

Last edited by jp-lack; 08-12-2006 at 05:53 PM.
 
Old 08-13-2006, 11:37 AM   #3
mwade
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.10
Posts: 43

Original Poster
Rep: Reputation: 15
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.

Last edited by mwade; 08-14-2006 at 02:00 PM.
 
Old 08-15-2006, 11:12 AM   #4
mwade
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.10
Posts: 43

Original Poster
Rep: Reputation: 15
Any ideas anyone?
*bump*
 
Old 08-15-2006, 11:58 PM   #5
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86; Gentoo PPC; Gentoo Sparc64; FreeBSD; OS X; Solaris
Posts: 3,731
Blog Entries: 4

Rep: Reputation: 68
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.
 
Old 08-16-2006, 12:00 PM   #6
mwade
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.10
Posts: 43

Original Poster
Rep: Reputation: 15
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
 
Old 08-16-2006, 12:52 PM   #7
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86; Gentoo PPC; Gentoo Sparc64; FreeBSD; OS X; Solaris
Posts: 3,731
Blog Entries: 4

Rep: Reputation: 68
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
 
Old 08-17-2006, 04:52 PM   #8
mwade
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.10
Posts: 43

Original Poster
Rep: Reputation: 15
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.
 
Old 08-17-2006, 05:06 PM   #9
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
There's nothing wrong with spawn and SUSE, you just got something wrong there.
 
Old 08-17-2006, 05:23 PM   #10
mwade
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.10
Posts: 43

Original Poster
Rep: Reputation: 15
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. Any ideas what I'm doing wrong? Thanks.
 
Old 08-17-2006, 06:14 PM   #11
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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.
 
Old 08-18-2006, 09:49 AM   #12
mwade
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.10
Posts: 43

Original Poster
Rep: Reputation: 15
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.

Last edited by mwade; 08-18-2006 at 10:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 05:54 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
return value from shell script to c code? khucinx Programming 1 05-13-2004 03:43 PM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 07:51 PM
return key on shell script chupacabra Programming 2 10-22-2002 12:11 PM


All times are GMT -5. The time now is 12:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration