Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 14 Dec 2018, 02:30
All times are UTC - 4
 Forum index » House Training » Beginners Help ( Start Here)
Basic Shell (Console) operation for beginners
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 6 of 24 [350 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7, 8, ..., 22, 23, 24 Next
Author Message
Bruce B

Joined: 18 May 2005
Posts: 11488
Location: The Peoples Republic of California

PostPosted: Sat 12 Mar 2011, 05:38    Post subject:  

Just for fun, how fast can your computer execute 1,000,000 commands?

The script will execute
date 2x
: 999,998x

: is a command which says do nothing and give an successful error
code which would be 0

Code:
#!/bin/bash

date
for ((i=1;i<999999;i++));do
:
done
date


It takes my midrange machine about 17 seconds

Script name: millionx (attached)

Isn't there a command for timing execution? If so, what's its name?

~

EDIT - an improved millionx file is available down a few posts, better to
use it.
millionx.zip
Description 
zip

 Download 
Filename  millionx.zip 
Filesize  219 Bytes 
Downloaded  260 Time(s) 

_________________
New! Puppy Linux Links Page

Last edited by Bruce B on Sat 12 Mar 2011, 21:00; edited 1 time in total
Back to top
View user's profile Send private message 
Bruce B

Joined: 18 May 2005
Posts: 11488
Location: The Peoples Republic of California

PostPosted: Sat 12 Mar 2011, 06:29    Post subject:  

The if statement

This script gives examples of
    the if statement
    conditional checks and branching according to the condition
    using echo -n for echoing output without a linefeed
    how to get user input from within the program
    how to quote variables in on screen output
    using echo to print and format the output
    the 'exec' command to start a new script and terminate the existing script

I recommend downloading, reading, studying and running this script
You will be using the if statement and other commands in the script
over and over again when writing your scripts.

The 'for loop' is another which need be committed to memory.
Code:
#!/bin/bash

echo -n "Please enter a primary color: "
read clr
echo

if [ "$clr" = "red" ] ; then
   echo " You entered a valid primary color \"$clr\""
elif [ "$clr" = "green" ] ; then
   echo " You entered a valid primary color \"$clr\""
elif [ "$clr" = "blue" ] ; then
   echo " You entered a valid primary color \"$clr\""
elif [ "$clr" = "yellow" ] ; then
   echo " You entered a primary color used in painting \"$clr\""
else
   echo " \"$clr\" is not known to the program as a primary color"
   echo " We use red, green, and blue as primary colors"   
fi   

echo
echo -n "Again (y,n)? "
read -n 1 a
echo

if [ "$a" = "y" ] ; then
   echo
   exec $0

fi

echo


script name: colors, file attached

~
colors.zip
Description 
zip

 Download 
Filename  colors.zip 
Filesize  412 Bytes 
Downloaded  276 Time(s) 

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
Bruce B

Joined: 18 May 2005
Posts: 11488
Location: The Peoples Republic of California

PostPosted: Sat 12 Mar 2011, 07:29    Post subject:  

In a recent post I presented an idea of 'something in - something out'

We all know the concept unconsciously. We press the 'm' key, this is input.
We expect to see 'm' echoed as output on the screen. When it doesn't happen,
we think something is wrong. What is wrong can be conceptualized
as an input/output problem.

In this post I want to talk about thinking in terms of true and false.
If we are sane, we don't think in absolutes. In programming, true
and false is often all we are making our decisions on. If you program
for several years, you will probably lose sanity.

My advice is, after the sanity is lost, be calm, quite, soft spoken,
articulate and whatever you do, don't appear to be a danger to
others. Chances are they won't lock you up, if you take my advice.

Let's look at our if statement from a true / false perspective.

Code:
if [ "$clr" = "red" ] ; then
   if false no commands execute
   if true the commands execute
elif [ "$clr" = "green" ] ; then
   the elif is shorthand for 'else if'
   the only chance of the elif running commands is if the 'if' was not true
   the elif can only execute if it is true
   if false nothing happens
elif [ "$clr" = "blue" ] ; then
   same as above
elif [ "$clr" = "yellow" ] ; then
   same as above above
else
   if none of our if or elif statements were true
   the else commands will execute
   they will only execute under these circumstances
fi   

We have five possible conditions, but only one will execute commands

We only need the 'elif' and 'else' - if we need them.

The basic if statement is like this

Code:
if [ "3" = "4" ] ; then
   echo 3 equals 4
fi


3 doesn't equal 4 so nothing happens, because it is a false condition

Code:
if [ "4" = "4" ] ; then
   echo 4 equals 4
fi


Above we have a true condition, so the command runs.

Code:
if [ "3" = "4" ] ; then
   echo 3 equals 4
else
   echo 3 does not equal 4
fi


See how it works, the first test is false, we programmed in the else.
The else executes because the first 'if test' was false. If it were true
the else would not execute commands.

~

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
Bruce B

Joined: 18 May 2005
Posts: 11488
Location: The Peoples Republic of California

PostPosted: Sat 12 Mar 2011, 08:00    Post subject:  

Piping commands through scripts - introduction

We can make some very useful scripts (and C programs) for use as
pipes.

I'll give a couple bare bones examples

Filename: upperc
contents:
tr "a-z" "A-Z"

Filename lowerc
contents:
tr "A-Z" "a-z"

To make the pipes, put the corresponding line at the top of the file,
name the file and make it executable.

Then if you want to change case of a string of text or even a file, you
can pipe the text through the executable text file.

You can actually pipe through some complex scripts, but this is just
an introductory to the concept.

~

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
Bruce B

Joined: 18 May 2005
Posts: 11488
Location: The Peoples Republic of California

PostPosted: Sat 12 Mar 2011, 11:37    Post subject:  

About Characters

Our characters are not really characters from the computer's perspective.
They are binary values.

There are currently eight people in the whole world, aliens maybe, who
can read binary strings.

Thousands of propeller heads can read octal and hex.

Normal people read decimal math. Although hex is very useful in
programming.

Each character we see doesn't exist except by translation from binary to
character.

Sometimes we want to know the value of our characters, especially in C
and probably other programs.

The following program will display characters and their decimal
equivalents in your bash shell.

You might want to keep the program for future reference.

After the program runs, you can scroll up to see what scrolled off screen.

Code:
#!/bin/bash

for ((i=33;i<127;i++)) ; do
   printf "\x$(printf %x $i )"
   echo "  has decimal value of $i"
done


Script file attached

~
charvalues.zip
Description 
zip

 Download 
Filename  charvalues.zip 
Filesize  255 Bytes 
Downloaded  269 Time(s) 

_________________
New! Puppy Linux Links Page

Last edited by Bruce B on Sat 12 Mar 2011, 11:57; edited 1 time in total
Back to top
View user's profile Send private message 
Bruce B

Joined: 18 May 2005
Posts: 11488
Location: The Peoples Republic of California

PostPosted: Sat 12 Mar 2011, 11:53    Post subject:  

A C type 'for loop' construct

I've used this construct twice so far, without explanation.

for ((i=33;i<127;i++))

i=33 initializes the variable i with the value of 33
i<127 terminates the loop after count reaches 126
i++ increments the value by one on each iteration

You can use this construct when you need a loop that counts. It is not
used much in bash, but used extensively in C

Another way is use the normal for loop like this

for i in 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 and etc.

But that is work, we want better ways to improve our laziness.

Try this on the command line

# myvar=`echo {33..126}` (those are braces and backticks)

then

# echo $myvar

After setting the variable, you could then use the conventional
for construct

for i in $myvar

~

_________________
New! Puppy Linux Links Page

Last edited by Bruce B on Sat 12 Mar 2011, 13:13; edited 2 times in total
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3217

PostPosted: Sat 12 Mar 2011, 12:45    Post subject:  

Bruce B wrote:


Isn't there a command for timing execution? If so, what's its name?

~


"timer" script; put in $PATH
Code:

#!/bin/sh

#### begin with:  "timer & "
###  end with:    "kill `pidof timer` "

SEC="0"

echo -en "\nWorking..............."
while [ "$0" ]; do
  for ((i=0;i<1000001;i++)); do
 
    SEC="$(( $SEC+1 ))"
 [ ${SEC} -gt 0 -a ${SEC} -lt 11 ] &&  echo -en "$SEC"   
 [ ${SEC} -gt 10 -a ${SEC} -lt 101 ] &&  echo -en "\b$SEC"   
 [ ${SEC} -gt 100 -a ${SEC} -lt 1001 ] &&  echo -en "\b\b$SEC"   
 [ ${SEC} -gt 1000 -a ${SEC} -lt 10001 ] &&  echo -en "\b\b\b$SEC"   
 [ ${SEC} -gt 10000 -a ${SEC} -lt 100001 ]  &&  echo -en "\b\b\b\b$SEC"   
 [ ${SEC} -gt 100000 -a ${SEC} -lt 1000001 ] &&  echo -en "\b\b\b\b\b$SEC"   
    echo -en "\b"
    sleep 1
   done
done


Then:

Code:

#!/bin/bash

date
exec timer &
for ((i=1;i<99999;i++));do
:
done
date
kill `pidof timer`
Back to top
View user's profile Send private message 
Moose On The Loose


Joined: 24 Feb 2011
Posts: 778

PostPosted: Sat 12 Mar 2011, 12:57    Post subject:  

Bruce B wrote:
A C type 'for loop' construct

# myvar=`{33..126}` (those are braces and backticks)




Code:

# myvar=`{1..10}`
bash: 1: command not found


# myvar=`echo {33..126}`
# echo $myvar
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#
Back to top
View user's profile Send private message 
Bruce B

Joined: 18 May 2005
Posts: 11488
Location: The Peoples Republic of California

PostPosted: Sat 12 Mar 2011, 13:14    Post subject:  

Moose On The Loose,

Thanks, I fixed the original post.

Bruce

I could say, I'm doing this to demonstrate that FOSS is a better way of
development because of peer review. But probably, I had insomnia. Also
known as sleeping at the keyboard.

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
Bruce B

Joined: 18 May 2005
Posts: 11488
Location: The Peoples Republic of California

PostPosted: Sat 12 Mar 2011, 15:11    Post subject:  

jpeps,

The timer script is a keeper. Thanks.

I found a perfect second timer for us. I'll post and explain it later.

As mentioned, I taught myself, mostly through books. My dad was/is a
computer scientist, which I found helpful. Still, most of what I learned is
self taught.

What I found over and over is the authors would do OK for a few chapters,
then get over my head. I call it an easy gradient followed by too steep a
gradient.

I would like to teach a lot of basics, both concepts and language.

I really don't want to do what others did to me, which is hit me with a
steep gradient.

You know enough, you may not catch it if the gradient was too steep.

I would like however some PMs advising me about the gradient and if the
teaching is happening without too much brain strain.

Bruce

I also wonder if it is a good thing to listen to Pink Floyd at 77% volume
while doing this.

~

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2643

PostPosted: Sat 12 Mar 2011, 15:14    Post subject:  

'time' is the command for timing operations. If you need to time the toal of a bunch of repitions, then put the repeating vommands in a function and then: 'time func_name' should give the total of all the loops.
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3217

PostPosted: Sat 12 Mar 2011, 16:07    Post subject:  

amigo wrote:
'time' is the command for timing operations. If you need to time the toal of a bunch of repitions, then put the repeating vommands in a function and then: 'time func_name' should give the total of all the loops.


also can run "time myscript"
Back to top
View user's profile Send private message 
PupGeek

Joined: 06 Sep 2009
Posts: 388

PostPosted: Sat 12 Mar 2011, 18:46    Post subject:  

Bruce B wrote:

What I found over and over is the authors would do OK for a few chapters, then get over my head.


Same here.... every time..... and it always gets crazy right around variables and expressions. And arrays? forget it.

I think there are not enough examples to cover every scenario and that some of the examples are not very interesting as well. When I was in college for electronics, we (meaning the professor and the students) engineered and built a laboratory power supply right from the ground up. We all had a voice and applied our theoretical knowledge and lab experiences for this project. I think this would be a good approach to learning programming too. Kinda what I tried to do when starting this thread.

oh, and as for aliases being portable, could you not define them in the script you want to share? I know this would only be advantageous in long scripts that use the alias(es) numerous times.
Back to top
View user's profile Send private message 
Bruce B

Joined: 18 May 2005
Posts: 11488
Location: The Peoples Republic of California

PostPosted: Sat 12 Mar 2011, 21:07    Post subject:  

I tried the time command and couldn't figure out how to apply to the
script. Some examples please?

Fortunately, I found Lupu 5.20 replaced the BusyBox date extension with
the 'real' date command, which has a second timer.

This makes the math super easy, just subtract the start time from the
finish time. No hour or minute roll overs to complicate things.

Code:
#!/bin/bash

echo Executing one million commands
start=`date +%s`
for ((i=1;i<999999;i++));do
:
done
stop=`date +%s`
total=`expr $stop - $start`
echo "Total time: $total seconds"


If script doesn't right work on your Puppy, I can upload the real date
command.

~
millionx.zip
Description 
zip

 Download 
Filename  millionx.zip 
Filesize  289 Bytes 
Downloaded  286 Time(s) 

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
Bruce B

Joined: 18 May 2005
Posts: 11488
Location: The Peoples Republic of California

PostPosted: Sat 12 Mar 2011, 22:28    Post subject:  

PupGeek,

After reading your post, I played some with using aliases for adding color
to text output. I wasn't pleased.

I then made functions and the results looked great.

Adding colored output gives a more professional look to the script. But
colors are a hassle, unless made easy, which I think I did.

You can give the functions the names you want, in this script, I used this pattern:

whi = normal white
bwhi = bright white
and so on
std = return to normal

Script usage examples

cya
echo Puppy Linux
std

# or use ;
gre;echo Lupu 5.20;std

#or

bmag;echo -n "Puppy Linux ";bwhi;echo version 5.20;std
red;echo "-- Read Warning --"
bred;echo "-- Read Warning --";std


The functions
Code:
function bla() {
    echo -en "\033[30m"
}
function red() {
    echo -en "\033[31m"
}
function gre() {
    echo -en "\033[32m"
}
function yel() {
    echo -en "\033[33m"
}
function blu() {
    echo -en "\033[34m"
}
function mag() {
    echo -en "\033[35m"
}
function cya() {
    echo -en "\033[36m"
}
function whi() {
    echo -en "\033[37m"
}
function std() {
    echo -en "\033[0m"
}

function bred() {
    echo -en "\033[31;1m"
}
function bgre() {
    echo -en "\033[32;1m"
}
function byel() {
    echo -en "\033[33;1m"
}
function bblu() {
    echo -en "\033[34;1m"
}
function bmag() {
    echo -en "\033[35;1m"
}
function bcya() {
    echo -en "\033[36;1m"
}
function bwhi() {
    echo -en "\033[37;1m"
}


You can copy and paste the text to the top of your file OR you can import
the functions.

Import Example: . /root/bin/clrs

File attached: clrs
clrs.zip
Description 
zip

 Download 
Filename  clrs.zip 
Filesize  285 Bytes 
Downloaded  274 Time(s) 

_________________
New! Puppy Linux Links Page

Last edited by Bruce B on Sun 13 Mar 2011, 00:43; edited 1 time in total
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 6 of 24 [350 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7, 8, ..., 22, 23, 24 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » House Training » Beginners Help ( Start Here)
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0945s ][ Queries: 12 (0.0226s) ][ GZIP on ]