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 Mon 28 Jul 2014, 19:06
All times are UTC - 4
 Forum index » House Training » Users ( For the regulars )
The Text-User Interface
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 2 of 8 [110 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7, 8 Next
Author Message
Bruce B


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

PostPosted: Thu 02 Jul 2009, 21:38    Post subject:  

Find scripts

You've already been introduced to; grep, redirection, variables.

Now, explaining command line variables. They are numbered from left to right as you type them. If you have three arguments, then you have variables $1 $2 $3

$@ means all command line arguments
$# means the number of arguments

In this example we use the case statement to determine which commands to run. And this is your first viewing of case in this study

The information we give case is $# which is the total count of our arguments. Valid matches, (true conditions), are 1 to 3. If we don't give case a number between 1 to 3 arguments, then * is true and it echos an error and does nothing.

Have a look and see what you can learn and derive.

Code:
#!/bin/bash

main() {

    var
    dofind "$@"

}


var() {

    file="/var/log/updatedbd"

}

dofind() {

    case $# in

        1)
        <$file grep --ignore-case "$1"
        ;;

        2)
        <$file grep --ignore-case "$1" \
        | grep --ignore-case "$2"
        ;;

        3)
        <$file grep --ignore-case "$1" \
        | grep --ignore-case "$2" \
        | grep --ignore-case "$3"
        ;;

        *)
        echo "Incorrect number of arguments"
        ;;

    esac

}   

main "$@"


To install these scripts, download the package, use Midnight Commander to open the zip package in a virtual filesystem and copy the three scripts to /root/bin

findf is for finding files
findd is for finding directories
updatedb is for building the databases

findf means find files
findd means find directories
First run updatedb to build your database, then run findd and see if you can locate the directories containing pixmaps and icons.

Remembering, we don't want to type too much, if you want the output of a line, just double-click it to select it and shift+insert or push mouse button to paste the text on your command line or into an editor. Useful when writing scripts to make sure you don't make a typo.

Suppose you want to change to a directory in your output, type cd (space) in the command line, then paste the directory to the command line and hit enter.

There are many ways to keep from typing too much when using the command line, in time we will learn more and more

~

Chapter 15 - Find scripts
findutils.zip
Description  findutils kit : contents : three scripts
zip

 Download 
Filename  findutils.zip 
Filesize  1.03 KB 
Downloaded  1068 Time(s) 

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


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

PostPosted: Thu 02 Jul 2009, 22:16    Post subject:  

tar and gzip

This chapter is short, sweet and important. I'll tell you why it's important. In the thousands of posts I've read on the forum, I seen many, many posts were it's obvious to me the problem is somewhere in the directory /etc

But I can't tell remotely exactly what the problem is.

/etc is not a static directory like /bin. /etc changes some every boot. Usually what brings on the users problem is a bad shutdown. Then, they can't recover.

Let's avoid problems by maintaining a backup of /etc
Code:
# filename bkup-etc
tar cvzf /var/etc.tar.gz /etc
echo Done!
ls -l --color=always /var/etc.tar.gz


What's introduced in this script is tar and gzip.

I'll talk a little about tar. We only run one utility here, tar, but tar works in conjunction with gzip (or bzip2). So we are running two utilities, although it doesn't look like it.

A .tar.gz file is very, very common in Linux. You will also find you can make and extract tar.gz archives very easily with Midnight Commander, simply hit the F2 (the menu) key to intuit how.

Tar makes the archive, but it's uncompressed, gzip is the utility that compresses the tar archive. This is why the two extensions, tar says a tar archive and gz says compressed by gzip. But sometimes it's displayed as .tgz. When you encounter a .tgz or tar.gz file, just know it is the same thing, described differently.

The switches used are cvzf

c - create
v - verbose output
z - says compress it with gzip
f - the filename

f - should be the last in the chain, just proceeding the filename

If we wanted to extract the package we'd say

tar xvzf etc.tar.gz

(the x says extract)
(the c says create)

Not so hard. You already have the tools and know how to easily make the script - bkup-etc, so do it.

/root
contains your user settings and maybe important files. If you have the space to backup /root - I recommend doing it - use bkup-etc as your template for making the script.

