Mittwoch, 25. April 2012

Preparing LPIC-1, #7 umask

The command 'umask' sets the default permissions on files and directories. The umask itself is specified as digits which are subtracted from the standard permissions maks, which is
  • 666 for files (that is rw-rw-rw-),  and
  • 777 for directories (that is rwxrwxrwx)
So if 'umask 002'  is specified, files will have standard permissins of 664 and directories will have 775.

'umask' without any parameters displays the currently set umask. If you give a parameter, 'umask' will interpret it as a umaks and set it. Thus you can change your umask by putting something like 'umask 022' in your .profile.

Mittwoch, 18. April 2012

Building a playground quota system on linux

I wanted to get a bit of experience using the linux quota system. I had no extra partition or disk at hand, so I created a virtual file system image. First I needed a file for this:

dd if=/dev/zero of=disc.img bs=1k count=100000

Then I created a partition table and a partition on it (not quite shure if this is really nessessary) using fdisk:

fdisk disc.img

Then I created a filesystem on my virtual partition:

mkfs.ext3 disc.img

ignoring the warning, that my disc.img is not a block device. Note that other filesystems would be possible, but  LPIC1 (for which I am currently preparing) does not require ext4 or btrfs which would be more modern filesystems. This might change soon though. I hear new exams are comming this summer.

Then I mounted it with the option for user quotas:

mkdir mnt
sudo mount  -o loop,usrquota disc.img mnt

Then I needed to create a quota file. This is a file wich resides at the root of the filesystem for which we want to use quotas. It can be done like this:

sudo quotacheck -u mnt/

This creates a  aquota.user file, which enables quotas for users. Now I set a quota


sudo setquota -u isaac 1000 10000 100 200 mnt/

following the syntax
 setquota -u user  <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit>

Bash filedescriptors and IO redirection with exec

The topic is very well covered in my favorite Bash-Scripting Guide. However, in a few examples they used something like the following code:

#!/bin/bash
# read a file line by line and echo it

filename=$1

exec 3<> $filename
while read line <&3
  do
  echo $line
done

exec 3>&-

The line "exec 3<> $filename" redirects the $filename to filedescriptor 3, which is then used as stdin in the next line. This example just uses input from the file, so I wondered why there was a "<>" instead of just "<", which would be sufficient for just reading the file. The reason is simple: Even though for this example it is not needed, the file is not only opened for reading, but also for writing, thus the ">". The last line "exec 3>&-" closes the file again.

We could modify the example to take advantage of the writing:

#!/bin/bash
# read a file line by line and echo it

filename=$1

exec 3<> $filename
while read line <&3
  do
  echo $line
done
echo another line >&3

exec 3>&-

This adds another line to the file given to the script as first argument. Its the "echo another line >&3" ( -- I should probably start using proper code highlighting ).

Montag, 2. April 2012

ISO-8859 to UTF8 conversion

I thought I was through with this long time ago, but just today it bit me again: a text file with German umlauts was not displaying correctly in less. It turned out to be ISO-8859 encoded ... To make sure I'll remember, here is what I did:

iconv --from-code=ISO-8859-1 --to-code=UTF-8 file.txt >file_utf8.txt

Again, this has been described a lot of times elsewhere.