The time now is Fri 06 Dec 2019, 21:22
All times are UTC - 4 |
Author |
Message |
PupGeek
Joined: 06 Sep 2009 Posts: 388
|
Posted: Sun 06 Mar 2011, 14:26 Post subject:
|
|
Thanks keef I completely spaced that out.
Anyways, I edited it to include that.
|
Back to top
|
|
 |
jpeps
Joined: 31 May 2008 Posts: 3217
|
Posted: Sun 06 Mar 2011, 15:24 Post subject:
|
|
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
|
Back to top
|
|
 |
rjbrewer

Joined: 22 Jan 2008 Posts: 4421 Location: merriam, kansas
|
Posted: Sun 06 Mar 2011, 15:44 Post subject:
|
|
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
|
Back to top
|
|
 |
r1tz

Joined: 09 Sep 2010 Posts: 165 Location: In #puppylinux (IRC)
|
Posted: Sun 06 Mar 2011, 16:13 Post subject:
|
|
Quote: | 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
Quote: | 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.
|
Back to top
|
|
 |
PupGeek
Joined: 06 Sep 2009 Posts: 388
|
Posted: Sun 06 Mar 2011, 18:21 Post subject:
|
|
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, 18:37; edited 1 time in total
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 4208 Location: Kiel,Germany
|
Posted: Sun 06 Mar 2011, 18:34 Post subject:
|
|
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: | 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" .
|
Back to top
|
|
 |
Bruce B
Joined: 18 May 2005 Posts: 11488 Location: The Peoples Republic of California
|
Posted: Mon 07 Mar 2011, 02:52 Post subject:
|
|
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.
~~
_________________ New! Puppy Linux Links Page
|
Back to top
|
|
 |
Bruce B
Joined: 18 May 2005 Posts: 11488 Location: The Peoples Republic of California
|
Posted: Mon 07 Mar 2011, 03:17 Post subject:
|
|
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
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.
~
_________________ New! Puppy Linux Links Page
|
Back to top
|
|
 |
Bruce B
Joined: 18 May 2005 Posts: 11488 Location: The Peoples Republic of California
|
Posted: Mon 07 Mar 2011, 03:54 Post subject:
Re: Part 1: Getting started |
|
Quote: | Code: |
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: | #/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.
~
_________________ New! Puppy Linux Links Page
|
Back to top
|
|
 |
Bruce B
Joined: 18 May 2005 Posts: 11488 Location: The Peoples Republic of California
|
Posted: Mon 07 Mar 2011, 05:21 Post subject:
|
|
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
~
_________________ New! Puppy Linux Links Page
|
Back to top
|
|
 |
r1tz

Joined: 09 Sep 2010 Posts: 165 Location: In #puppylinux (IRC)
|
Posted: Mon 07 Mar 2011, 05:50 Post subject:
|
|
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: | 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"
This will auto correct some of your mistakes while doing "cd"
----
EDIT
Also, i dunno what the "shift" is for. It is redundant
I just thought that i will copy it over...
Last edited by r1tz on Mon 07 Mar 2011, 05:56; edited 1 time in total
|
Back to top
|
|
 |
Bruce B
Joined: 18 May 2005 Posts: 11488 Location: The Peoples Republic of California
|
Posted: Mon 07 Mar 2011, 05:54 Post subject:
|
|
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.
_________________ New! Puppy Linux Links Page
|
Back to top
|
|
 |
jamesbond
Joined: 26 Feb 2007 Posts: 3384 Location: The Blue Marble
|
Posted: Mon 07 Mar 2011, 06:42 Post subject:
|
|
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? .
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: Latest version | Contributed packages | ISO builder
|
Back to top
|
|
 |
Bruce B
Joined: 18 May 2005 Posts: 11488 Location: The Peoples Republic of California
|
Posted: Mon 07 Mar 2011, 07:01 Post subject:
|
|
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.
~
_________________ New! Puppy Linux Links Page
|
Back to top
|
|
 |
Bruce B
Joined: 18 May 2005 Posts: 11488 Location: The Peoples Republic of California
|
Posted: Mon 07 Mar 2011, 07:07 Post subject:
|
|
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
~
_________________ New! Puppy Linux Links Page
|
Back to top
|
|
 |
|
|
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
|