The etc.tar.gz will be about 1/2 megabyte

If /root is too big for pup_save, you could do something like this:

tar cvzf /mnt/home/root.tar.gz /root


I'll leave it to you as an extra-curricular kind of thing to figure out the logistics.

~

Chapter 16 - tar and gzip

~

_________________
New! Puppy Linux Links Page

Last edited by Bruce B on Sat 04 Jul 2009, 09:59; edited 1 time in total
Back to top
View user's profile Send private message 
Bruce B


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

PostPosted: Sat 04 Jul 2009, 05:21    Post subject:  

Change Directories - the cd command

The cd command is what we use to change directories. I'll start by offering some examples

cd : changes to the $HOME directory, for us it would be: /root

cd .. : takes us up one level

cd ../.. : takes us up two levels

cd /bin : takes us to /bin

cd bin : takes us to a child directory bin

cd /usr/bin : takes us to /usr/bin

bin : takes us to /root/bin - the reason why is, we made an alias called bin

cd - : returns us to our last directory

Related commands pushd and popd

pushd /usr/local/bin changes to /usr/local/bin and saves the directory we were in when we ran pushd

popd : returns to the the directory saved by pushd, 1x only

Adding extra commands

The uu command saves our current directory for the duration of the session or until we run uu again

The u command returns us to the directory saved by uu

The ad command displays the first level child directories, if any, of our current working directory.

The cdd command behaves like cd and shows all the first level subdirectories, if any.

The normal cd command used this way: cd /usr will change directories to /usr but not show the subdirectories in /usr

cdd /usr will change to /usr and show all the subdirectories in /usr/ This makes it easy to dig deeper with tab completion or copy and paste the output.

Adding the new commands;

Code:
function cdd {
   [ $1 ] && cd $1
   ad
}
alias uu='echo cd $PWD>/tmp/uu~'
alias u='source /tmp/uu~'
alias ....='cd ../../../.. ; ad'
alias ...='cd ../.. ; ad'
alias ...='cd ../../.. ; ad'
alias ..='cd .. ; ad'
alias rf='source /root/.bashrc ; echo "bashrc refreshed"'

To install these new aliases and function;

bashrc
followed by the old copy, paste and save routine

ad is a shell script. Download ad.zip. Add the contents 'ad' to our /root/bin directory using Midnight Commander.

After you've used ad a bit, I'll explain it. Part of what I'm doing in these earlier chapters is helping make your TUI environment as user friendly as possible. Soon, quite soon, changing directories on the TUI will be easier and faster than with the GUI, but it doesn't start off that way.

~

Chapter 17 - Change Directories - the cd command
ad.zip
Description  ad.zip : all directories : a script to show all subdirectories
zip

 Download 
Filename  ad.zip 
Filesize  411 Bytes 
Downloaded  995 Time(s) 

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


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

PostPosted: Sat 04 Jul 2009, 06:32    Post subject: Why?
Subject description: Chapter 18
 

Why?

In our last chapter, we made aliases, a function and installed a shell script. In this chapter I'll present theory for the whys of our whats.

Changing directories in a shell script

Remembering that our shell script is a child process, if we change directories in a script, we will change directories in the script. When the script is finished we are in the same directory we were in when when ran the script.

This script called usrbin;

#/bin/bash
cd /usr/bin


will actually do what it says, but when it finishes we are left in the same directory we ran it from.

Changing directories with an alias

This alias of the same name: usrbin

alias usrbin='cd /usr/bin'


Actually changes our current directory to /usr/bin, just as if we typed the commands. This alias doesn't launch a child process, thus giving the desired results.

What about the function?

We also added a function to .bashrc , I'll explain the rational for this now;

function cdd {
[ $1 ] && cd $1
ad
}


The function as an alias would look like this:

alias cdd='[$1] && cd $1 ; ad'


The problem with using an alias; is I've found aliases horrible for accepting command line arguments in the middle of the structure.

But they do great at the end of the structure, here's a working example;

alias mount='mount-FULL'

With this alias, when I use the command mount, on the command line, the actual mount command will be used, which in Puppy, and probably only Puppy is, mount-FULL

