Writing scripts (for noobs!)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
canbyte
Posts: 264
Joined: Sat 10 Jan 2009, 20:20
Location: Hamilton, Canada

Writing scripts (for noobs!)

#1 Post by canbyte »

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 :lol: 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/viewto ... 338#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
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/linuxco ... f/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/Creat ... cript.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.
Last edited by canbyte on Tue 05 Jun 2012, 03:46, edited 1 time in total.
[color=orange]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 [/color]

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#2 Post by seaside »

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

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#3 Post by sunburnt »

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.

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#4 Post by jpeps »

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.
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.

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#5 Post by Lobster »

I was very impressed with this one liner bash script

Code: Select all

ps aux | awk '{if ($5 != 0 ) print $2,$5,$6,$11}' | sort -k2n
then I remembered just typing top would get me similar . . . :oops:

Any fav one liners?
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
darkcity
Posts: 2534
Joined: Sun 23 May 2010, 19:16
Location: near here
Contact:

#6 Post by darkcity »

its still a pretty cool one liner

learn my first awk command yesterday

Code: Select all

# 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 :twisted:

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#7 Post by Keef »

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.

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#8 Post by greengeek »

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.

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#9 Post by Keef »

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.

kaeza
Posts: 51
Joined: Thu 01 Dec 2011, 02:49

#10 Post by kaeza »

*TROLL MODE ENGAGED*

My fav one liner is

Code: Select all

kill 0
You could also try

Code: Select all

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!!!
[i]"Courage is not the absence of fear, but rather the judgement that something else is more important than fear."[/i] -- Ambrose Redmoon

kaeza
Posts: 51
Joined: Thu 01 Dec 2011, 02:49

#11 Post by kaeza »

@Keef

Here's a snippet for you (also for everyone):

Code: Select all

#! /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.
[i]"Courage is not the absence of fear, but rather the judgement that something else is more important than fear."[/i] -- Ambrose Redmoon

kaeza
Posts: 51
Joined: Thu 01 Dec 2011, 02:49

#12 Post by kaeza »

@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:

Code: Select all

echo $PATH
That should output something like

Code: Select all

/bin:/usr/bin:...
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.
[i]"Courage is not the absence of fear, but rather the judgement that something else is more important than fear."[/i] -- Ambrose Redmoon

User avatar
darkcity
Posts: 2534
Joined: Sun 23 May 2010, 19:16
Location: near here
Contact:

#13 Post by darkcity »

@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 ; -)
Image

@kaeza

neet game, is 4 the maximum number of guesses ever required?

kaeza
Posts: 51
Joined: Thu 01 Dec 2011, 02:49

#14 Post by kaeza »

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: Select all

while [ $ntries -lt 10 ]; do
Change the 10 to the maximum number of guesses you want.

You can also change the

Code: Select all

targetnum=$(((RANDOM%10)+1));
to something like

Code: Select all

targetnum=$(((RANDOM%34)+23));
To select between 23 and 34.

Also, here's a revised version (for experts only) with colors:

Code: Select all

#! /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: Select all

#! /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";
[i]"Courage is not the absence of fear, but rather the judgement that something else is more important than fear."[/i] -- Ambrose Redmoon

User avatar
darkcity
Posts: 2534
Joined: Sun 23 May 2010, 19:16
Location: near here
Contact:

#15 Post by darkcity »

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. :arrow:

Post Reply