The Text-User Interface

Using applications, configuring, problems
Message
Author
Bruce B

#76 Post by Bruce B »

78 - select menu

I show a simple way to make a useful menu with bash. When we did the look utility, I used this bash' select command. I didn't however explain it at the time, thinking I'll do it later. Now is later.

In chapter 77 we installed 'links' and set the variable PAGER.

By now, I hope you have some documentation in our 'doc' directory /root/doc, because this script is for perusing the /root/doc directory.

I called this script doks, but as you know you can give it another name.

Code: Select all

#!/bin/bash
main() {

    vars
    menu

}

vars() {

    # $clr
    dir=/root/doc
    [ ! -d $dir ] && echo "$dir doesn't exist, exiting" && exit
    PS3='Please select : '

    # branch example, use different mode for the pager if X is running

    xrun=`pidof X`
    [ x$xrun != x ] && graphics="-g"

}

menu() {

    cd $dir
    clear ; echo -e \\n\\n\\n\\n\\n
    echo "Scripting And Linux Documentation" ; echo
    select fname in * ;
    do
           $PAGER $graphics ${dir}/${fname}
          break;
    done
    exec $0

}

main

# 2009-08-02
With this utility you can type in doks and easily read the files in this directory with your new pager. (if they are the right kind of files)

What I'd like for an assignment is to have you modify this file some, give it colors, change the color of PS3. Change and color the title, so on and so forth.

You can setup these simple little menu systems throughout your system as needs be. For example, I have scripts to run wine supported files. I type in 'winem', short for 'wine menu' and then select the number of the application I want to run.

The wine scripts have their own place in the PATH so I can run them directly also with command line arguments. And while on the subject, you can add lots of paths to grow your system and keep it organized.

A little change of subject, but while I'm thinking about it, when we installed mrxvt it didn't come with a complete rc file. I made a complete file, not all the values are entered, nor should they be, but the options are. With the rc file, it's easy to change the way mrxvt looks and behaves. And of course see the options available which the other file didn't show.

I'll attach it as mrxvtrc and you can download it to /root and use

swapf mrxvtrc .mrxvtrc

Would you like a utility for swapping directories? If so, you can use swapf as the template. Copy it to the name swapd and change it internally, accordingly.

~

78 - select menu
Attachments
mrxvtrc.zip
(1.8 KiB) Downloaded 2043 times

Bruce B

#77 Post by Bruce B »

Chapter 79 - Line endings

This chapter is important to understand, but I don't know under what circumstances you will need to apply the information.

Unix, MS-DOS, and Mac all use different line endings. Because of this we can end up with a Mac text file, a DOS text file or a Unix text file.

Also we can end up with a hodge podge of line endings if we've been editing with DOS and Linux editors, which don't convert all unchanged line endings on save.

Puppy and most Linux come with utilities called unix2dos and dos2unix, when you port text based documents from one operating systems, it's good to make your line endings consistent.

In order to help you better conceptualize I have hexdump for the three file types

Unix / Linux File - Line Feed (LF) (0x0a)

Code: Select all

74 68 69 73 20 69 73 20  61 20 75 6e 69 78 20 66  |this is a unix f|
69 6c 65 0a 69 74 20 75  73 65 73 20 6c 69 6e 65  |ile.it uses line|
20 66 65 65 64 73 0a 66  6f 72 20 6c 69 6e 65 20  | feeds.for line |
65 6e 64 69 6e 67 73 0a  6e 6f 74 65 20 74 68 65  |endings.note the|
20 30 61 0a 0a                                    | 0a..|
Messy DOS File - (Carriage Return + Line Feed) (CR+LF) (0x0d 0x0a)

7

Code: Select all

4 68 69 73 20 69 73 20  61 20 6d 65 73 73 79 20  |this is a messy |
64 6f 73 0d 0a 66 69 6c  65 2c 20 69 74 20 75 73  |dos..file, it us|
65 73 20 63 61 72 72 69  61 67 65 0d 0a 72 65 74  |es carriage..ret|
75 72 6e 73 20 61 6e 64  20 6c 69 6e 65 20 66 65  |urns and line fe|
65 64 73 0d 0a 66 6f 72  20 6c 69 6e 65 20 65 6e  |eds..for line en|
64 69 6e 67 73 2c 20 6e  6f 74 65 0d 0a 74 68 65  |dings, note..the|
20 30 64 20 61 6e 64 20  30 61 0d 0a              | 0d and 0a..|
Mac File - Carriage Return - 0x0d