aliases accept appended arguments very well. Our problem is inserting them somewhere other than the end.

mount -t ext /dev/hda1 /mnt/hda1 , without an alias runs a Puppy shell script called mount which in turn will run mount-FULL and return to the parent shell when finished

With the alias

mount -t ext /dev/hda1 /mnt/hda1 runs the mount-FULL in the current shell

Why the shell script ad?

It works swell as a shell script, it lends itself well to shell scripting.

There nothing wrong with shell scripting. We will be doing a lot of shell scripting.

Major point: Sometimes there is a better approach to shell scripting, especially when trying to change directories in our current shell.

~

Chapter 18 - Why?

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


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

PostPosted: Sat 04 Jul 2009, 07:18    Post subject: Chapter 19
Subject description: Troubles with whitespace
 

Troubles with whitespace

For our purposes; whitespace could be defined as characters which don't display.

Our most common text editor whitespace characters would be line endings, space and tab characters.

In bash we have the backslash \ character, an instructoion for the bash interpreter.

For this post I'll define \ as the 'do something different for the next character character'

In bash, the end of line (eol) character is the signal for end of command.

Sometimes I post code and have you insert the code, other times I don't post the code and have you download it.

Here is an excerpt from the file 'ad' in chapter 17

Code:
    find  -maxdepth 1 -type d | cut -b 3-80 \
    | sort -g | mkfiles


Note the use of \, it says do something different about the next character, which is the eol character, (0x09). What it does different is; bash doesn't treat the whitespace, the eol, as a signal that the command is complete.

It continues the command execution to the line below. Without the \ character the command would need to look like this:

Code:
    find  -maxdepth 1 -type d | cut -b 3-80 | sort -g | mkfiles


Sometimes we make very long commands, longer than our text editor can display and longer than a forum post will safely display.

The forum will use the space character as a delimiter for line wrapping. We don't know our reader's font size or screen resolution. Because of this, we can get unwanted line wrapping with long code excerpts.

The long code might wrap at the wrong place. If the user copies and pastes code which the forum has wrapped (at the wrong place for code), the code will likely be very defective.

With the code block below, it is imperative that no white space follow the \ except for the end of line character. By using the \ we have to be sure the character we want to treat differently is actually - the eol character - and not a space character.

I didn't post the code, because I don't want it copied from the forum and have someone inadvertently insert a space character. To be safe, download it, because if I'm doing my part right, there are no boo-boos in the in the code and it will run as expected.
Code:

    find  -maxdepth 1 -type d | cut -b 3-80 \
    | sort -g | mkfiles


~

Chapter 19 - Troubles with whitespace

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


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

PostPosted: Sat 04 Jul 2009, 08:31    Post subject: Teaching on a gradient - learning on your own
Subject description: Chapter 21
 

Teaching on a gradient - learning on your own

Many people associate learning with pain. I expect if you are even reading Chapter 21, you aren't one of these types of people. Or if you are, you have enough determinism and desire to learn not to let the pain association be a deterrent.

I love learning, but I really don't want anyone teaching me at a gradient too steep for me ( too painful ).

At this point of your learning experience, I'd like to state or restate, some of what I'm doing.
    1) I'm trying to gradually help you set up a more friendly and powerful TUI environment than the one you started with. In doing this I might find myself a bit a cross-purposes with another objective, which is;

    2) teaching bash scripting on an easy to learn gradient.
Obviously, I want you working in as friendly and powerful environment as possible. We build this environment gradually.

If you find some file unexplained and over your head, it is very possible, I'm trying to help with number (1). Explaining every aspect of a file may be on too steep a gradient for number (2), it might remain unexplained for this reason.

If you run into a situation like this, I advise; do not try and understand the file or procedure, if it is too hard, especially if stands to reason its purpose is in category number (1).

Two requisites defined for the bash programmer
    1) Understanding the directory tree and files

    2) File management skills
Two requisites explained

If you don't have a solid grasp of the directory tree and files, your file management skills will be lacking. Your programs will not be as expansive as they otherwise could be. Lots of what we might want to do in scripting pertains to file management.

Optional self-learning assignment

