LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-28-2009, 10:18 PM   #1
jfain
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Rep: Reputation: 0
wget pipe doesn't execute sh script properly.


Hello all.

I am trying to make a sh script that backes up a file and edits it when run the first time. The second time it is ran it checks if it exists and if it does teh script asks if you want to restore the original.

I have successfully created this script. I want to execute it via this command.

Code:
wget -qO- http://somesite.com/dir/script.sh | bin/sh
But when I do I get a error.

Code:
sh: y: unknown operand
and is because of this code:

Code:
	read answer
		if [ $answer = y ]; 
		then
I have googled this like crazy but could not find anything. I have also tried the -eq and == operators. But I am a newb at this scripting language. I have tried, != n, = 'y' = "y" and many other combos. all returnning the same error.

If I run the script with out that part with the command above, the script runs flawlessly.

Heres the script in its entirety.

Code:
#!/bin/sh
# Add Roam Only Mode to Stock Pre v1.1 
# Originaly created by w5mw http://www.webos-internals.org/wiki/Roam_Control
# updated by fain for WebOS 1.1 and added error checking.

	/bin/echo
#check to see if the back up file exists. If it does do not proceed.
if [ -f /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js.1.1.bak ];
	then
	/bin/echo
	/bin/echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	/bin/echo "! Back up already exists... Have you ran the script already? If you have, be !"
	/bin/echo "! you have the original file in the directory of                             !"
	/bin/echo "! /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/            !"	
	/bin/echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	/bin/echo
	/bin/echo "Would you like to restore the preflist-assistant.js.1.1.bak"
	/bin/echo "to preflist-assistant.js now? [y or n]"
	read answer
		if [ $answer = y ]; 
		then
			/bin/echo
			/bin/echo "Making the file system Writable."
			/bin/echo
			/bin/mount -o remount,rw /
			/bin/echo "Restoring file from preflist-assistant.js.1.1.bak"
			mv /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js.1.1.bak /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js
			/bin/echo
			/bin/echo "Making the file system read-only."
			/bin/mount -o remount,ro /
			/bin/echo
			exit
		else
			/bin/echo
			/bin/echo "Nothing changed"
			/bin/echo
			exit
		fi
#If the file does not exist make a backup
else
	/bin/echo "Making the file system Writable." 
	/bin/mount -o remount,rw /
	/bin/cp /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js.1.1.bak
#verify the backup was created.
if [ -f /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js.1.1.bak ];
then
#insert patch lines
   	/bin/echo "File: preflist-assistant.js.1.1.bak now exists..Patching now"
	/bin/sed -i "1232 a\                else if(payload.extended.mode == 'roamonly') \{" /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js 
	/bin/sed -i '1233 a\                        this.voiceRoamingModel.currentVoiceRoaming = "roamonly";' /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js 
	/bin/sed -i "1234 a\                        \$('dataroamingrow').show();" /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js 
	/bin/sed -i "1235 a\                        \$('voiceRoamingRow').removeClassName('last');" /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js
	/bin/sed -i "1236 a\                \}" /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js
	/bin/sed -i '227 a\                        \{label : "Roam Only",value: "roamonly"\}' /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js
	/bin/sed -i '227 s/\}/\},/' /usr/palm/applications/com.palm.app.phoneprefs/app/controllers/preflist-assistant.js
	/bin/echo "Completed...now I am making the file system read-only."
#mount the filesystem read only
	/bin/mount -o remount,ro /
	/bin/echo
else
#if the back up does not exist make no changes.
	/bin/echo
   	/bin/echo "File: preflist-assistant.js.1.1.bak does not exist, no changes made."
	/bin/echo "Making the file system read-only."
	/bin/mount -o remount,ro /
	/bin/echo
fi
fi
When running this script as:
Code:
sh ./script.sh
It runs perfect.

Thanks,
Fain

Last edited by jfain; 07-28-2009 at 10:21 PM.
 
Old 07-29-2009, 12:50 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
The syntax looks ok to me, at least for that section of the script. It could be that something funky is happening with your input. Try putting an "echo $answer" line in there before the if statement to see what the script is seeing.

