Script help - test user input yes/no [Solved :-) ]

Post here if you are a new Porteus member and you're looking for some help.
User avatar
Ed_P
Contributor
Contributor
Posts: 3053
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

Script help - test user input yes/no [Solved :-) ]

Post#1 by Ed_P » 07 Dec 2013, 00:13

I'm attempting to tailor a script @Fathom gave me a while back.

Code: Select all

#!/bin/sh
echo -en "Did you do the SU command?\nPress Enter if you did\n"
read
cp -a /home/guest/cleanup /mnt/live/
My question is how do I test for a negative answer and then exit the script?
Last edited by Ed_P on 22 Dec 2013, 01:15, edited 2 times in total.
Ed

User avatar
Slaxmax
Contributor
Contributor
Posts: 403
Joined: 03 Jan 2013, 09:51
Distribution: KDE4
Location: Campinas Brazil https://goo.gl/yrxwKi

Re: Script help

Post#2 by Slaxmax » 07 Dec 2013, 03:31

Example

Code: Select all

#!/bin/sh
echo "blablablabla ?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) YOUR CODE HERE; break;;
        No ) exit;;
    esac
done 
“DNA is like a computer program but far, far more advanced than any software ever created.”
― Bill Gates, The Road Ahead

User avatar
Ed_P
Contributor
Contributor
Posts: 3053
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

Re: Script help

Post#3 by Ed_P » 07 Dec 2013, 04:55

Thanks @Slaxmax. Do I not need a Read after the Echo?


-update-

Wow!! :Yahoo!: No, I do not need the Read. Cool!! Thanks @Slaxmax.
Ed

User avatar
Ed_P
Contributor
Contributor
Posts: 3053
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

Re: Script help

Post#4 by Ed_P » 07 Dec 2013, 17:37

Ok, lost again.

Code: Select all

#!/bin/sh
echo -en "Did you do the SU command?\n"

select yn in "Yes" "No"; do
    case $yn in
        Yes ) cp -a /home/guest/cleanup /mnt/live/;
              break;;
        No ) break;;
    esac

sh /home/guest/touchpad.sh
synclient
echo -en "Did synclient run?\n"
read

exit
done
If the answer is No I simply want to exit the case command not the script.
Ed

User avatar
Slaxmax
Contributor
Contributor
Posts: 403
Joined: 03 Jan 2013, 09:51
Distribution: KDE4
Location: Campinas Brazil https://goo.gl/yrxwKi

Re: Script help

Post#5 by Slaxmax » 07 Dec 2013, 19:21

EdP wrote:Ok, lost again.

Code: Select all

#!/bin/sh
echo -en "Did you do the SU command?\n"

select yn in "Yes" "No"; do
    case $yn in
        Yes ) cp -a /home/guest/cleanup /mnt/live/;
              break;;
        No ) break;;
    esac

sh /home/guest/touchpad.sh
synclient
echo -en "Did synclient run?\n"
read

exit
done
If the answer is No I simply want to exit the case command not the script.
delete the second break
“DNA is like a computer program but far, far more advanced than any software ever created.”
― Bill Gates, The Road Ahead

User avatar
Ed_P
Contributor
Contributor
Posts: 3053
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

Re: Script help

Post#6 by Ed_P » 07 Dec 2013, 19:45

Slaxmax wrote: delete the second break
Yup, I figured it out also. Then I replaced it with exist again, that's really what I want to script to do, not either / or but both in root mode and nothing if in guest mode.

Thanks for helping me @Slaxmax.
Ed

tome
Contributor
Contributor
Posts: 561
Joined: 26 Jun 2013, 14:03
Distribution: x64 Openbox
Location: Poland
Contact:

Re: Script help

Post#7 by tome » 07 Dec 2013, 22:35

this can be done automatically at start (etc/rc.d/rc.local) or probably by cliexec cheatcode

User avatar
brokenman
Site Admin
Site Admin
Posts: 5436
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

Re: Script help

Post#8 by brokenman » 08 Dec 2013, 14:21

If i understand you are tired of putting in commands only to find that you did not elevate your privileges to root before doing so. This means you have to then elevate to root and retype the command? Is this the real problem?
If it is just for one script then this will suffice.

Code: Select all

