ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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
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.
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 02:50 PM.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.