You might also try changing the if statement to a case statement. That's usually a better option for y/n input questions like this anyway, since you can also add a final wildcard option:
Code:
*) echo "invalid choice--try again" ;;
Finally, you can also run your script with "#!/bin/bash -x" to enable verbose output for debugging, or bookend a block of code with "set -x" and "set +x" to do the same thing on just that section of the file.
 
Old 07-29-2009, 01:06 PM   #3
jfain
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Ok thanks for your reply I will try this tonight after work and report back.

Also, when I run the script with the wget command it doesn't even stop read a user input. It just jumps to the error.
 
Old 07-29-2009, 01:49 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Ah, well that seems to be your problem then--the way you're executing it. I guess I should've noticed it before. You're downloading the script itself with wget and piping it directly into /bin/sh.

I'm not very familiar with this way to execute the code, but my guess is that when you pipe the script as text into /bin/sh, it runs as a subshell of the first process, so the stdin is the pipe itself, and the read command only gets input from the same pipe. Or something like that. I may be wrong about exactly what's happening.

In any case, you need to be able to launch the script as an independent command. The #!/bin/sh already tells the system what interpreter to use, so it just needs to be made executable before you run it. But frankly, I'm not sure about the best way to go about doing that.

Perhaps you could, for example, write a second "wrapper" script, one that simply runs wget to download the first script, chmods it, and runs it for you, then deletes the file at the end, if necessary.

Maybe someone more experienced in this area can chime in with a better suggestion.

Last edited by David the H.; 07-29-2009 at 01:50 PM.
 
Old 07-29-2009, 09:50 PM   #5
jfain
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Hello again,

Haha we think alike. I thought of the wrapper script already but the funny thing is, it does the same thing???

I just tried removing the !#/bin/sh at the beginning of the script, no go.

I tried converting the ifs to case statements. No errors that time but it skips and exits.

echo $answer
outputs nothing.

The problem is with the read command and the pipe.

Oh well guess I'll stick with the 3 step program for now. I just wanted a easy 1 step setup for users to install this patch. Thanks again for your help.

I got this from the output of wget.

-O Save to filename ('-' for stdout)

Could it have to do with stdin conflicting with stdout?

Last edited by jfain; 07-29-2009 at 10:14 PM.
 
Old 07-30-2009, 02:03 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Well, they both tell the system which interpreter to use, but it's the way you launch it and pass the script to it that's different. As I said, I'm not 100% sure what's happening; only that the piped way appears to interfere with accessing the readline.

In any case, it seems to me that if you're going to give this to users as an "all-in-one" solution, then having them run a simple wrapper script would be easier than typing in a more complex command.

What the wget message says is that the "-O" option takes a filename argument. If you want the output to go to stdout, use "-" as the filename. So I'm guessing you forgot to add the final dash to the command "wget -qO- <url>".

BTW, one thing you can do to make your script more robust is to give your variables a default value, as described here. It won't fix the problem for you, but it can get rid of the nasty error messages.

To make your case statement continually try again on invalid input, embed it in a "while true; do <case...> done" loop. Then use break or exit after the commands when you want them to exit the loop or the script.
 
Old 07-30-2009, 02:34 AM   #7
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,087

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The problem is not in the 'y'.

The problem is that $answer variable is <nothing> and Bash now reads:
Code:
if [ = y ]
and it cannot handle that.

If you were or are able to edit the script, you could try:
Code:
if [ "$answer" = "y" ]
which compares an empty string "" with "y".

All other problems like your script not stopping for input is caused by the way how you pipe the output of wget in a separate subshell.

All in all it is not a good idea anyway to pipe something coming from outside directly into bash. I hope you don't have to run this script as root.

jlinkels

Last edited by jlinkels; 07-30-2009 at 02:37 AM.
 
Old 07-30-2009, 08:16 AM   #8
jfain
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
The script is run as root, thanks for the advice. I will have them download the script and run it locally from now on.
 
  


Reply

Tags
conditional, pipe, script, sh, unknown, wget


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
How to execute binary executable using wget redss Linux - General 5 06-11-2009 11:37 AM
Will this sequence execute properly? archieval Programming 2 11-02-2007 05:57 AM
Use pipe with wget Basta Linux - Newbie 3 04-15-2005 05:42 AM


All times are GMT -5. The time now is 12:50 PM.

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