( Midnight Commander is a powerful file manager and just by using it, you will become gradually more familiar with Linux' directories and files. ) ( have you noticed with mc, you don't need to extract the .zip package and many other packages to copy files out of them? )

    1) download the package tree.zip, its contents are one binary named tree and one text file called tree.txt which is the --help for tree.

    At this point, no instructions should be needed. Simply put the binary file tree it it's appropriate directory and the documentation tree.txt in its appropriate directory.

    2) using the Internet search, find a page which helps you to learn Linux default or basic directory tree. Read it and learn the basics, ( if you need the instructions at all ).

~

Chapter 21 - Teaching on a gradient - learning on your own

~
tree.zip
Description  contents : binary file - tree : text file - tree.txt
zip

 Download 
Filename  tree.zip 
Filesize  16.25 KB 
Downloaded  966 Time(s) 

_________________
New! Puppy Linux Links Page

Last edited by Bruce B on Sat 04 Jul 2009, 10:01; edited 1 time in total
Back to top
View user's profile Send private message 
Bruce B


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

PostPosted: Sat 04 Jul 2009, 09:50    Post subject: Formatting and colorizing text - a preview
Subject description: Chapter 22
 

Formatting and colorizing text - a preview

This chapter is a preview for the next chapter, which will make very easy work of colorizing and formatting your script's on screen output.

Tastefully adding color to your text output, can add a professional look to your scripts.

When and where to color your text will be up to you.

Showing you how, and how to do it with ease, is my job.

See picture for preview.

~

Chapter 22 - Formatting and colorizing text - a preview
colorizing-text.png
 Description   Demo - colored text on the console
 Filesize   2.57 KB
 Viewed   2182 Time(s)

colorizing-text.png


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


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

PostPosted: Sat 04 Jul 2009, 12:48    Post subject:  

Displaying text with our scripts

The echo command is internal to our command processor, bash. How to know?

Type help and see if echo is in the list

In Puppy, the echo command may also be an external command. How to know?

which echo
and/or
ls -l /bin/echo

The rule is: an internal command will have precedence to an external one. However, if for some reason we wanted to use the external one we could, if it exists, by specifying the full path;

/bin/echo

Using echo

We typically use echo for displaying something to standard output (or in redirection). I want to emphasize echo's abilities go beyond what it is typically used for:

How about math? Try this command:

echo $[5+23]

Displaying files, try this:

echo *

Using quotes as a safety net

Until we learn the ins and outs and the peculiarities of echo, I recommend quoting what you wish to display. By quoting your intended output in your script, you are more likely to have the results you intended.

echo "My last directory was: $OLDPWD"

echo's default is to give a line feed at the end of the line. Remember from earlier the 'eol' is a non visible character, but it affects the formatting of our output.

echo -n says don't use a line feed. Let's try the above command with echo -n as follows;

echo -n "My last directory was: $OLDPWD"

echo -e : means enable interpretation of \ escape characters, which we need for coloring

Introducing cecho

If we type cecho on the command line, bash tells us:

cecho: command not found

Good, this is an available command name. We will call the our new command: cecho
It means; color echo

cecho usage:

cecho color "text for screen display"

for no new line;

cecho color "text for screen display" n

The cecho function, displayed in its present form, but I want to refine some things before you install it. The refinements and install instructions, will have to wait until next chapter. (or the next, next) Sorry. I'm starting to learn what they mean when saying, "Rome wasn't built in a day."