Code: Select all

74 68 69 73 20 69 73 20  61 20 73 61 6d 70 6c 65  |this is a sample|
0d 6f 66 20 61 20 4d 61  63 20 66 69 6c 65 0d 6e  |.of a Mac file.n|
6f 74 65 20 74 68 65 20  30 64 2c 20 74 68 65 73  |ote the 0d, thes|
65 0d 61 72 65 20 63 61  72 72 69 61 67 65 20 72  |e.are carriage r|
65 74 72 75 6e 73 0d                              |etruns.|
I'm including a utility called 'tofrodos' with this post. One binary file and one text help file

You can also download the source and compile it yourself from:

http://www.thefreecountry.com/tofrodos/index.shtml

Puppy's equivalent utilities don't have the features tofrodos has, so that's why I'm adding it.

I have an idea it will convert Mac files, even if the help doesn't say so, or prepare them for conversion, with a little help from simple script. But I need to test this first.

The main things to remember from this chapter, I've highlighted in bold.

~

Chapter 79 - Line endings
Attachments
tofrodos.zip
(6.84 KiB) Downloaded 1982 times

Bruce B

#78 Post by Bruce B »

Chapter 80 - Clean File Improved

I removed notrail from Chapter 50 for a better utility posted in this chapter.

The contents of notrail are: one shell script called notrail, you will want to replace it with cleanf ( means clean file )

The main change is: I wrote a C program pipe which does more with the characters than the sed command.

Zip file contents are:

cleanf - the script to run
cleanf-bin - the script automatically runs this file
cleanf-bin.c - the source code distributed GNU version 2

That's about it for this chapter, enjoy!

~

Chapter 80 - Clean File Improved
Attachments
cleanf.zip
(2.27 KiB) Downloaded 1969 times

Bruce B

#79 Post by Bruce B »

Chapter 81 - How to do some modest binary hacking

Rule #1 is make a backup
Rule #2 is make a backup

The hex editor is the right tool for the job, unless you insert or delete bytes the byte size of the file doesn't change. The hex editor displays all content.

I compiled gqview and found out it wanted its help file from a subdirectory of my temporary build directory.

I edited the directory string in the file to point to the right place. It only took a minute and beat debugging the source code.

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

More recently, as in today, I was using a really neat PIM (Personal Information Manager)

It has (had) 1 pane for a todo list, 1 pane for the calendar and one pane for the appointment list.

Problem is I don't have many appointments. But I do have goals and the way the PIM is organized goals work perfectly in place of appointments. So I hacked it replacing this string
Appointments
with this
Goal Planner

These characters are only for display purposes and the number of characters I replaced was identical, so the file size didn't change, which you really likely don't want to do.

I've attached a little picture of the changes.

The idea I wish to convey in this chapter is don't be afraid to change some human readable text. ( you followed rule number #1 and #2 )

Generally, when you are inside the file, you can intuit if the text you plan to change is okay to change.

~

Chapter 81 - How to do some modest binary hacking
Attachments
hex-changes.png
(5.22 KiB) Downloaded 2786 times

Bruce B

#80 Post by Bruce B »

Chapter 82 - File extensions and file names

Sometimes we need to add and remove file name information. I'll show an example. Suppose we wanted to decode .mp3 to .wav files with lame.

Lame's syntax is like this lame -decode <infile> <outfile>

For 16 songs that would be a lot of typing. If we want to do a good job we want the outfile with a .wav extension and not a .mp3.wav extension.

Suppose our files are named like this Track_01.mp3, Track_02.mp3, . . .

We have an advantage from the start because we know the infile extension.

Puppy comes with an external utility called basename. Basename will strip the directory name from our file, if the directory name exists, but it won't strip the extension unless we tell it what it is.

Back to the old for loop.

Code: Select all

for i in *mp3 ; do
    fn=`basename .mp3`
    lame -decode ${fn}.mp3  ${fn}.wav
done
When the loop is done, we have 16 .mp3 and 16 .wav files and it didn't take typing. Note this would be a reusable script.

