Author |
Message |
canbyte

Joined: 10 Jan 2009 Posts: 266 Location: Hamilton, Canada
|
Posted: Sun 03 Jun 2012, 12:17 Post subject:
Writing scripts (for noobs!) |
|
Hi
I posted this message but could see it myself. Maybe the moderator can fix why I can't see it. Thanks [This is your moderator speaking You had a space in the first URL in this post. I removed it and now the post shows as it should. The forum has a quirk where it will just show a blank post if it doesn't like a URL that's in the post. Check for spaces, carriage returns, line feeds, any characters not allowed in a URL.]
Good to know - thanks Mod
Anyway, It seems a couple of us noobs want to try writing some script. I understand scripts can be complicated so I hope you won't think we're too presumptuous. Well, maybe we are!!! Hopefully our projects aren't too lengthy so we can sort of muddle through with a bit of help.
We started out trying to automount/ auto unmount as described here. http://www.murga-linux.com/puppy/viewtopic.php?p=631338#631338 But in the interests of others who may want to try some script writing, I thought it would be worthwhile to start a general thread on the subject. Here are some thoughts collected thus far which hopefully others can add so it becomes a useful reference thread. Thanks in advance.
---------------------
So far I've found Puppy uses two scripts, one called Bash and another more basic script called sh. For instance, Drive_all is a script that controls automounting and is written in sh. RXVT terminal seems to be based on bash script so you can test stuff by opening a terminal in the directory of interest.
Here is some background info on sh
Quote: | Each script starts with a "shebang" and the path to the shell that you want the script to use, like so:#!/bin/bash
The "#!" combo is called a shebang by most Unix geeks. This is used by the shell to decide which interpreter to run the rest of the script, and ignored by the shell that actually runs the script. Confused? Scripts can be written for all kinds of interpreters — bash, tsch, zsh, or other shells, or for Perl, Python, and so on. You could even omit that line if you wanted to run the script by sourcing it at the shell, but let's save ourselves some trouble and add it to allow scripts to be run non-interactively.
The Bourne shell (sh), was the default Unix shell of Unix Version 7. Most Unix-like systems continue to have /bin/sh—
|
Bash seems like an easier script to understand than sh and includes things like a copy command which sh does not.
General thoughts for noobs:
. be careful in mods to basic puppy files as you may screw something up. If this happens, shutdown without saving or just use a ram only Puppy for experiments.
. there may be more than one termination point in the file you are trying to modify. I tried to tack on some bash commands at the end of an sh file without any response so maybe the next step is to try calling a new script file based on bash. Not sure how to do that but will edit later.
Here is a general / comprehensive Bash manual although I don't think the commands are the same as sh but we might find some clues.
http://sourceforge.net/projects/linuxcommand/files/TLCL/09.12/TLCL-09.12.pdf/download
This looks to be an 'official' reference manual for sh.
http://heirloom.sourceforge.net/sh/sh.1.html
This link explains when / why to use sh vs Bash
http://bsdwiki.reedmedia.net/wiki/Create_a_simple_Bourne_shell_script.html
EDIT: Instead of using 'reply' I'll just thank everyone for their suggestions /encouragement in the following posts here - thus i'll save the reply for when I have a specific question/problem as suggested. I think before I get to the 'waterstarting' stage as noted below, I'll just be doing some standard 'uphaul starts'. Which will make sense to those who enjoy my fav sport, windsurfing! Cheers.
_________________ 1. Dell Dimension E521, AMD Athln 64, 2 GHz 1.93GB ram,
Puppy 533 on CD, accesses flash drive only,
FFox Nightly12.0
2. Compaq P3 733Hz 375RAM
Printer: Oki C3400 > LAN
Last edited by canbyte on Mon 04 Jun 2012, 23:46; edited 1 time in total
|
Back to top
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 935
|
Posted: Sun 03 Jun 2012, 18:30 Post subject:
|
|
canbyte,
Just to clarify -
Puppy generally uses Bash. You may see scripts that are named "xxxx.sh" and some that have "#!/bin/sh " at the top but they are using "Bash".
Sh is a link to Bash. Enter "ls -l /bin/sh" in a terminal and you'll see that it is a link to Bash (/bin/sh -> bash).
Cheers,
s
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5087 Location: Arizona, U.S.A.
|
Posted: Sun 03 Jun 2012, 23:54 Post subject:
|
|
Hi canbyte; The copy command cp is a separate exec. sh or bash can run it.
It is part of BusyBox, but it`s disabled in Puppy as it`s not very capable.
If you look in /bin you`ll see many of the common commands, /sbin too.
|
Back to top
|
|
 |
jpeps
Joined: 31 May 2008 Posts: 3217
|
Posted: Mon 04 Jun 2012, 00:45 Post subject:
|
|
Hi canbyte:
There's at least several years worth of lessons available in numerous threads on scripting, in addition to a large collection of scripts for studying sitting right in the bins. Let us know if you have a specific problem with something you're working on...that's really what this thread is about. Picking up a book is always a good idea when trying to learn a language from scratch.
Quote: |
RXVT terminal seems to be based on bash script so you can test stuff by opening a terminal in the directory of interest.
Here is some background info on sh
|
Somehow, this reminded me of my initial entry into windsurfing, where, after purchasing my own board and expecting to sail away..I suddenly learned that there was just no way to climb onto the thing. One of the sailors introduced me to the term "waterstart"...something I had to go home an look up. It took a few months just to learn the technique so that I could just get onto the board and begin to learn how to windsurf. Then there was getting into the footstraps.....etc., etc.
|
Back to top
|
|
 |
Lobster
Official Crustacean

Joined: 04 May 2005 Posts: 15550 Location: Paradox Realm
|
Posted: Mon 25 Jun 2012, 04:55 Post subject:
|
|
I was very impressed with this one liner bash script
Code: | ps aux | awk '{if ($5 != 0 ) print $2,$5,$6,$11}' | sort -k2n |
then I remembered just typing top would get me similar . . .
Any fav one liners?
_________________ Puppy on Raspberry Pi Release Candidate
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html 
|
Back to top
|
|
 |
darkcity

Joined: 23 May 2010 Posts: 2549 Location: near here
|
Posted: Thu 19 Jul 2012, 04:53 Post subject:
|
|
its still a pretty cool one liner
learn my first awk command yesterday
Code: | # removes lines between tags and removes tags
awk '/start tag/{p=1};!p;/end tag/{p=0}' old.txt >> new.txt |
sed, grep and awk are useful, but there names and uses aren't overly intuitive
_________________ helping Wiki for help
|
Back to top
|
|
 |
Keef

Joined: 20 Dec 2007 Posts: 975 Location: Staffordshire
|
Posted: Thu 19 Jul 2012, 17:31 Post subject:
|
|
darkcity
That bit of awk-iness solves a problem I was stuck on ages ago.
Thanks!
I did a search on that line, and it took me here.
|
Back to top
|
|
 |
greengeek

Joined: 20 Jul 2010 Posts: 5624 Location: Republic of Novo Zelande
|
Posted: Fri 20 Jul 2012, 04:57 Post subject:
|
|
O.K if this is for noobs then I'm just gonna take myself off to the naughty mat and wrap a sheet over my head and have a little weep - cos this stuff looks REALLY complex!
Noobs can do this??? Dang. I need to go back to school.
|
Back to top
|
|
 |
Keef

Joined: 20 Dec 2007 Posts: 975 Location: Staffordshire
|
Posted: Fri 20 Jul 2012, 12:20 Post subject:
|
|
I wasn't pretending I understand that stuff....
I've done "Hello World" in every programming language under the sun - and apart from changing the text to something rude, that's as far as I usually get.
I'm sort of persisting with bash scripting, but my head still hurts.
|
Back to top
|
|
 |
kaeza
Joined: 30 Nov 2011 Posts: 51
|
Posted: Fri 20 Jul 2012, 13:04 Post subject:
|
|
*TROLL MODE ENGAGED*
My fav one liner is
You could also try
Code: | rm -rf $DESTDIR/bin |
Now guess what happens when DESTDIR was not set at all?
That's what I call sheer madness...
NOTE TO NEWBIES: THIS IS MEANT AS A SARCASTIC/TROLL MESSAGE!! DO NOT *EVAR* ATTEMPT THE SECOND COMMAND UNLESS YOU ARE 400% SURE THE 'DESTDIR' VARIABLE IS SET, AND IT IS SET TO WHAT YOU INTENDED!!!11!!!!one!!!
_________________ "Courage is not the absence of fear, but rather the judgement that something else is more important than fear." -- Ambrose Redmoon
|
Back to top
|
|
 |
kaeza
Joined: 30 Nov 2011 Posts: 51
|
Posted: Fri 20 Jul 2012, 13:21 Post subject:
|
|
@Keef
Here's a snippet for you (also for everyone):
Code: |
#! /bin/sh
ntries=0;
targetnum=$(((RANDOM%10)+1));
while [ $ntries -lt 10 ]; do
echo -n "Type a number between 1 and 10 inclusive:";
read num;
test "$num" || exit;
num=$((num));
ntries=$((ntries+1));
if [ "$num" -eq $targetnum ]; then
echo "Congrats pal! You got it in $ntries attempts!";
exit;
elif [ "$((num))" -gt $targetnum ]; then
echo "Target number is lower.";
else
echo "Target number is higher.";
fi
done
echo "You didn't make it in 10 attempts...";
|
A silly game done in 20 lines of code.
Post more games like this if you have any ideas.
_________________ "Courage is not the absence of fear, but rather the judgement that something else is more important than fear." -- Ambrose Redmoon
|
Back to top
|
|
 |
kaeza
Joined: 30 Nov 2011 Posts: 51
|
Posted: Fri 20 Jul 2012, 13:33 Post subject:
|
|
@sunburnt
Don't forget to mention that there are also commands on /usr/bin, /usr/sbin, and /usr/local/bin.
In fact, to know where the shell looks for commands, type this:
That should output something like
Each item in the list is separated by a colon (':') character, and it specifies a directory to look for executable files.
Of course, you can also specify the full path (as is required with the shebang) such as /bin/sh or /usr/bin/env or /my/fully/qualified/path/to/my/executable or whatever.
_________________ "Courage is not the absence of fear, but rather the judgement that something else is more important than fear." -- Ambrose Redmoon
|
Back to top
|
|
 |
darkcity

Joined: 23 May 2010 Posts: 2549 Location: near here
|
Posted: Sat 21 Jul 2012, 21:16 Post subject:
|
|
@Greengeek
awk seems typical of bash. a short incomprehensible command with lots of symbols attached, but which is extremely powerful. I don't understand it, but I understand one small corner - enough to get me by. The great thing about Linux is there are so many tutorials on the net and forums with people making just about every mistake possible ; -)
@Keef
that is the very page that help me out ; -)
@kaeza
neet game, is 4 the maximum number of guesses ever required?
_________________ helping Wiki for help
|
Back to top
|
|
 |
kaeza
Joined: 30 Nov 2011 Posts: 51
|
Posted: Sat 21 Jul 2012, 21:39 Post subject:
|
|
As you can see from the script, you can guess the number up to 10 times, but you can change that by modifying the line:
Code: | while [ $ntries -lt 10 ]; do |
Change the 10 to the maximum number of guesses you want.
You can also change the
Code: | targetnum=$(((RANDOM%10)+1)); |
to something like
Code: | targetnum=$(((RANDOM%34)+23)); |
To select between 23 and 34.
Also, here's a revised version (for experts only) with colors:
Code: | #! /bin/sh
ntries=0;
targetnum=$(((RANDOM%10)+1));
while [ $ntries -lt 10 ]; do
echo -ne "\033[34mType a number between 1 and 10 inclusive:\033[0m";
read num;
test "$num" || exit;
num=$((num));
ntries=$((ntries+1));
if [ "$num" -eq $targetnum ]; then
echo -e "\033[34;1mCongrats pal! You got it in $ntries attempts!\033[0m";
exit;
elif [ "$((num))" -gt $targetnum ]; then
echo -e "\033[34;1mTarget number is lower.\033[0m";
else
echo -e "\033[34;1mTarget number is higher.\033[0m";
fi
done
echo -e "\033[31;1mYou didn't make it in 10 attempts...\033[0m";
|
And another useless script:
Code: | #! /bin/sh
echo -e "\033[40m\033[J\033[H";
for bg in `seq 0 7`; do
echo -ne "\t";
for fg in `seq 0 7`; do
echo -ne "\033[3${fg};4${bg};1mXXX";
done
echo -e "\033[0m";
done
echo -ne "\033[24;1H\033[34;1mHit RETURN to exit.\033[0m";
read x;
echo -ne "\033[0m\033[J\033[H";
|
_________________ "Courage is not the absence of fear, but rather the judgement that something else is more important than fear." -- Ambrose Redmoon
|
Back to top
|
|
 |
darkcity

Joined: 23 May 2010 Posts: 2549 Location: near here
|
Posted: Sat 21 Jul 2012, 22:49 Post subject:
|
|
The expert version is a bit advanced for me, I like the colours though.
I meant using logic, if the range of number is 1 to 10, then you will always be able to guess in four or less go s.
_________________ helping Wiki for help
|
Back to top
|
|
 |
|