Code:
cecho() {

    case $1 in

        black)    echo -e$3 "\\033[1;30m""$2"
        ;;   # black

        blue)     echo -e$3 "\\033[1;34m""$2"
        ;;   # bright blue

        cyan)     echo -e$3 "\\033[1;36m""$2"
        ;;   # bright cyan

        green)    echo -e$3 "\\033[1;32m""$2"
        ;;   # bright green

        magenta)   echo -e$3 "\\033[1;35m""$2"
        ;;   # bright magenta

        red)      echo -e$3 "\\033[1;31m""$2"
        ;;   # bright red

        white)    echo -e$3 "\\033[1;37m""$2"
        ;;   # bright white

        yellow)   echo -e$3 "\\033[1;33m""$2"
        ;;   # bright yellow

        black0)  echo -e$3 "\\033[0;30m""$2"
        ;; # black normal

        blue0)     echo -e$3 "\\033[0;34m""$2"
        ;;   #  normal

        cyan0)     echo -e$3 "\\033[0;36m""$2"
        ;;   #  cyan normal

        green0)    echo -e$3 "\\033[0;32m""$2"
        ;;   #  green normal

        norm*)     echo -en "\\033[0;39m"
        ;;   # normal normal

        magenta0)  echo -e$3 "\\033[0;35m""$2"
        ;;   #  magenta normal

        red0)      echo -e$3 "\\033[0;31m""$2"
        ;;   #  red normal

        white0)    echo -e$3 "\\033[0;37m""$2"
        ;;   #  white normal

        yellow0)   echo -e$3 "\\033[0;33m""$2"
        ;;   #  yellow normal

    esac

}


~

( the function works, but I think I can do better )

Chapter 23 - Displaying text with our scripts

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


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

PostPosted: Sat 04 Jul 2009, 17:26    Post subject: The case statement, escape sequences
Subject description: Chapter 24
 

The case statement, escape sequences

Code:
case $1 in

    blue)     echo -e$3 "\\033[1;34m""$2"
    ;;   # bright blue

    green)    echo -e$3 "\\033[1;32m""$2"
    ;;   # bright green

esac

Define 'case $1 in' in English; "In case my first argument matches my criteria, (blue or green), then do the commands in my criteria. Otherwise do nothing."

------------------

What does blue mean?

It is a search criteria for $1

Literally, to bash, blue means: echo -e$3 "\\033[1;34m""$2" , because we made it mean that, you see how?

I think 'blue' is both easier and more intuitive, than the full bash command, thus the reason for the function I posted (and will refine). Make it easy!

-----------------

How many arguments are in this command?

cecho blue This is my text to display n

If we don't consider cecho as an argument, which it is. (The command name is $0) We have 8 arguments.

But blue only accepts 3 arguments. Very important to understand is; how to make 8 arguments into 3 arguments, a basic answer is: with quotes;

cecho blue "This is my text to display" n

arg1 = blue
arg2 = This is my text to display
arg3 = n

blue) echo -e$3 "\\033[1;34m""$2" ;;

The 3 arguments explained;

$1 is our search criteria name and our actual color
$2 is the full quoted text
$3 is the optional 'n' argument, which is appended to echo -e, thus making echo -en

If $3 doesn't exist, it is nothing, of null effect. If it exists as 'n', it tells cecho not to add a new line.

The \ is the character I call the do something different character. In the case of \\, I interpret it as saying do something different to the do something different character :)

Combined with the echo -e switch, the full string, echo -e "\\033[1;34m" changes the text attributes to boldblue.

The double ;; signals the end of the particular matched case criteria execution - and all execution in the case statement.

~

Chapter 24 - The case statement, escape sequences

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


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

PostPosted: Sat 04 Jul 2009, 17:47    Post subject:  

A function based program

In this chapter we will work together and build a function based program. Programming begins with an idea. I have an idea that I would like you to be able to easily, very easily, as easy as possible make practice scripts.

I will title the program 'fun' and if that name doesn't suit you we will write it so you can change its title.

Scripts run commands. Scripts read from top to bottom. When I build a completely function based script, the first command is at the bottom. Putting it on the bottom means bash has read the entire script, (its functions) before any program execution begins.

The 'explanation formatting' in this chapter will be first the script, followed by explanation.

Code:
#!/bin/bash

main() {

   echo

}

main

We now have a complete script that only echoes a linefeed. The reason for the echo command is bash doesn't want us to leave our functions empty. Echo doesn't fill our need, rather a bash demand.

Code:
var() {
   
   wrkdir="/root/bin"
   cmd=`basename $0`
   test "$wrkdir" = ""  && "echo variable wrkdir is empty" && exit
   test ! -d $wrkdir && "echo working directory doesn't exist" && exit
   cnt="0"
   quit="1"

}


var is the function we use to define and process variables

wrkdir : our working directory, make sure our files are always in that directory

cmd : is the stripped $0 , using basename we convert it from: /root/bin/fun to: fun