It wouldn't handle song names with spaces, but as the administrator of your Linux system, I think you are better off not introducing file or directory names with spaces.

The idea in this chapter is showing the utility basename and how to remove and change extensions. If you 'got' that much you got it. There are other ways to capture extensions and remove them. I'll save that for later.

~

Chapter 82 - File extensions and file names

Bruce B

#81 Post by Bruce B »

Chapter 83 - A little more about variables

Global Variables - These variables are set for system wide use. Some of the variables we set and exported in profile.local are global variables.

You want to try and avoid giving your script the same names as the global variables unless that's what you want.

PATH is a particularly important global variable. One time on the forum the coder used PATH=with:his:values and the rest of the script didn't run. The script has precedence.

Most of the variables I've set, in this series, I've used quotes. Actually, you don't need to quote your variables if there aren't spaces.

EDITOR=mp (doesn't need quotes)
EDITOR="mp" (doesn't hurt or change the end result)
EDITOR='mp' (also doesn't hurt or change the end result)

However sometimes quotes are the last thing you should use in a variable. As you have learned, many characters have special or double meanings in bash and utilities.

If you want your variable interpreted literally, use ' ' and not " "

In order to demonstrate the concept, try this exercise:

echo $PATH
echo "$PATH"
echo '$PATH'

echo Puppy Linux
echo " Puppy Linux "
echo ' "Puppy Linux" '

Between these two examples, you will get an idea of what I mean by a literal interpretation when you use ' ' instead of " "

~

Chapter 83 - A little more about variables

Bruce B

#82 Post by Bruce B »

Chapter 84 isolating file types

It is very common convention in Unix that our binary files and our shell scripts are put together in the /bin and /sbin directories.

Suppose we want to have a directory with just scripts, maybe for the purpose of studying the scripts on the system. To manually go through the directories, prune the scripts from binaries, and copy out the scripts would be a lot of work.

We could find out if the file is a script file with the file command:

file scriptname ; the output would be;
scriptname: Bourne shell script text executable

Another way would be standard out to grep
<scriptname grep '#!/bin/'; outputs;
#!/bin/bash

If grep returns with zero, I added the ' ' as reinforcement of making a literal interpretation. Try it with " " and you get bad results.

We don't want to take a lot of time making a script, which we don't plan to use a lot or distribute.

We make a directory called /root/scripts

Code: Select all

#!/bin/bash
for i in * ; do
	if [ ! -d $i ] ; then 
		file $i | grep "shell script" >/dev/null
		if [ x$? = x0 ] ; then
			cp $i -i /root/scripts
		fi
	fi
done
It's about as simple at that.

cd to your bin directories and run the command. When done, copies of most of the system's scripts will be in /root/scripts (except for those not in /bin directories)

Frankly, I'm not necessarily recommending this exercise, except for;

1) showing that sometimes very daunting tasks can be very simple with a few lines in bash

2) and simply by looking at scripts others have written, YOU WILL LEARN things. (should you actually make and run the script)

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

Note: some time you might encounter someone claiming the use of #!/bin/sh is better than #!/bin/bash. Maybe, and they aren't particularly wrong if they say this.

I use #!/bin/bash exclusively because in Puppy 4.00 there is not sh. /bin/sh is a symlink to bash. So why not be direct?

Chapter 84 isolating file types

Bruce B

#83 Post by Bruce B »

Chapter 85 - string comparisons

We frequently see constructs like this:

[ "$myvar" = "words" ]
or as a shortcut
[ x$myvar = x$words ]

We are comparing string1 with string2 to see if they are equal

The quotes or the x don't actually exist in either string. We use them as place holders, to make sure there is something to compare with. We can't compare something to nothing and bash will give us an error.

By using these placeholders, there is always something to compare to.

" " is prettier
x is easier

Either work fine.

Pretty simple, but I don't think I mentioned the reasons why earlier.

Chapter 85 - string comparisons

cherrysteel
Posts: 1
Joined: Wed 05 Aug 2009, 12:21

THANK YOU SO MUCH

#84 Post by cherrysteel »

:D after many hours of attempting to learn puppylinux ( my first linux distro)

I can finally get a foot hold of the command line.

I began to doubt my sanity as i followed other linux tutorials and got NOTHING but "command not found"

YOUR contributions are Priceless and your generosity inspiring!

Bruce B

#85 Post by Bruce B »

Chapter 86 LS_COLORS

The question:
  • In mrxvt, when I use a ls command, the resulting list of directories are in different colours. I would like to know what each colour denotes." - Dave
The technical reason for the colors are probably, (depending on your Puppy), it;

1) sets alias ls='ls --color=auto' in /etc/profile
2) replaced busybox ls with the real ls

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


