views:

54

answers:

3

I am, like many non-engineers or non-mathematicians who try writing algorithms, an intuitive. My exact psychological typology makes it quite difficult for me to learn anything serious like computers or math. Generally, I prefer audio, because I can engage my imagination more effectively in the learning process.

That said, I am trying to write a shell script that will help me master Linux. To that end, I copied and pasted a list of Linux commands from the O'Reilly website's index to the book Python In a Nutshell. I doubt they'll mind, and I thank them for providing it. These are the textfile `massivelistoflinuxcommands,' not included fully below in order to save space...

OK, now comes the fun part. How do I get this script to work?

#/bin/sh
read -d 'massivelistoflinuxcommands' commands <<EOF

accept
bison
bzcmp
bzdiff
bzgrep
bzip2
bzless
bzmore
c++
lastb
lastlog
strace
strfile 
zmore
znew


EOF

for i in $commands
do

$i --help | less | cat > masterlinuxnow

text2wave masterlinuxnow -o ml.wav

done
+1  A: 

The first problem here is that not every command has a --help option!! In fact the very first command, accept, has no such option! A better approach might be executing man on each command since a manual page is more likely to exist for each of the commands. Thus change;

$i --help | less | cat > masterlinuxnow

to

man $i >> masterlinuxnow

note that it is essential you use the append output operator ">>" instead of the create output operator ">" in this loop. Using the create output operator will recreate the file "masterlinuxnow" on each iteration thus containing only the output of the last "man $i" processed.

you also need to worry about whether the command exists on your version of linux (many commands are not included in the standard distribution or may have different names). Thus you probably want something more like this where the -n in the head command should be replace by the number of lines you want, so if you want only the first 2 lines of the --help output you would replace -n with -2:

if [ $(which $i) ]
then
$i --help | head -n >> masterlinuxnow
fi

and instead of the read command, simply define the variable commands like so:

commands="
bison
bzcmp
bzdiff
bzgrep
bzip2
bzless
bzmore
c++
lastb
lastlog
strace
strfile 
zmore
znew
"

Putting this all together, the following script works quite nicely:

commands="
bison
bzcmp
bzdiff
bzgrep
bzip2
bzless
bzmore
c++
lastb
lastlog
strace
strfile 
zmore
znew
"

for i in $commands
do

if [ $(which $i) ]
then
$i --help | head -1  >> masterlinuxnow 2>/dev/null
fi
done
ennuikiller
If there's no --help, i'm not interested in it at this stage, so the script can just skip it. man would be too lengthy for an audio format. Thanks though.
old Ixfoxleigh
so additional comments in my answer.
ennuikiller
That's very helpful. Thanks.
old Ixfoxleigh
I liked your answer just as much as Dennis's, but I chose his because it clarified the way to use the format I was originally trying to use, and the reason why it didn't work. But actually your clarification of how to use cat is also quite useful to me. I have been using cat with a single chevron for far too long.
old Ixfoxleigh
A: 

You're going to learn to use Linux by listening to help descriptions? I really think that's a bad idea.

Those help commands usually list every obscure option to a command, including many that you will never use-- especially as a beginner.

A guided tutorial or book would be much better. It would only present the commands and options that will be most useful. For example, that list of commands you gave has many that I don't know-- and I've been using Linux/Unix extensively for 10 years.

whooops
I've been using Linux for 10 years too. Have you mastered it yet? Not me.
old Ixfoxleigh
As for what you say about tutorials, have you ever considered that maybe some people don't learn well with them?For more info about 'why I hate tutorials,' please see my other question: http://stackoverflow.com/questions/3428245/elegant-pythonBasically, whenever reading a tutorial, I just feel profoundly confused, and I also tried to point out my learning difficulties in the intro to this question. So I gave you a down-rating, because I felt your answer was quite dismissive. Try not referring to something the OP is genuinely interested in as 'those help commands.' I happen to love them.
old Ixfoxleigh
No, I haven't mastered it; I'm still learning just like everyone else. My expression 'Those help commands' was not meant pejoratively at all, and I see no reason for you to be so defensive. I use 'those help commands' all the time when I need more in depth information about a particular command or its options. However, anyone that has ever glanced at them before knows that they aren't meant to be read cover-to-cover along with 1000 other commands (and the authors would not disagree). I can hardly imagine text2wave reading "dash dash vee dash dash version version information". Is that useful?
whooops
If you just posted this question as needing help with a shell script, I'm sure you would get useful answers. But if you write a paragraph about learning strategies, then you are inviting people to comment on them. I was merely trying to give useful advice.
whooops
+3  A: 

It really helps when you include error messages or specific ways that something deviates from expected behavior.

However, your problem is here:

read -d 'massivelistoflinuxcommands' commands <<EOF

It should be:

read -d '' commands <<EOF

The delimiter to read causes it to stop at the first character it finds that matches the first character in the string, so it stops at "bzc" because the next character is "m" which matches the "m" at the beginning of "massive..."

Also, I have no idea what this is supposed to do:

$i --help | less | cat > masterlinuxnow

but it probably should be:

$i --help > masterlinuxnow

However, you should be able to pipe directly into text2wave and skip creating an intermediate file:

$i --help | text2wave -o ml.wav

Also, you may want to prevent each file from overwriting the previous one:

$i --help | text2wave -o ml-$i.wav

That will create files named like "ml-accept.wav" and "ml-bison.wav".

I would point out that if you're learning Linux commands, you should prioritize them by frequency of use and/or applicability to a beginner. For example, you probably won't be using bison right away`.

Dennis Williamson
This is helpful too. I'm not a beginner, actually. I've just never made a single-minded attempt at knowing what's in my /usr/bin directory. I like the fact that you've made individual wavs out of each command, actually -- I didn't think of that, but that gives me the option of refining my 'interested' list by just deleting every MP3'd command I don't want. Thank you.
old Ixfoxleigh