tests : unnecessary used as example. but, sometimes wisdom says to test condition prior to execution of other commands

cnt : we set our counter to zero, it will be used in the next function : mkfile

quit : quit will also be used in the next function : mkfile

Code:
makefile() {

   cd $wrkdir

   while [ "$quit" = "1" ] ; do
      
      cnt=`echo $[$cnt+1]`
      if [ ! -f ${cmd}$cnt ] ; then
         
         file="${cmd}$cnt"
         echo "#!/bin/bash" > $file
         echo -e \\n\\n >> $file
         chmod 755 $file
         $guieditor $file &
         quit="0"
         
      fi

   done                     
   

}


cd to our working directory

begin the execution of the while loop. while loops as long as our condition is true, which we've define is the variable quit=1, if something happens where the variable quit equals anything else, the loop stops running

cnt : note we increment it by a numerical value of 1 each time the while statement loops

if [ condition check ] : we are looking for a file that doesn't exist. the filename is fun# , which is the variable cmd plus the variable cnt

echo -e \\n\\n >> $file : adds two linefeeds

when we find a fun# file that doesn't exist, we make the new file, give it executable attributes, and open it in our 'guieditor'

all this happens by typing in one command : fun

here is our function based script put together in working form:

Code:
#!/bin/bash

main() {

    var
    makefile

}

var() {
   
    wrkdir="/root/bin"
    cmd=`basename $0`
    test "$wrkdir" = ""  && "echo variable wrkdir is empty" && exit
    test ! -d $wrkdir && "echo working directory doesn't exist" && exit
    cnt="0"
    quit="1"

}

makefile() {

    cd $wrkdir

    while [ "$quit" = "1" ] ; do
       
        cnt=`echo $[$cnt+1]`
        if [ ! -f ${cmd}$cnt ] ; then
           
            file="${cmd}$cnt"
            echo "#!/bin/bash" > $file
            echo -e \\n\\n >> $file
            chmod 755 $file
            $guieditor $file &
            quit="0"
           
        fi

    done                           
   

}

main


Please note the program flow. One call to main. Main makes one call to each function. Each function returns to main for its next command. When main has no more commands, the program quits.

fun is attached, don't copy and paste this one, you know the drill

From now on, when you want a practice script simply type fun.

~

Chapter 25 - A function based program
fun.zip
Description  Contents : script called fun
zip

 Download 
Filename  fun.zip 
Filesize  459 Bytes 
Downloaded  982 Time(s) 

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


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

PostPosted: Sat 04 Jul 2009, 19:28    Post subject: A conventionally formatted script
Subject description: Chapter 26
 

A conventionally formatted script (with explanations)

Code:
#!/bin/bash
# filename : base-name
ARGUMENTS=`echo $0 | tr "/" " "`
echo "Command name with path is: \"$0\""
for VAR in $ARGUMENTS ; do
   CMD_NAME=$VAR
done
echo "Command name without path is: \"$CMD_NAME\""


This is a short conventionally formatted bash script. The variables are in CAPS. Note how much different it is than the function based script.

It's purpose is to do what basename does, find the name of the command minus the path.

For fun we also display some information on the screen.

Line2)
    A comment saying what we name the script
Line3)
    ARGUMENTS=`echo $0 | tr "/" " "`

    we echo $0 through a pipe to tr. This is the first time you've seen tr. tr will replace one character with another. We replace / character with a space.

    the on screen output would be: root bin base-name

    the space character is used as an argument delimiter or separator. this is how we will use it and why we replaced the / with a space

    we use the backtick character to direct the output to the variable ARGUMENT
Line4)
    echo "Command name with path is: \"$0\""

    displays like this:

    Command name with path is: "/root/bin/base-name"

    We use the 'do something different character \' in this scenario to get our quotes to display
Line5 to Line7)
    for VAR in $ARGUMENTS ; do
    CMD_NAME=$VAR
    done


    For our variable VAR read each argument and do with the argument as instructed

    Instructed to put each argument value into the variable CMD_NAME

    The iterations, what happens on each pass through the loop;

    VAR=root
    VAR=bin
    VAR=base-name
    # our file name

    for has done as told, it processed all our arguments, leaving us with a VAR set to our filename. The path has been removed.

    done is part of the for loop syntax, it literally means; done
