WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Y/N script  (Read 4230 times)

Offline rexi

  • Newbie
  • *
  • Posts: 38
Y/N script
« on: November 30, 2023, 12:25:37 PM »
Hello, it's me again.
I want to make a .sh file, which when you open it will appear: ''do you want to shut down the computer Y/N'' and if you put Y, exitcheck.sh would run and shut down the computer and if you put N, the script would closed down.

I found a "template" of this program (because I use linux for a short time and don't know all the commands), but it threw an error. Can anyone help me?

Code: [Select]
read a
if [[ $a == "N" || $a == "n" ]]; then
        echo "not gonna do it"
else
        echo "gonna do it"
fi

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: Y/N script
« Reply #1 on: November 30, 2023, 02:06:37 PM »
Hi rexi
Try it like this:
Code: [Select]
#!/bin/sh

read a
if [[ "$a" == "N" || "$a" == "n" ]]
then
echo "not gonna do it"
else
if [[ "$a" == "Y" || "$a" == "y" ]]
then
echo "gonna do it"
else
echo "Enter y or n"
fi
fi

The first line needs to be  #!/bin/sh

You also need to make your file executable:
Code: [Select]
chmod 775 YesNo.sh
You should also handle the case where someone enters
something other than  y  or  n , which this example does.

Offline rexi

  • Newbie
  • *
  • Posts: 38
Re: Y/N script
« Reply #2 on: December 01, 2023, 09:40:43 AM »
Thanks for the sample script.
I ran the command: ''chmod 775 /mnt/sdb1/run2.sh''
and then I run: ''sh /mnt/sdb1/run2.sh'', but it wrote
Code: [Select]
: not foundun2.sh: line 2:
' : bad variable name¨
/mnt/sdb1/run2.sh: line 14: syntax error: unexpect ''fi'' (expecting ''then'')
and why does it have to be at the beginning :
Code: [Select]
#!/bin/sh

Offline gadget42

  • Hero Member
  • *****
  • Posts: 818
Re: Y/N script
« Reply #3 on: December 01, 2023, 10:20:39 AM »
The fluctuation theorem has long been known for a sudden switch of the Hamiltonian of a classical system Z54 . For a quantum system with a Hamiltonian changing from... https://forum.tinycorelinux.net/index.php/topic,25972.msg166580.html#msg166580

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: Y/N script
« Reply #4 on: December 01, 2023, 11:52:15 AM »
Hi rexi
... and then I run: ''sh /mnt/sdb1/run2.sh'', ...
Run it like this:
Code: [Select]
./mnt/sdb1/run2.sh
Quote
... but it wrote
Code: [Select]
: not foundun2.sh: line 2:
' : bad variable name¨
/mnt/sdb1/run2.sh: line 14: syntax error: unexpect ''fi'' (expecting ''then'')
...
It sounds like either:
1. You introduced a typo entering it manually instead of copy/paste.
or:
2. You altered the original example.

Quote
...and why does it have to be at the beginning :
Code: [Select]
#!/bin/sh
Because it defines which interpreter to use when running the script.

Offline rexi

  • Newbie
  • *
  • Posts: 38
Re: Y/N script
« Reply #5 on: December 01, 2023, 01:44:17 PM »
Quote
It sounds like either:
1. You introduced a typo entering it manually instead of copy/paste.
or:
2. You altered the original example.

The problem was that I copied and pasted the original code into a .txt file and renamed it to .sh (all on windows 10). So it added an ''┐'' to the end of each line

I also tried running the script with ''.'' instead of sh, but every time it said: sh: ./mnt/sdb1/run2.sh: not found. But ''sh'' work fine.
THX

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: Y/N script
« Reply #6 on: December 01, 2023, 05:10:01 PM »
Hi rexi
Windows marks the end of a line with a carriage return/linefeed sequence.
Linux use a linefeed to mark the end of a line.
If you insist on editing with Windows, make the file Linux compliant like this:
Code: [Select]
dos2unix /mnt/sdb1/run2.sh

Offline rexi

  • Newbie
  • *
  • Posts: 38
Re: Y/N script
« Reply #7 on: December 02, 2023, 01:31:17 PM »
I improved my script:
Code: [Select]
#!/bin/sh

echo "Do you want to turn off your computer? (Y/N)"
while :
do
: loop
read a
if [[ "$a" == "Y" || "$a" == "y" ]]
then
exitcheck.sh
else
if [[ "$a" == "N" || "$a" == "n" ]]
then
echo "exit"
else
echo "Do you want to turn off your computer?"
echo "Enter Y or N"
fi
fi
done
I know it's not exactly ideal and that it could be done better, but I'm so happy with it and it works for my purposes without any problems. But I ran into a problem, instead of echo "exit" I want to make the script turn off (close/terminate).

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: Y/N script
« Reply #8 on: December 02, 2023, 01:51:35 PM »
Hi rexi
Then change this:
Code: [Select]
echo "exit"To this:
Code: [Select]
exit
The script will terminate right there.

I improved my script:
Code: [Select]
#!/bin/sh
 ----- Snip -----
while :
do
: loop

 ----- Snip -----

What is the purpose of the  colons  and the  loop  statement?
Does it not work if you do this way:
Code: [Select]
#!/bin/sh
 ----- Snip -----
while true
do

 ----- Snip -----

Offline rexi

  • Newbie
  • *
  • Posts: 38
Re: Y/N script
« Reply #9 on: December 02, 2023, 02:55:54 PM »
The colon at the loop command is just a remnant of the old command where I was checking if there was something like GOTO, thanks for reminding me.

I used exit instead of echo ''exit'', then I tested the script and it still doesn't do what I needed. I need to replace exit with something that completely shuts down the terminal, not just jumps out of the script.
« Last Edit: December 02, 2023, 03:01:00 PM by rexi »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: Y/N script
« Reply #10 on: December 02, 2023, 03:33:03 PM »
Hi rexi
Depending on which terminal you have installed, try:
Code: [Select]
aterm-xterm -e /Path/To/YN.shor:
Code: [Select]
xterm -e /Path/To/YN.sh

Offline rexi

  • Newbie
  • *
  • Posts: 38
Re: Y/N script
« Reply #11 on: December 03, 2023, 02:26:28 PM »
I tried aterm-xterm -e /Path/To/YN.sh, but it just opened another script window.
In the end, I decided to simplify the script.
Thank you for your help
Code: [Select]
#!/bin/sh

while :
do
echo "press ENTER to turn off the computer"
read a
if [[ "$a" == "" || "$a" == "" ]]
then
exitcheck.sh

fi
done

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: Y/N script
« Reply #12 on: December 03, 2023, 05:12:08 PM »
Hi rexi
I tried aterm-xterm -e /Path/To/YN.sh, but it just opened another script window. ...
Right, and if you answer  n  that window closes. If you answer  y  it runs  exitcheck.sh
and then closes that window. If you change  exitcheck.sh  to  exitcheck.sh &  it will
launch  exitcheck.sh  in the background and immediately close that window.

Offline rexi

  • Newbie
  • *
  • Posts: 38
Re: Y/N script
« Reply #13 on: December 09, 2023, 08:17:22 AM »
OK, so now I have the script, but I want to make it a TCZ extension. I understand everything (md5sum, unpacking and repacking TCZ...), but I still didn't understand how to edit the file in the "tce.installed" folder

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: Y/N script
« Reply #14 on: December 09, 2023, 11:07:17 AM »
Hi rexi
The file in  tce.installed  is a script that runs after
the extension is installed. If you don't need to run
any commands after your extension gets installed
then you don't need to create it.
If you do create it, it must have the same name as
the extension (without  .tcz ).

If you look at the  tce.installed  directory it contains
the names of all of the extensions that are currently
installed. The ones that that have a size of 0 bytes
were created by  tce-load.  The ones that have a
size greater than zero are scripts that were included
with extensions. You can look at them to see what
kind of commands other extensions need to have run
after they've been loaded.

This will tell you which of your extensions are currently installed:
Code: [Select]
tce-status -i
This will tell you which of your extensions are not currently installed:
Code: [Select]
tce-status -u