Thanks Bruce. One can always learn new things every day here - even from the Beginner section _________________Fatdog64, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread.

When this topic started, I thought I'd leave others talk about shell
scripting. I'd contribute by teaching how customize the shell with aliases
and functions.

Before making a new command of any kind, make sure the command
name doesn't exist. We are going to make a new command called mnt.
What you would want to do is type in mnt to make sure no such
command already exists.

Enter this on the command line:

# alias mnt='cd /mnt'

Then run mnt. You should now be in the /mnt directory

Then using the command separator ; run the following command:

# mnt;rox

This should open an instance of rox in the /mnt directory. It is faster
and easier than using the rox interface to get there.

Each time we open a terminal emulator, bash reads /root/.bashrc and
executes all the commands.

Presently, I'm focused on getting us started with aliases to change
directories. Once these aliases have been written into .bashrc, we never
have to enter them again. They become a part of our shell commands,
seamlessly.

I want everything easy. For this reason I use very cryptic commands. I
also try and keep things a bit consistent. Commands to edit files start
with ed

Open ~/.bashrc with a text editor and add the following commands. You
don't need the comments, so delete them as wanted. Make sure to always
leave on linefeed at the end of your file after manually editing.

Code:

# shows full path at prompt
export PS1='[\w] '

# shows current directory
#export PS1='[\W] '

# moves back one directory
alias ..='cd ..'

# moves back two directories
alias ...='cd ../..'

# moves back three directories
alias ....='cd ../../..'

# to return to a saved directory, a script is needed
# but not yet included in this post
alias u='. /tmp/uu~'

tells Linux it is a shell script executable
/bin/bash tells which interpreter I want to use, also its location

[ $1 ] && name=$1

[ is a command called test
a good unix user would not put a command in a filename
it is little things like what Windows users do that can
cause us problems with our scripts, unless we know [ is
an actual command

$1 is a command line argument or parameter.
$1 would be the first command
$2 would be the second command

] is not a command. It is syntax used to say "end of test"

[ $1 ] is simply a way of 'testing' to see of the user entered a command
line argument

&& is a conditional operator. If the condition is true, it executes the
command on the right, if false nothing happens

name is a variable. The = sign is the end of the variable. Anything to the
right of the = sign gets moved into the variable

If I entered the text foobar on the command line, after the executable
name, then $1 would be true and the variable name would contain the
text foobar

[ ! $1 ] && name=`basename $PWD`

Either I entered text on the command line or I didn't.

! is an operator which means not

[ ! $1 ] tests to see if not $1 - in English it asks if I did not enter a
parameter.

If I didn't then a true condition exists and the operator && will execute
the command on its right.

name=`basename $PWD`

name is as already explained a variable, it can contain a variety of data,
the data in it is not static.

$PWD is an environment variable. It changes its contents to match our
current directory.

If we were in /usr/lib/firefox then $PWD would contain the data /usr/lib
/firefox

If we typed basename $PWD on the command line it would echo firefox

In this usage, basename strips out the parent directories

We want to move, shove the output of basename $PWD into our variable,
this is where the backticks come in. We need them in order to force the
output from right to left into the variable.

echo alias $name="'cd $PWD'">>/root/.bashrc

echo means to repeat and is a very common command

our goal here is to stuff the following command at the tail of .bashrc
alias firefox='cd /usr/lib/firefox'

name and PWD are both initialized variables

We don't want the variables, we want the data they contain. To
accomplish this we add the $ to the front of the variable.

echo $PWD would print /usr/lib/firefox
echo $name would print firefox

I need the single quotes in the command I want to write to .bashrc

The single quote also has a meaning and prevents the expansion I need,
but using the double-quotes allows it.

Let's look at the command again:

echo alias $name="'cd $PWD'">>/root/.bashrc

I do not want the command printed to the screen, also known as standard
output, I want it printed to the file /root/.bashrc

The >> redirects the output to where I send it, which is /root/.bashrc

The >> appends the file

A > would delete all existing data in .bashrc and leave me only with the
command.

>, >> and < are called redirection symbols, they are instructions to
redirect output

</root/.bashrc grep "$name"

The < redirects the entire /root/.bashrc file to grep

grep is a search utility, as the data is being redirected through grep, it
searches for the contents of $name which is firefox, in our example.

All lines containing 'firefox' will be displayed to standard output, our
monitor.

Its purpose is merely to verify that the command acd did what we wanted.

When you run a script, your .bashrc aliases and functions are not
available in the script. This is usually, if not always, to your advantage.
You don't want some alias you forgot about interfering with your script.

Here is an example:
alias rm='rm -i'

This alias means that you will be asked to verify each deletion.

You may not want that interaction in your script, considering the aliases
don't exist, you use the rm command as you want without interference.
Your script will behave as written.

Some distributions add the .bashrc commands below by default,
considering how hard it is to undo mistakes, I recommend the aliases
below.

BTW - we are done with the change directory instructions.

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

This will cause an interactive command where you have to confirm the
actions.

Suppose you know you want to remove every file in a directory and don't
want to confirm everyone, you can escape the alias with a backslash
\rm *

Here is another example of aliases I use
alias xms='xmms *.mp3 &'

Remember 'doors' does cd /mnt/sda2/mp3/doors

As fast as I can type doors;xms, the music I want to play starts playing.
This is more convenient than the mouse movements required to open
xmms and do the same thing.

Also, I could have an alias like this:
alias playdoors='xmms /mnt/sda2/mp3/doors/*.mp3 &'

We write a program in order to do something for us. In most cases I write
programs because I want to solve problems. The problem might be how
to make something complex - simple.

We find in writing good programs we want to implement conditional check
and branches. Otherwise we can write a destructive program.

It is very important to know exactly what you want your program to do,
before writing it.

Think about it for a while and write notes before you program.

I'll make a demo program to show you how to make conditional checks
and branches.

Progname: bkuproot
Purpose: to make a backup of /root files on a separate partition
I need to:
make sure the partition is mounted
that there is sufficient space on the partition
I'll use the zip utility for my backup
I'll skip the space check to make my example easier
zip will bomb out with an on screen error if there wasn't enough space

My primary purpose here is actually to demonstrate how to make a
conditional check and branch, this should be easy. I'm ready to code.

Code:

#!/bin/bash

DESTPART=/dev/sda2
DESTDIR=rootbkup
FILENAME=bkuproot.zip

Please note: by using variables, we make our script easily portable, and
we can make them easy to modify. We merely change the data in the
variable. We don't have to search the entire script.

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