Tiny Core Linux

Tiny Core Extensions => TCE Q&A Forum => Topic started by: rexi on November 30, 2023, 12:25:37 PM

Title: Y/N script
Post by: rexi 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
Title: Re: Y/N script
Post by: Rich 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.
Title: Re: Y/N script
Post by: rexi 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
Title: Re: Y/N script
Post by: gadget42 on December 01, 2023, 10:20:39 AM
bash shebang

https://stackoverflow.com/questions/10376206/what-is-the-preferred-bash-shebang
Title: Re: Y/N script
Post by: Rich 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.
Title: Re: Y/N script
Post by: rexi 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
Title: Re: Y/N script
Post by: Rich 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
Title: Re: Y/N script
Post by: rexi 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).
Title: Re: Y/N script
Post by: Rich 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 -----
Title: Re: Y/N script
Post by: rexi 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.
Title: Re: Y/N script
Post by: Rich 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
Title: Re: Y/N script
Post by: rexi 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
Title: Re: Y/N script
Post by: Rich 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.
Title: Re: Y/N script
Post by: rexi 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
Title: Re: Y/N script
Post by: Rich 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
Title: Re: Y/N script
Post by: rexi on December 19, 2023, 10:10:48 AM
So after a few days of procrastination, I got back into development. I took my script and edited the .desktop file, then packaged everything, generated the md5sum and put it in the cde folder.
But after turning on the computer, the application does not appear anywhere and cannot be started in any way.
I keep mulling over the tce.installed folder, but I don't know if that's it and I don't know what to put in there either (I'm not that experienced with linux).

Here is the content of the .desktop file.
Code: [Select]
[Desktop Entry]
Name=Exit
Exec=cliorx sudo exit.sh
Icon=exittc
X-FullPathIcon=/usr/local/share/pixmaps/exittc.png
Type=Application
Categories=System;
Title: Re: Y/N script
Post by: Rich on December 19, 2023, 10:41:16 AM
Hi rexi
The difference between a  cde  and  tce  folder is  cde
is used for  read-only  media like CD and DVD. When
using writable media (disk, USB thumb drive, SD card, etc.)
you should alway use a  tce  folder.
Title: Re: Y/N script
Post by: rexi on December 19, 2023, 11:34:43 AM
1. Thank you for the useful information.
2. I would use the TCE folder, but I use tinycore on a USB flash drive (created with rufus (https://rufus.ie/en/ (https://rufus.ie/en/)) and I used the .iso from the official site) and the TCE folder is not there.
Title: Re: Y/N script
Post by: Rich on December 19, 2023, 03:12:33 PM
Hi rexi
Third party installers don't know how to handle Tinycore.

Boot your flash drive.
If you used the Coreplus ISO, tc-install-GUI.tcz should be present.
Otherwise, install tc-install-GUI.tcz.
Use tc-install-GUI.tcz to install Tinycore to another flash drive.
Title: Re: Y/N script
Post by: rexi on December 20, 2023, 10:47:45 AM
I've known about tc-install-GUI.tcz for a long time, but I've never seen a reason to use it (plus I find it a bit more complicated to install)
Is there any way to burn an .iso to a flash drive on TinyCore?
Title: Re: Y/N script
Post by: CentralWare on December 21, 2023, 02:26:48 AM
For most bootable ISO files, you can use "dd"

Code: [Select]
dd if=filename.iso of=/dev/usbstick bs=1M status=progresswhere "usbstick" is your USB device's designation.

WARNING: ALL information on /dev/usbstick will be wiped out without notice or hesitation - check your typing BEFORE pressing ENTER!

If you're using a Windows based machine to set things up with, download the program "Rufus" which can burn your ISO to USB with some sane logic involved.
Title: Re: Y/N script
Post by: rexi on December 21, 2023, 09:29:51 AM
OK, fine, but could we go back to my original question. What do I put in the tce.install folder to make the app work?
Title: Re: Y/N script
Post by: Rich on December 21, 2023, 10:44:09 AM
Hi rexi
... then packaged everything, generated the md5sum and put it in the cde folder.
But after turning on the computer, the application does not appear anywhere and cannot be started in any way. ...
I guess I missed the part that mentions how you are loading
your new extension.

If you want it to load automatically when you boot, add it to
your  onboot.lst  file.

Or, if you want to load it from the command line:
Code: [Select]
tce-load -i YourExtensionName.tcz
You might also want to take an hour to read this fine book:
http://tinycorelinux.net/book.html
Title: Re: Y/N script
Post by: CentralWare on December 21, 2023, 11:24:14 AM
@rexi: I had to read the entire thread in order to follow along and answer your last question; which I think I now know what you're after.

Copy/paste the following into a file called /opt/test.sh; run this script and it will create the files used in this example.
Code: [Select]
#!/bin/sh

## If our graphic isn't already downloaded, do so here ##
[ ! -f /usr/local/share/pixmaps/exit.png ] && wget -O /usr/local/share/pixmaps/exit.png https://pngimg.com/uploads/exit/exit_PNG35.png

## Create our "power button" icon on the desktop ##
cat > /usr/local/share/applications/exit.desktop << EOF
[Desktop Entry]
Name=exit
Exec=cliorx sudo sh /opt/yesno
Icon=exit
Terminal=false
X-FullPathIcon=/usr/local/share/pixmaps/exit.png
Type=Application
Categories=Utility;
EOF

## Create our Y/N script with shut-down ##
cat > /opt/yesno << EOF
#!/bin/sh

echo -n "Do you want to turn off your computer? (y/N) "
read yesno
case \$yesno in
   [Yy]*) sudo poweroff ;;
esac
EOF
sudo chmod +x /opt/yesno && chown tc.staff /opt/yesno

In the above example, your extension ("yesno.tcz") would consist of:
/usr/local/share/pixmaps/exit.png (our desktop graphic)
/usr/local/share/applications/exit.desktop (our desktop info file)
/opt/yesno (our "program")
/usr/local/tce.installed/yesno (without .sh in the filename) which is launched as the extension is brought online.  If you do not need to do anything right when the extension is brought online, you do not need to create it - an empty one will be created by tce-load

Once you have your yesno.tcz and its associated files (.tcz.dep, .tcz.info, etc.) placed in the /etc/sysconfig/tcedir/optional directory, add yesno.tcz to /etc/sysconfig/tcedir/onboot.lst and it will launch every time you boot the computer.

NOTE: If using the ISO, you will do this in the /mnt/sda1/cde directory instead of /mnt/sda1/tce directory
Title: Re: Y/N script
Post by: rexi on December 21, 2023, 12:10:58 PM
@rich I forgot to add apps to onboot.lst, everything works fine now, thanks.

@CentralWare I already created the application manually, but thank you for trying to help.