if [ $(whoami) != "root" ]; then
    echo "Did you SU?"
    read sued
    if [ "$sued" != "y" ]; then exit; fi
fi
This will only ask you the question are running as guest. If the user does not press the 'y' key it will exit. I don't see the need for a case here.
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Ed_P
Contributor
Contributor
Posts: 3053
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

Re: Script help

Post#9 by Ed_P » 09 Dec 2013, 00:43

tome wrote:this can be done automatically at start (etc/rc.d/rc.local) or probably by cliexec cheatcode
Part of it yes, but not the part that requires root privileges. Or can root mode be achieved via a script command?
brokenman wrote:If i understand you are tired of putting in commands only to find that you did not elevate your privileges to root before doing so. This means you have to then elevate to root and retype the command? Is this the real problem?
With the current script unless I scroll back I do not see the error msg the cp command fails with when run as guest. If I can become confident the synclient changes are executing I won't need to run the synclient command to list everything and will see the cp error if run as guest. Other options would be to test if the cp command issued an error msg, the script programmatically switching to root mode, being able to display specific synclient settings rather than all of them, etc.
brokenman wrote:If it is just for one script then this will suffice.

Code: Select all

if [ $(whoami) != "root" ]; then
    echo "Did you SU?"
    read sued
    if [ "$sued" != "y" ]; then exit; fi
fi
This will only ask you the question are running as guest. If the user does not press the 'y' key it will exit. I don't see the need for a case here.
Thanks @brokenman.
Ed

tome
Contributor
Contributor
Posts: 561
Joined: 26 Jun 2013, 14:03
Distribution: x64 Openbox
Location: Poland
Contact:

Re: Script help

Post#10 by tome » 09 Dec 2013, 17:02

Part of it yes, but not the part that requires root privileges. Or can root mode be achieved via a script command?
rc.local and cliexec are executed as root

I need also help

Code: Select all

echo "    Mounting iso or xzm from a url without downloading       "
echo "        Paste your url and press Enter      "

hpoint=/mnt/httpfs2
xls=`ls $hpoint`

read bup
httpfs2 "$bup" $hpoint

#to do
#How to bind /mnt/httpfs2, "/" and module.xzm (ls $hpoint) to /mnt/httpfs2/module.xzm? (or another, the easiest way to do this)

#yls=`$hpoint/$xls`

#mkdir /mnt/$xls
#mount -o loop $yls /mnt/$xls

User avatar
Ed_P
Contributor
Contributor
Posts: 3053
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

Re: Script help

Post#11 by Ed_P » 09 Dec 2013, 18:07

tome wrote:
Part of it yes, but not the part that requires root privileges. Or can root mode be achieved via a script command?
rc.local and cliexec are executed as root
!!! Wow!!

In looking at the cheatcode it shows cliexec=my_script, how does it know where "my_script" is? Would I use cliexec=/home/guest/test.sh or cliexec=/dev/sdb2/home/guest/test.sh ? Or does the script have to be in the rc.local folder to be executed by cliexec?
Ed

tome
Contributor
Contributor
Posts: 561
Joined: 26 Jun 2013, 14:03
Distribution: x64 Openbox
Location: Poland
Contact:

Re: Script help

Post#12 by tome » 09 Dec 2013, 18:35

it doesn't matter for cliexec=. You can edit and add this to rc.local:
cp -a /home/guest/cleanup /mnt/live/

User avatar
Ed_P
Contributor
Contributor
Posts: 3053
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

Re: Script help

Post#13 by Ed_P » 09 Dec 2013, 18:46

:( I can't find rc.local. Even when logged in as root!!
Ed

tome
Contributor
Contributor
Posts: 561
Joined: 26 Jun 2013, 14:03
Distribution: x64 Openbox
Location: Poland
Contact:

Re: Script help

Post#14 by tome » 09 Dec 2013, 19:16

etc/rc.d/rc.local

User avatar
Ed_P
Contributor
Contributor
Posts: 3053
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

Re: Script help

Post#15 by Ed_P » 16 Dec 2013, 04:41

Thank you @Tome. I've been busy and haven't got back to this but I will.



-update-

I found the file, made the change but couldn't save it. :x Signed off as guest and back on as root, found the file, made the change, saved it :Yahoo!: , rebooted and voila!! It worked.

Thank you @Tome. :beer:
Ed

Post Reply