Basic Shell (Console) operation for beginners

Booting, installing, newbie
Post Reply
Message
Author
PupGeek
Posts: 353
Joined: Sun 06 Sep 2009, 11:30

#16 Post by PupGeek »

Thanks keef I completely spaced that out. :oops:

Anyways, I edited it to include that.

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

#17 Post by jpeps »

It can be helpful using something like "tree" to view what's present in the directory.

http://www.murga-linux.com/puppy/viewtopic.php?t=47657

User avatar
rjbrewer
Posts: 4405
Joined: Tue 22 Jan 2008, 21:41
Location: merriam, kansas

#18 Post by rjbrewer »

This subject belongs in the "How to" or "Users" sections;
not Beginners!!

Inspiron 700m, Pent.M 1.6Ghz, 1Gb ram.
Msi Wind U100, N270 1.6>2.0Ghz, 1.5Gb ram.
Eeepc 8g 701, 900Mhz, 1Gb ram.
Full installs

User avatar
r1tz
Posts: 162
Joined: Thu 09 Sep 2010, 05:19
Location: In #puppylinux (IRC)

#19 Post by r1tz »

This is not intended to be a complete programming introduction, it was meant to help people solve a problem they might have. As for the preceding "./" I did not want to be confusing people this early in the game ( "./" and "/" would look a bit too similar to me, at a glance, if I were a beginner). I had to be obvious because I wanted to be as concise as possible, as it was long-winded enough.
True enough :)
Honestly, I do not have a single problem with using ls in a command substitution. In fact, I have more problem with the inclusion of spaces and newlines in filenames, as, I feel, that is more against naming convention. When it comes to naming files, I like to treat spaces and newlines as illegal characters myself. Instead I use CamelNotation and dashes.
The reason not to use "ls" is because it wont have "inclusion of spaces and newlines in filenames". Also it looks more simple without the ls.
The quotes are necessary for inclusion of newlines/tabs/spaces.

So in order for the script to adapt to the filename and not the other way, it is better to have quote and not use ls.

PupGeek
Posts: 353
Joined: Sun 06 Sep 2009, 11:30

#20 Post by PupGeek »

I did not know you could leave the ls command out of the for loop and achieve the same result (I just checked that out and was surprised). Nice shortcut there, thanks.

With my Roxapp example, the script only deals with files that are essential to the program itself. Data files are handled from within the program. The script only sets up the program temporarily and cleans itself up when you are done using it.

As for quoted variables, they have always looked wrong to me... probably because of my earlier experiences, but it is nice to know that quoting a variable allows it to handle data containing spaces. I was wondering how to go about doing that. I don't like putting spaces in filenames and I know many coders don't either, but I guess I have to know how to deal with them if I'm going to code.

some of the biggest commands I have trouble with are grep, sed, and awk. I also have trouble with regular expressions (or at least, complex ones.... kinda like Algebra :) ). That's when it really starts to get confusing to me. I try to avoid those wherever I can.

@rj: I placed this in beginners because I wanted to let people know that, yes, even a beginner can learn to use the CLI. I'm sure there are beginners who are interested in this kind of stuff but may not be quite ready to leave the beginner's circle yet. This can ease them into a more advanced area.
Last edited by PupGeek on Sun 06 Mar 2011, 22:37, edited 1 time in total.

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#21 Post by Karl Godt »

For files it is important to use double quotes

but try this
while true; do ps | grep X | grep -v 'grep'; S="/bin/sleep 0.2"; "$S" ;done
and this

Code: Select all

 while true; do ps | grep X | grep -v 'grep'; S="/bin/sleep 0.2"; $S ;done
Things might work differently on "ash and bash" and "rxvt and urxvt" .

Bruce B

#22 Post by Bruce B »

Why use the command line.

In the beginning was the command line. I couldn't build or setup a
computer without using the command line. Today, maybe I could, but
I've not tried it.

The primary reason I use it so extensively today is because it is so much
faster and easier than the GUI in many cases.

In and of itself, I don't think the command line would be faster,
except for the aliases and scripts we make.

Additionally, when the computer doesn't work right, there often isn't a
GUI application to do the troubleshooting and repair. The power user
knows what command line tools are available and how to use them.

Presently, as I write this text, I'm converting .wav files to .mp3 files in
large batch. There is probably a GUI tool to do this. Even if there is, it
won't provide me with the level of control I can get with my own script.

I might have almost one terabyte of data on my computer. Because of
scripts I've written, I can find files much faster than the computer can
search itself with any GUI tool I know of.