Below are some specifics from dircolors. (attached to this post)

We can control the colors and what is denoted by them.

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

# Configuration file for dircolors, a utility to help you set the
# LS_COLORS environment variable used by GNU ls with the --color option.
# Copyright (C) 1996, 1999-2008
# Free Software Foundation, Inc.
# Copying and distribution of this file, with or without modification,
# are permitted provided the copyright notice and this notice are preserved.
# The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
# slackware version of dircolors) are recognized but ignored.
# Below, there should be one TERM entry for each termtype that is colorizable
TERM Eterm
TERM ansi
TERM color-xterm
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM con80x30
TERM con80x43
TERM con80x50
TERM con80x60
TERM cons25
TERM console
TERM cygwin
TERM dtterm
TERM eterm-color
TERM gnome
TERM gnome-256color
TERM jfbterm
TERM konsole
TERM kterm
TERM linux
TERM linux-c
TERM mach-color
TERM mlterm
TERM putty
TERM rxvt
TERM rxvt-cygwin
TERM rxvt-cygwin-native
TERM rxvt-unicode
TERM screen
TERM screen-256color
TERM screen-bce
TERM screen-w
TERM screen.linux
TERM vt100
TERM xterm
TERM xterm-16color
TERM xterm-256color
TERM xterm-88color
TERM xterm-color
TERM xterm-debian
# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
#NORMAL 00 # no color code at all
#FILE 00 # regular file: use no color at all
RESET 0 # reset to "normal" color
DIR 01;34 # directory
LINK 01;36 # symbolic link. (If you set this to 'target' instead of a
# numerical value, the color is as for the file pointed to.)
HARDLINK 44;37 # regular file with more than one link
FIFO 40;33 # pipe
SOCK 01;35 # socket
DOOR 01;35 # door
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
ORPHAN 40;31;01 # symlink to nonexistent file, or non-stat'able file
SETUID 37;41 # file that is setuid (u+s)
SETGID 30;43 # file that is setgid (g+s)
CAPABILITY 30;41 # file with capability
STICKY_OTHER_WRITABLE 30;42 # dir that is sticky and other-writable (+t,o+w)
OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable
# This is for files with execute permission:
EXEC 01;32
# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')
# If you use DOS-style suffixes, you may want to uncomment the following:
#.cmd 01;32 # executables (bright green)
#.exe 01;32
#.com 01;32
#.btm 01;32
#.bat 01;32
# Or if you want to colorize scripts even if they do not have the
# executable bit actually set.
#.sh 01;32
#.csh 01;32
# archives or compressed (bright red)
.tar 01;31
.tgz 01;31
.arj 01;31
.taz 01;31
.lzh 01;31
.lzma 01;31
.zip 01;31
.z 01;31
.Z 01;31
.dz 01;31
.gz 01;31
.bz2 01;31
.bz 01;31
.tbz2 01;31
.tz 01;31
.deb 01;31
.rpm 01;31
.jar 01;31
.rar 01;31
.ace 01;31
.zoo 01;31
.cpio 01;31
.7z 01;31
.rz 01;31
# image formats
.jpg 01;35
.jpeg 01;35
.gif 01;35
.bmp 01;35
.pbm 01;35
.pgm 01;35
.ppm 01;35
.tga 01;35
.xbm 01;35
.xpm 01;35
.tif 01;35
.tiff 01;35
.png 01;35
.svg 01;35
.svgz 01;35
.mng 01;35
.pcx 01;35
.mov 01;35
.mpg 01;35
.mpeg 01;35
.m2v 01;35
.mkv 01;35
.ogm 01;35
.mp4 01;35
.m4v 01;35
.mp4v 01;35
.vob 01;35
.qt 01;35
.nuv 01;35
.wmv 01;35
.asf 01;35
.rm 01;35
.rmvb 01;35
.flc 01;35
.avi 01;35
.fli 01;35
.flv 01;35
.gl 01;35
.dl 01;35
.xcf 01;35
.xwd 01;35
.yuv 01;35
# http://wiki.xiph.org/index.php/MIME_ (cut)
.axv 01;35
.anx 01;35
.ogv 01;35
.ogx 01;35
# audio formats
.aac 00;36
.au 00;36
.flac 00;36
.mid 00;36
.midi 00;36
.mka 00;36
.mp3 00;36
.mpc 00;36
.ogg 00;36
.ra 00;36
.wav 00;36
# http://wiki.xiph.org/index.php/ (cut)
.axa 00;36
.oga 00;36
.spx 00;36
.xspf 00;36

