2012年6月26日星期二

Setting option flags -n -v -x

sh -n scriptname checks for syntax errors without actually running the script. This is the equivalent of inserting set -n or set -o noexec into the script. Note that certain types of syntax errors can slip past this check.

sh -v scriptname echoes each command before executing it. This is the equivalent of inserting set -v or set -o verbose in the script.

The -n and -v flags work well together. sh -nv scriptname gives a verbose syntax check.

sh -x scriptname echoes the result each command, but in an abbreviated manner. This is the equivalent of inserting set -x or set -o xtrace in the script.

Inserting set -u or set -o nounset in the script runs it, but gives an unbound variable error
message and aborts the script.

2012年6月25日星期一

Playing music

#!/bin/bash
# music.sh
# MUSIC WITHOUT EXTERNAL FILES

# Author: Antonio Macchi
# Used in ABS Guide with permission

# /dev/dsp default = 8000 frames per second, 8 bits per frame (1 byte),
#+ 1 channel (mono)

duration=2000 # If 8000 bytes = 1 second, then 2000 = 1/4 second.
volume=$'\xc0' # Max volume = \xff (or \x00).
mute=$'\x80' # No volume = \x80 (the middle).

function mknote () # $1=Note Hz in bytes (e.g. A = 440Hz ::
{ #+ 8000 fps / 440 = 16 :: A = 16 bytes per second)
   for t in `seq 0 $duration`
   do
     test $(( $t % $1 )) = 0 && echo -n $volume || echo -n $mute
   done
}

e=`mknote 49`
g=`mknote 41`
a=`mknote 36`
b=`mknote 32`
c=`mknote 30`
cis=`mknote 29`
d=`mknote 27`
e2=`mknote 24`
n=`mknote 32767`
# European notation.

echo -n "$g$e2$d$c$d$c$a$g$n$g$e$n$g$e2$d$c$c$b$c$cis$n$cis$d \
$n$g$e2$d$c$d$c$a$g$n$g$e$n$g$a$d$c$b$a$b$c" > /dev/dsp
# dsp = Digital Signal Processor
exit # A "bonny" example of a shell script!

Open a TCP connection to the associated socket.

/dev/tcp/$host/$port

Checking whether a disk is in the CD-burner (soft-linked to /dev/hdc):

head -1 /dev/hdc

# head: cannot open '/dev/hdc' for reading: No medium found
# (No disc in the drive.)

# head: error reading '/dev/hdc': Input/output error
# (There is a disk in the drive, but it can't be read;
#+ possibly it's an unrecorded CDR blank.)

# Stream of characters and assorted gibberish
# (There is a pre-recorded disk in the drive,
#+ and this is raw output -- a stream of ASCII and binary data.)
# Here we see the wisdom of using 'head' to limit the output
#+ to manageable proportions, rather than 'cat' or something similar.

why is it called "loopback" device?

from http://stackoverflow.com/questions/3614719/what-does-loop-mean-in-loop-device

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


Why is it called "loopback" though? I can see two parallels: Loopback of the network device (i.e having some word play), or loop back in the sense that the driver asks back into the file-system again for the file to map into. What meaning is correct? – Johannes Schaub - litb Sep 1 '10 at 4:02
I think it's the latter one. Correct me if I am wrong. – smwikipedia Sep 1 '10 at 4:14

3  
It's the latter one. Normally a request would go userspace -> VFS -> block device layer, but when the block device driver is loop, it goes userspace -> VFS -> loop device -> VFS -> block device. So it loops back to a higher level. – hobbs Sep 1 '10 at 4:19

As far as I know the term originated in the depths of the telecom industry (and probably dates back to telegraphs). See mmonem's answer. – RBerteig Sep 1 '10 at 6:44


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

I guess this term comes from the communication realm when sometimes it is needed to test the communication system by simulating a peer using a proxy circuit loop.
The concept came also to UNIX networking where loopback network interfaces do not send network traffic to the medium.
The same concept in file systems loop means that the file system driver does not really goes through the hard disk IO stack and, instead, ends using a plain disk image file for IO.

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



2012年6月12日星期二


tar -jxvf xx.tar.bz2


4
很正常, valgrind的机制是simulate 指令, 并且替换malloc/free一类的调用
栈上的不是用malloc分配的, 所以它弄不清楚
换句话说比如
int a[64];
int b;
假设分配时b在a 后面
那CPU要访问a[64]和访问b, 产生的指令是一模一样的, valgrind当然不知道, 因为它
不管源代码




strdup.c

char *strdup (const char *s) {
    char *d = malloc (strlen (s) + 1);   // Space for length plus nul
    if (d == NULL) return NULL;          // No memory
    strcpy (d,s);                        // Copy the characters
    return d;                            // Return the new string
}