~~

Bruce B

#23 Post by Bruce B »

When you start writing your own scripts, you are literally programming.

There are some things you need to know about, things which may seem
odd.

I wish to introduce you to one.

A Shell is an interface to the operating system. We call Bash a shell. But
the GUI is also a shell.

When X is running we run Bash in an emulator. Rxvt, Urxvt, or others.

When we run a script it does NOT run in the same shell we started it in.
It runs in a child or secondary shell and when done with the commands,
returns us to the first shell.

Here is a very simple script that won't seem to work. Please try it.

Scriptname r

Code: Select all

#!/bin/bash
cd /
After you've made it executable, run it. When finished you will still be in
the same directory you ran it in, even though the command is to cd to /

What happened is it actually did cd to / in the child shell, then when
done, left us where we started.

Next take the same script and use this command:

. r

Notice that it changed to the root directory like it was supposed to.

. causes the command to run in the current shell, that's why it works.

.
is a shortcut of sorts for source

source r should achieve the same results

When done, delete the file r, it's purpose is served
and I will show you a much better way.

~

Bruce B

Re: Part 1: Getting started

#24 Post by Bruce B »

Code: Select all

for i in *.deb
do
undeb "$i" #It is a good habit to quote variables.
shift
done
# It is a good habit to quote filenames. $i would have a filename. It is
especially a good habit if one distributes the script.

# The shift in a for loop either shouldn't be there or I don't
understand something.

A filename might contain a space. Without the quotes, it would throw the
script off, because the first space it encounters would be interpreted as a
command delimiter.

Unix people never put spaces in directory or filenames. To do so is stupid.

Windows came up with a hacked LFN and I noticed Gates was so proud
he overcome his 8.3 limits he started putting spaces in directory names.

Windows users started using spaces in directory and filenames to make
them more readable.

Then when they moved to Linux, they brought their fears and computing
habits with them.

Yet, if we look at real programmers, programmers I respect anyway, they
don't use spaces. Check out the system32 directory in Windows for
examples of how people who know what they do name files.

I don't use spaces in file names, so for my own scripts, I wouldn't quote
because I want to see the error message and fix the file.

There are certain characters I don't want in my filenames or at a
minimum I want to know if they exist. For this reason I wrote a script to
check files for characters I don't want.

Code: Select all

#/bin/bash
for i in *
do 

	echo $i | grep " "
	echo $i | grep __
	echo $i | grep \'
	echo $i | grep "?"
	echo $i | grep "("
	echo $i | grep ")"
	echo $i | grep "&"
	echo $i | grep ","
	echo $i | grep "!"

done
The script could be written for much better speed.

Some of these characters are for appearance. But some of these
characters can wreak havoc with our scripts.

Frankly, I think the programmer, especially the beginning programmer,
will discover she's spending most of her time debugging her scripts.

Some of these hard ones to figure is because Windows users make
filenames using characters bash stumbles on if not properly escaped.

~

Bruce B

#25 Post by Bruce B »

Below is a basic order of precedence in which bash finds commands

1st = alias
2nd = function
3rd = internal command
4th = external command


To see the aliases

# alias

To see the functions

# set (then look for the functions)

To see internal commands

# help

To see external commands

# tab (then answer) y

Knowing how bash finds the commands can be helpful if you want to use
a specific command with a redundant name. For example, maybe
pwd is an internal command and an external command.

The internal command will run unless you specify the external
command, which might be stated like as below in your script.

/usr/bin/pwd

If the command is not found in an alias, function, or internally; bash will
search the PATH for the command.

It will use the PATH statement from left to right.

To see the path statement

echo $PATH

~

User avatar
r1tz
Posts: 162
Joined: Thu 09 Sep 2010, 05:19
Location: In #puppylinux (IRC)

#26 Post by r1tz »

Another useful command is "mkdir -p"
mkdir= Make directory
-p = Make the parent file if it doesn't exist.

So, i want to make a directory /mnt/sda1/lupu52/backup/2010

Code: Select all

mkdir -p /mnt/sda1/lupu52/backup/2010
Instead of making a dir, enter it, make another dir, enter it.

Also, if you keep making some minor typo when doing "cd"

Code: Select all

shopt -s cdspell
This will auto correct some of your mistakes while doing "cd"
----
EDIT
Also, i dunno what the "shift" is for. It is redundant :D
I just thought that i will copy it over... :?
Last edited by r1tz on Mon 07 Mar 2011, 09:56, edited 1 time in total.