Line8)
    echo "Command name without path is: \"$CMD_NAME\""

    displays on screen:

    Command name without path is: "base-name"
Optional assignment

Look at this file for the purpose of seeing what a large script with comments looks like: /etc/rc.d/rc.shutdown

As far as the code in this chapter, we have no future use for it. This chapter's purpose is presenting and reinforcing theory. However, feel free to download the script.

~

Chapter 26 - A conventionally formatted script (with explanations)
base-name.zip
Description 
zip

 Download 
Filename  base-name.zip 
Filesize  288 Bytes 
Downloaded  967 Time(s) 

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


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

PostPosted: Sat 04 Jul 2009, 20:26    Post subject: cecho
Subject description: Chapter 27
 

cecho

Package description:

Code:
Archive:  cecho.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
     1304  07-04-09 16:50   colors.txt
     1304  07-04-09 16:47   .clr
     8356  07-04-09 16:52   tput
 --------                   -------
    10964                   3 files


Usage modification from previous instructions
    To reset colors to normal in your scripts use this command: cecho reset
Theory
    Variables easily get passed up. The variables we set in profile.local get passed up to our X terminal emulator.

    Functions don't get passed up easily.

    Cecho is a (the only) function of a file .clr

    In your scripts you could simply copy the function to your script. Also because variables get passed up, we can source .clr with a variable or source it directly.

    Our script could have this command

    Code:
    source /root/bin/.clr


    If we set a variable called clr which sources .clr, it could use this command:

    Code:
    $clr

Installation instructions
    put color.txt in your doc directory, use it for reference, it is an exact copy of .clr
    put .clr and tput in the /root/bin directory

    add these lines to /root/.bashrc and to /etc/profile.local

    export clr="source /root/bin/.clr"
    $clr

Usage examples:
    cecho red "This is a line of red text"

    cecho blue "This is a line of blue text"

    cecho green "Color green" n # green text, no newline

    cecho reset # resets font attributes to normal.

Optional Assignment
    after installing the cecho components, use the utility fun and practice with the echo and cecho commands, displaying text on the monitor, from your script commands.

~

Chapter 27 - cecho
cecho.zip
Description 
zip

 Download 
Filename  cecho.zip 
Filesize  5.09 KB 
Downloaded  989 Time(s) 

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


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

PostPosted: Sun 05 Jul 2009, 01:33    Post subject: hardinfo and lshw
Subject description: Chapter 28
 

hardinfo and lshw

I'd like to introduce you to some useful utilities in the next few chapters. In this chapter we will learn hardinfo and lshw

Instructions for hardinfo

cd /root/doc
hardinfo --generate-report --report-format html > hardinfo.html


Make it easy, copy and paste the long line to the command line. We learn through repetition, I'll repeat the copy and paste.
    * Highlight the long line, leave it highlighted

    * change to mrxvt

    * to paste; shift insert or press middle mouse button, (hit enter) then; wait until hardinfo generates its report and performs its benchmarks, it takes some time.

Instructions for lshw

If you don't have lshw, download it from this post. You know the drill to install it. Then;

cd /root/doc
lshw -html > lshw.html


Remember the bookmark you made in your favorite browser? Open the directory /root/doc and view your new reports.

~

Chapter 28 - hardinfo and lshw
lshw.zip
Description  contents : binary file lshw
zip

 Download 
Filename  lshw.zip 
Filesize  230.93 KB 
Downloaded  994 Time(s) 

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


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

PostPosted: Mon 06 Jul 2009, 05:20    Post subject:  

An unknown file type

I download a file referenced in this forum post: Slitaz' floppy boot disk boots Puppy

The file name is: floppy-grub4dos

Often when we download a floppy image, the extension is;

img : for floppy image ( sometimes hdd image )
or
imz : for bziped floppy image

There are no solid rules on the extension naming conventions with these images, just common conventions.

floppy-grub4dos
has no extension - this is perfectly fine for me and for our lesson.

Learning more about the file

ls -l floppy-grub4dos

gives me this output:

-rw-r--r-- 1 root root 1474560 2009-07-06 00:51 floppy-grub4dos


