Category Archives: unix

fun with bash: append text to a file

There is time when we need to edit a file on linux, or unix for that matter, and usually a configuration file. And to me at least, it is usually append to end of a file.

On the other hand, unix got many many, text manipulating utilities, and often, one liner to do text manipulation exist. From cat, to sed. And some an echo will do.

To append a line to a file, it just

echo “your text”>> yourFile

here’s a bit explaination, in layman term

echo “your text”

will by default, print to your screen, which comes to

>>

which means, redirect output to, somewhere. Another variation is, >, the difference is that, this will replace, the whole thing. >> will append

>> yourFile

means redirect to your file.

And there you go, a one line to append a file.

fun with python: running unix command with commands module

One of the best thing in python is, the fact that it have a “battery included”, this I really believe.

There is a few ways of running system command on python. There is the popular, execl, execlv, etc in the os modules, which is cross platform.

There is also a commands module. What is cool about it, is that, it will generate output. Where as execl, will exit python shell, back to the parent shell. Meaning, it’s easy to write code that read an output of a command, and put it in a front end of something. While it is cool, the command module comes with a cost, it’s only available for unix like system, such as linux, bsd, and other unix.

Here’s an example, assumes that you already started python, in python shell, either in terminal, or idle, type

>>> import commands

this will loads the modules, now type

>>> commands.getstatusoutput(‘ls /’)

you should get something like this:

(0, ‘bin\nboot\ncdrom\ndebian\ndev\netc\nhome\ninitrd\ninitrd.img\nlib\nlib32\nlib64\nlost+found\nmedia\nmnt\nopt\nproc\nroot\nsbin\nsrv\nsys\ntmp\nusr\nvar\nvmlinuz’)

the output depends on the system you have. the first part of the tuple is the exit code, which since the code successfully executed, it’s 0. the second part is the output of the command, separated by \n.

Now type

>>> commands.getoutput(‘ls /’)

you should get something like this

‘bin\nboot\ncdrom\ndebian\ndev\netc\nhome\ninitrd\ninitrd.img\nlib\nlib32\nlib64\nlost+found\nmedia\nmnt\nopt\nproc\nroot\nsbin\nsrv\nsys\ntmp\nusr\nvar\nvmlinuz’

the output differs between system. now you get a string again separated by \n.

last methods that is available to the commands module is. getstatus, this can only run on an directory:

>>>commands.getstatus(‘/’)

the output should be something like this.

‘drwxr-xr-x 23 root root 4096 2007-12-11 13:21 /’

What’s interesting is, it getoutput, and getstatusoutput, applies to many system command(if not all).

interesting example that I do. This is to print the result of ping, interesting example

import commands
s=commands.getoutput(‘ping google.com -c 10’)
#i try to limit the command to make sure it stops
l=s.split(‘\n’) #because it’s separated by \n
for line in l:
print l

another interesting thing to do is, is to get cpu info, nothing that cannot be done, using open, since it uses the proc filesystem(gotta love /proc) but still it’s interesting.

import commands
s=commands.getoutput(‘cat /proc/cpuinfo)
for line in s.split(‘\n’):
print line

the commands module is an interesting way to automate stuff on unix. Pity it doesn’t work, on windows. But still it’s fun, and interesting. Something the original unix principle.

playing with simh: running unix v5

One of the thing in computer history that I’m fascinated is unix. It’s one of the most important Operating System, that most people today didn’t use. And already have many concept, that is added much later time, in commercial PC operating system. And it already have the concept of code sharing, before Free Software or Open Source exist.

Pretty all of the resource is from this article. Except my opinion

To simplify the step.

1) download http://simh.trailing-edge.com/kits/uv5swre.zip
2) unzip everything
3) create a pdp11.ini

set cpu U18
attach rk0 unix_v5_rk.dsk

3) then run the pdp11 emulator. This I assume that you installed simh already.
note: on windows you might need to copy the pdp11 emulator, from simh, to the same folder as the unix.

you should see the following. prompt

PDP-11 simulator V3.7-0
Disabling XQ
@

just type unix

which shows

;login:

, then type root. Now you should see the familiar root shell.

But note it’s not a unix as we know on linux, or bsd, or any modern unix(i think), for example there is no cd, it’s chdir. And many thing is not there, or very different.

But that’s unix. What cool, is that, it contain the source code for unix as well. And amazing how to stuff the OS, in a relatively small package

lesson learns, usermod command

on linux, probably unix too.
to modify a user a user information on shell you type

usermod

here’s what i learn, to assignment a group to user, you type

usermod -G groupname username

here’s the catch, it will remove all the other group the user is in.

Another thing that I learn,is that to append, a group to a user, on shell, use the -a parameter.
So to append group to user, type:

usermod -aG groupname username

one more thing, always add the -a parameter, before -G, for some reason, -G assumes that -a is a group. so it is always

usermod -a -G groupname username
usermod -aG groupname username

but never

usermod -Ga groupname username
usermod -G -a groupname username

could be an ubuntu bug.