Bruce B

#27 Post by Bruce B »

smallfish wrote:This is intended to introduce beginners to the shell
and scripts so that they may begin experiencing the true power of Linux.
Who are you talking to?

You stuck this piece in the middle of some posts I made. I wonder if it
was meant for me. If not, say who you intended it for.

None are going to be programmers without some basic theory. Which is
what I've been presenting. I have plans to move to other areas. But I'm
not there yet.

I'm presuming basic computer skills, knowledge of basic acronyms and a
reading level of an eighth grader.

If you think I went over an eighth grade level, please copy and paste the
text in context. I'd like to see where I went wrong.

If someone else moved at a faster gradient than he should have, copy
and paste and I'll explain at an eighth grade level.

Or for that matter, if you have any questions, ask, people are here to
help.

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#28 Post by jamesbond »

Bruce B wrote:source r should achieve the same results

When done, delete the file r, it's purpose is served
and I will show you a much better way.

~
Bruce, would you be sharing how to do it in a better way? Image.
I find this thread useful, I didn't know the "help" command exist - I keep referring to the bash reference manual when I need to know how a certain internal command works.
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

Bruce B

#29 Post by Bruce B »

jamesbond wrote:
Bruce B wrote:source r should achieve the same results

When done, delete the file r, its purpose is served
and I will show you a much better way.
Bruce, would you be sharing how to do it in a better way?
That is exactly what I mean to do.

In a post or two, you will see.

~

Bruce B

#30 Post by Bruce B »

jamesbond wrote: I didn't know the "help" command exist
I didn't mention once you see the command, then type, according to
examples below:

# help commandname

# help pwd
#for a real example

~

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#31 Post by jamesbond »

Thanks Bruce. One can always learn new things every day here - even from the Beginner section :D
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

Bruce B

#32 Post by Bruce B »

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.

~

Bruce B

#33 Post by Bruce B »

Introducing ~/.bashrc

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

alias edrclocal='mp /etc/rc.d/rc.local'
alias edprofile='mp /etc/profile'

You get the idea, cryptic, but somewhat intuitive.

More lecture on sourcing

.bashrc is sourced when bash opens

Bash programmers sometimes get confused by not understanding what
does and doesn't get carried down, up or sideways to another emulator.

Puppy's /etc/profile sets up the working environment. It is not a shell
script and needs to run at very low level, it runs by sourcing and before
bash even comes into play.

A sourced file is simply a flat text file with commands in it. It doesn't
need to have an executable bit set. It doesn't need #!/bin/bash on the
top line.

For a practice test, make a file with this line.

echo foo bar

Then run the file like this:

. filename


~

Bruce B

#34 Post by Bruce B »

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

# 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~'

# returns to last directory
alias b='cd -'

# refreshes .bashrc
alias rf='. ~/.bashrc'

# opens .bashrc for editing, the resources it
# use text editor of YOUR choice
alias eda='mp ~/.bashrc;. ~/.bashrc'

Changes will take effect on opening the next emulator.

More to come, soon.

~

Bruce B

#35 Post by Bruce B »

I advise keeping all your scripts in their own directory. I use /root/bin

If the directory of your choice doesn't exist, make it.

To add it to the path statement open /etc/profile and find the PATH= line

Add your directory to the end like this :/root/foobar

The script presented here is called acd, it means 'add current directory'
(to .bashrc)

It is a way of bookmarking your favorite directories.

If you are in /usr , then type acd and it will add usr as a command to cd
to /usr


alias usr='cd /usr'

If you are in /usr/lib it will add lib as the command to cd to /usr/lib, you
may not want that as your command name, if not then run the
command like this

acd usrlib

alias usrlib='cd /usr/lib'

If you are in /mnt/sda2, acd will add this command to your .bashrc file

alias sda2='cd /mnt/sda2'

If you want another name, run it like this: acd a2

alias a2='cd /mnt/sda2'

Code: Select all

#!/bin/bash
[ $1 ] && name=$1
[ ! $1 ] && name=`basename $PWD`
echo alias $name="'cd $PWD'">>/root/.bashrc
</root/.bashrc grep "$name"
From here on you can build your command line 'bookmarks' with great
ease. It becomes especially usefully with long directory paths. Look what
the command doors does on my computer.

alias doors='cd /mnt/sdb1/mp3/doors'

Attached is the actual file acd.zip, if you'd rather not make it.

~
Attachments
acd.zip
(236 Bytes) Downloaded 1655 times

Post Reply