The byte size 1474560 is precisely what one would expect for an uncompressed floppy image

file floppy-grub4dos, gives;

x86 boot sector, code offset 0x2e


hexedit floppy-grub4dos; for a visual interface

The boot sector, the first 512 bytes, doesn't look like a DOS boot sector. I think the floppy image is a Linux format.

Next I want to mount it and look at it in human readable form.

In order to mount floppy-grub4dos, a character device, we set it up on a block device, /dev/loop?

losetup -f
: prints an available device

/dev/loop5, then the commands:

losetup /dev/loop5 floppy-grub4dos
mount -t ext2 /dev/loop /mnt/data


Note the file is mounted rw (read-write) we can modify it and save our changes. We cannot however increase the size.

The tree, in human readable form

|-- boot
| |-- btmgr.gz
| |-- etherboot
| |-- gpxe
| |-- memdisk
| |-- memtest
| |-- nssi.igz
| `-- plop.gz
|-- default
|-- grldr
|-- lost+found
|-- menu.lst
`-- slitaz.xpm.gz
2 directories, 11 files


umount /mnt/data
losetup -d /dev/loop5


Anthing learned?

Here is a list of the utilities you've been introduced to or reintroduced to in this chapter.

losetup
umount
mount
file

hexedit ( needs a better hex editor than what Puppy comes with )
ls

Optional assignment - You can do everything in this list.

~

Chapter 30 - an unknown file type

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


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

PostPosted: Mon 06 Jul 2009, 07:01    Post subject:  

Black holes

For the purpose of this chapter, and not being an astronomer, I'll define a black hole as something that tends to absorb light. The light being your consciousness and the black hole being what tends to leave you in the dark.

In computing, I'd call the command line itself, a black hole for many. The Windows registry for others. Also the MBR.

I hope to shed some light on the MBR (Master Boot Record) in this chapter.

I've read forum topics where the OP had a bad MBR, or thought he had a bad MBR. Then from 10 to 30 or more follow up posts trying to help the OP.

On your partitioned hard disk your MBR will be the first physical sector of the hard disk.

Introducing dd - a copy / convert command.

Save a copy of the MBR:

dd if=/dev/hda of=hda.mbr bs=512 count=1

Restore the MBR from the copy:

boot to the command line
cd to the location of hda.mbr
the commands listed below are a bit more than necessary, but we really want to do this right or not at all.

sync
dd if=/dev/hda.mbr of=hda.mbr bs=512 count=1
sync
reboot


(sync flushes pending buffered writes held in memory)

Tips:

I can't tell you if it is /dev/hda, /dev/sda or something else, not with Puppy. Puppy changes. I can (later) tell you how to tell the Linux designation for your hard drive.

It is good to have a copy of your MBR. If the copy is of /dev/hda, it is best to save a copy on another device. The reason why is; if your hda MBR really goes bad, you may not even be able to access the drive to get to your backup MBR.

So, save it on a floppy disk, USB stick or some place other than hda. But also save it on /dev/hda because in most cases hda will be accessible and you can use the backup MBR from it.

Why the MBR goes bad


Usually, it never changes. It shouldn't ever change. But when we install various operating systems, we run the risk of it changing the MBR in a way we don't want to, or a dumb install routine changing it in way we really don't want. ( discounting the very unlikely event that our own stupidity isn't the cause )

When to save a copy of the MBR

Immediately after every time we modify our partitions. Especially primary partitions.

If you end up not deleting an earlier MBR, need to restore and become confused which backup to use, use the ls command like this:

ls -l <filename>

View the date and this will help determine the date of your backup, by reading the date in ls output - it should correspond with the date you partitioned.

We could also make dated backups. But we've not been introduced to the Linux date command yet.

Optional Assignment

Don't do the example exercises, (commands in blue), at this point.

Read http://en.wikipedia.org/wiki/Master_boot_record and learn a little more about the MBR, don't try and digest everything, too hard.

~

Chapter 31 - black holes

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 2 of 8 [110 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7, 8 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » House Training » Users ( For the regulars )
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.2075s ][ Queries: 13 (0.0056s) ][ GZIP on ]