~

Chapter 86 LS_COLORS

(continued in chapter 87)


PS thank you cherrysteel
Attachments
dircolors.zip
(17.73 KiB) Downloaded 1974 times

Bruce B

#86 Post by Bruce B »

Chapter 87 using dircolors

So, now we've installed dircolors and dircolors.txt. What next?

Read the man page and/or type dircolors --help

I usually start off with the --help and if it is too cryptic or I need more, then the man page. Utilities and programs we use need to be learned. (at least the basics do)

dircolors --help gives us this;

Code: Select all

Usage: dircolors [OPTION]... [FILE]
Output commands to set the LS_COLORS environment variable.

Determine format of output:
  -b, --sh, --bourne-shell    output Bourne shell code to set LS_COLORS
  -c, --csh, --c-shell        output C shell code to set LS_COLORS
  -p, --print-database        output defaults
      --help     display this help and exit
      --version  output version information and exit

If FILE is specified, read it to determine which colors to use for which
file types and extensions.  Otherwise, a precompiled database is used.
For details on the format of these files, run `dircolors --print-database'.

Report dircolors bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report dircolors translation bugs to <http://translationproject.org/team/>
Warning if we redirect like this: > we clobber the destination file. If we redirect like this >> we append it.

I recommend this command.

cp /etc/profile.local /etc/profile.local.bak

The idea being, it is not fun clobbering or losing our configuration files. It is less painful to backup your configuration files before editing in the event of mistakes.

dircolors - the defaults

dircolors -b to view what it does

dircolors -b >>/etc/profile.local - to add the LS_COLORS variable and export command to /etc/profile.local

LS_COLORS will be about 1186 characters long, do NOT split the long line.

dircolors customizing or studying

dircolors -p > dircolors.db
edit dircolors.db and change things as you want. when done;
dircolors dircolors.db >> /etc/profile.local

(name dircolors.db not arbitrary, use any name you wish)

Of course if you've put previous LS_COLORS in /etc/profile.local, you'll want to edit the file and remove earlier entries.

~

Chapter 87 using dircolors

Bruce B

#87 Post by Bruce B »

Chapter 88 - my pup_save file filled up

I had about 95 MB free and then it was 0 MB free. Thinking back, I couldn't remember when I downloaded something that big. So, it was time to troubleshoot and locate where the free space went. After discovering where the lost space was, I can back track and explain.

The behavior of mount points

In Linux we can mount filesystems on just about any mount point, (directory). The mount point does not need to be empty to use it as a mount point for another filesystem.

/mnt/hda1 is merely a directory inside my pup_save file. That's all it is - a directory. But I use it as a mount point for /dev/hda1.

If /mnt/hda1 is not mounted, I could view any files inside it. But if I mount it, the files would no longer be shown. The viewable files would be the files in the partition /dev/hda1

What would happen if I copied, moved or downloaded files to /mnt/hda1 ?
  • If it is not mounted they would be placed inside /mnt/hda1, which is inside the pup_save file, thus increasing the used space in the pup_save file.

    But if /mnt/hda1 were mounted, the files would be placed inside /dev/hda1 and would not increase the pup_save used space.
I guess you can figure out what I did to fill up pup_save.

Introducing the utility du

du has many switches, modes of behavior. I made this script called disku (the u meaning usage)

Code: Select all

#!/bin/bash
du --max-depth=1 --one-file-system | sort -n
The --one-file-system says don't operate on other file systems. --max-depth=1 means just show me the first level directories.

After running the script I noted /mnt was bigger than it should be.

The file sort also has many options, the -n switch arranges du output sizes in ascending order.

-----------

Summary: In this troubleshooting scenerio, I didn't even have a script to do the job. However, I knew the external tools existed and what they did.

How could I have made this simple script without knowing about the tools available? Not very easily. The 'secret to success' in this case is in knowing the utilities available to us.

In the course of these chapters, I'll try and make sure you've been introduced to virtually all the common utilities.

~

Chapter 88 - my pup_save file filled up

Bruce B

#88 Post by Bruce B »

Chapter 89 - How to make a RAM drive

First, I'd like to discuss reasons why you might want a RAM drive based mainly I suppose on reasons I've wanted RAM drives.

1) With MS-DOS, I used RAM drives for speeding up execution of batch files, holding some commonly used utilities and as a temp directory.

2) Later as RAM became cheaper, I learned how to load the entirety of Windows in RAM. This made Windows fairly invincible, because even if some kind of corruption happened, it would disappear after reboot.

3) With Linux, I like RAM drives as temporary work places for files which have no life expectancy, such as the files used in scripting or things like ripping a music disc and converting the wav files to mp3s. The RAM drive is fast and it's no wear and tear on the hard disk.

4) (Many of my scripts manipulate files and I have a wrkdir variable pointing to a RAM disk.)

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

I don't know that you need or want a RAM Drive, or this kind of RAM Drive. Or if you already have one. Nevertheless, I'll show how.

Pick a ram device and a mount point

In /etc/rc.d/rc.local

mke2fs -m0 /dev/ram3
mount -t ext2 /dev/ram3 /mnt/ramd


Puppy's default will usually give about a 14MB drive, this size can be increased on the kernel line, like this;

ramdisk_size=65536 (would give a 64MB RAM drive)

In earlier tests ( versions 2.xx ), I found Puppy's upper limits between 400 and 500 MB

In the next chapter I'll discuss a different kind of RAM filesystem.

~

Chapter 89 - How to make a RAM drive

Bruce B

#89 Post by Bruce B »

Chapter 90 - tmpfs on /dev/shm

This type of RAM filesystem is very common. I've seen it called an shmfs (in older days) and more currently, tmpfs. The mount point has always been /dev/shm. But should be able to use other mount points.

Some Puppys have this filesystem and some don't. I think I even saw one version were /tmp was a symlink to /dev/shm.

My Puppy didn't have the tmpfs, so I made one. First step is adding this line to /etc/fstab;

Code: Select all

tmpfs	/dev/shm	tmpfs    noauto,rw,noatime,nodiratime 0 0
then in /etc/rc.d/rc.local run this command;

mount /dev/shm

You can also mount a tmpfs /dev/shm with a full set of mount instructions, but why bother? One fstab entry makes it much easier.

The default tmpfs size is 50% of RAM

You can change the size with this parameter, in fstab;
size=800M (if you wanted an 800MB tmpfs, I use 800 MB for CD copy)

Code: Select all

tmpfs	/dev/shm	tmpfs    noauto,size=800M,rw,noatime,nodiratime 0 0
This type of filesystem doesn't actually deplete your RAM.

I conceptualize it this way. The first type of RAM disk I showed is static in size on not accessible for paging. Tmpfs is dynamic, and used as a part of available memory, sort of like a swap file in RAM. (A RAM disk that doesn't really use RAM?)

I don't want to say which is better, because the answer is likely dependent on what it's used for.

I use the /dev/shm for SeaMonkey's disk cache set at 100MB. Also as my $wrkdir. And for major work like copying a cd disc.

~

Chapter 90 - tmpfs on /dev/shm

Bruce B

#90 Post by Bruce B »

Chapter 91- A 100 more to go ?

Sorry for few weeks lost making new chapters. This is not a work completed. There is a story about the lost time. One I hope you find of some interest.

I've been doing this project on a highly modified Puppy version 4.00. I wanted for us to be able to convert man pages to HTML or text format. I thought Puppy had a file called man2html, but wanted to be sure. It appears it didn't.

I decided to build a new Puppy, one which would lend itself better to this project. With me doing things different, like keeping a log of changes, having fresh pup_saves for testing, keeping addon files in separate directories and etc.

So, I setup a new Puppy and added many applications, even put together a new Midnight Commander. Then, the fateful day. I ran a filesystem check and found it was full of errors.

I decided, no way. I'm starting over with a pup_save with a ext3 filesystem, not the default ext2. For about a year and a half now, I've been using ext3 with virtually no file system errors, and I might add, under very heavy usage.

Then my cable modem went bad. Then I decided to upgrade the firmware for my old Linksys router and the upgrade went bad. So buy a new router. While I'm at it how about a controller card for another drive? Why not extra DVD discs and, what would be wrong with a new Plextor burner?

Well you get the picture.

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

The part that bothers me from an 'us' perspective is the excessive ext2 file system errors.

What you can do is something along these lines, if you are using Frugal on an HD (probably a RAM stick also), for the purpose of checking.

1) boot using the puppy pfix=ram option
or
2) add a extra small pup_save file about 32 MB

Find your pup_save file, run e2fsck -f on your pup_save file and see if you are getting too many errors.

If so, you might want to plan to convert to an ext3 file system.

We'll deal with the 'how to' on that later.

Nuff

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

I have questions to test your learning, here is a function:

Code: Select all

nd() {
	[ ! -d $1 ] && mkdir $1
	[ $1 ] && cd $1
}
Question: What does it do and where should it go?


~

Chapter 91- A 100 more to go ?

Bruce B

#91 Post by Bruce B »

Chapter 92 - a script wanted

Script wanted:

Friends, I've tested the 'proof of concept', the file I'm wanting is easy to make. I'm hoping the readers have been programming and can do this assignment. If you want to work on it, but are embarrassed or not confident, PM me and I'll post it synonymously.

Filenane: checkdiff

Description: using the find command it searches the /parent directories and makes a text database of all files, symlinks and directories in a /parent directory. e.g. /usr and /root

We want to learn about changes in the directory from time to time. We use the find command a second time, merge the two files and pipe the test file to sort | uniq -u.

uniq only works right on a list of sorted files, the uniq -u swich will print only the differences.

Thus we can get a report on files which have been added or deleted.

Fun practical and I have an even more useful similar utility up my sleeve, but I want to see how we do on this.

~

Chapter 92 - a script wanted

Bruce B

#92 Post by Bruce B »

Chapter 93 assignment submitted

I'm pleased to present anonymously submitted work from chapter 92.

First version

Code: Select all

#!/bin/bash
cd /usr/
# check if a list exists: if not create one and exit.
if
[ ! -e usr-list.cd ]
then echo "Nothing to compare. Creating 'usr-list' then exiting"
find -maxdepth 1 > usr-list.cd && exit
fi

find -maxdepth 1 > usr-list-new.cd
cat usr-list.cd usr-list-new.cd > joined.cd
sort joined.cd | uniq -u
# get rid of the temporary files
rm -f *.cd

exit 
Second version

Code: Select all

#!/bin/bash
# Just realised my basic error
if
[ ! -e list ]
then echo "Nothing to compare. Creating 'list' then exiting"
find -maxdepth 1 > list && exit
fi

find -maxdepth 1 > list-new.cd
cat list list-new.cd > joined.cd
sort joined.cd | uniq -u
rm -f *.cd
exit 


Third version

Code: Select all

#!/bin/bash
# I think I've finally got something a little more sensible:
if
[ ! -e /root/list ]
then echo "Nothing to compare. Creating 'list' then exiting"
find -maxdepth 1 > /root/list && exit
fi

find -maxdepth 1 > /root/list-new
cat /root/list /root/list-new > /root/joined
sort /root/joined | uniq -u
exit

# I just put the temporary files in root, out of the way, 
# and they get overwritten anyway - so no need for 'rm' really. 
Comment: Thanks for the submission. This is exactly how I learn. I write some code. Then, maybe the next day, I conceive a better way or notice a flaw, then refine or even rewrite the code.

In the beginning stages some can learn by lectures, examples and theory. As for me I need to 'code' to develop the skill and confidence.

Thanks again

~

Chapter 93 assignment submitted

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#93 Post by technosaurus »

Simple ldd script to find dependencies - should work on any *nix

Code: Select all

#!/bin/sh
if [ -x $1 ]; then
/lib/ld-linux.so.2 --list $1
else
 if [ -x `which $1` ]; then
 /lib/ld-linux.so.2 --list `which $1`
 else
 echo Can't find it. Try using full path.
 fi
fi
(This one has the comments remove - only 98bytes)

#!/bin/sh
if [ -x $1 ]; then /lib/ld-lsb.so.1 --list $1
else
/lib/ld-lsb.so.1 --list `which $1`
fi

(this one requires the full path - 37 bytes)

#!/bin/sh
/lib/ld-lsb.so.1 --list $1
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#94 Post by 01micko »

hehe

simple word/string finder

Inspired by techno :wink: In living colour :lol:

EDIT: Well I posted this in a separate thread and am maintaining it, the code has progressed from what is below. It mainly demonstrates the use of colours in the CLI. Enjoy! SCLISS

Code: Select all

#!/bin/sh
#01micko (01micko@gmail.com) GPL 2010
#simple command line search script
#syntax "scliss -w" for a word
#"scliss -s" for a string
#"scliss -h" help
#"scliss -v" version
#if you want to colour your bash I got it from "http://edoceo.com/liber/linux-bash-shell"
VER="0.1"
DIR=$2
if [ "$DIR" != "" ];then cd $DIR 2>/dev/null
fi
if [ "$DIR" = "/tmp" ];then
	echo -en "\033[0;35m""Er sorry.. haven't figured out how to handle /tmp yet"  #magenta text
	echo -e "\033[0m"
	exit
fi
if [[ $? -ne 0 ]];then
	echo -en "\033[0;31m""Woops! That's not a directory!"  #red text
	echo -e "\033[0m"
	exit
fi
case $1 in
-w)	echo "type one word you are searching for"
	read SEARCH
	grep $SEARCH *
	;;
-s)	echo "type a string you are searching for"
	read STRING
	grep "${STRING}" *
	;;
-h)	echo "You have invoked scliss help"
	echo "Options"
	echo "	-h prints this help and exits"
	echo ""
	echo "	-v prints version and exits"
	echo ""	
	echo "	-w) type a word to search for in the current directory"
	echo "	and the line(s) where that word resides will be returned for" 
	echo "	every instance it occurs in a line"
	echo "	SYNTAX: scliss -w [/path/to/dir]"
	echo ""
	echo "	-s) type a string to search for in the current directory"
	echo "	and the line(s) where that string resides will be returned for" 
	echo "	every instance it occurs in a line"
	echo "	SYNTAX: scliss -s [/path/to/dir]"
	echo ""
	echo -en "\033[1;33;45m""	the \"/path/to/dir\" is optional"  #yellow text bright, magenta background
	echo -e "\033[0m"
	echo "============================================================"	
	echo -en "\033[0;34m""01micko@gmail.com GPL 2010 NO WARRANTY" #blue text
	echo -e "\033[0m"
	exit
	;;
-v) echo "scliss-${VER}"
	exit
	;;
*)	echo "ERROR: You need an option. Type \"scliss -h\" for more"
	exit
	;;
esac
	if [[ $? -ne 0 ]];then
	echo -en "\033[0;31m""Boo! Not found"  #red text
	echo -e "\033[0m" 
	exit
		else echo -en "\033[0;32m""There you go, found it!"  #green text
		echo -e "\033[0m"
		exit
	fi
Cheers
Puppy Linux Blog - contact me for access

Bruce B

#95 Post by Bruce B »

Very good!

Years ago I made simple color changing C source code to use in DOS batch programming.

The basic idea is: it was easier to type in the first three characters of a color than calling a batch file or running the full escape sequence.

If I want text to change to magenta, I enter the command 'mag' then when I want to change the text output again, I enter another color command.

The command itself doesn't insert a line break or change cursor position, it just makes to the text after the command the color I want.

I'm pasting a picture of the C source rather than posting the code as text, because the escape character usually doesn't transfer.

I think it would be very easy to make Linux code by using its escape and color sequences and the printf function.

If you are interested in the project and sharing your work :)
Attachments
magenta.jpg
(15.59 KiB) Downloaded 6124 times

Post Reply